10 Replies - 30498 Views - Last Post: 25 December 2008 - 11:37 PM Rate Topic: -----

#1 XAssassin007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 08

Dynamically Create Multiple Picture Boxes

Post icon  Posted 23 December 2008 - 09:32 AM

Hi there...i'm trying to create multiple pictureboxes dynamically & also m trying to place images in them through Drag & drop if any1 plz can help me out..plz do so...m waiting for ur replies


Please reply ASAP

Regards;
Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically Create Multiple Picture Boxes

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Dynamically Create Multiple Picture Boxes

Posted 23 December 2008 - 09:34 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 XAssassin007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 08

Re: Dynamically Create Multiple Picture Boxes

Posted 23 December 2008 - 11:00 AM

PictureBox[] Shapes = new PictureBox[Num_Picbox];
					for (int i = 0; i < Num_Picbox; i++)
					{
						Shapes[i] = new PictureBox();
						Shapes[i].Name = "ItemNum_" + i.ToString();
						Shapes[i].Location = new Point(label5.Left+1,label5.Top);
						Shapes[i].Size = new Size(100, 100);
						Shapes[i].BackColor = Color.Black;
						Shapes[i].Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
						Shapes[i].Visible = true;
						this.Controls.Add(Shapes[i]);
					}

 


I don't have much code coz i was jst thinking about it for my project...the problem i think i'll face will be(i think) are:

1) How will i create them; like i want to create them wen my mouse enters PANEL Left_Mouse_Button_CLICKED meaning it contains image to b copied from source picture box to Dynamically Created target PictureBox.
2) Labeling(Naming) Problem for all Dynamically Created Picture Box
3) All the Events of Dynamically Created Pictures box bcoz i want to use them in my project

Please Help!

Regards;
Was This Post Helpful? 0
  • +
  • -

#4 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: Dynamically Create Multiple Picture Boxes

Posted 23 December 2008 - 11:33 AM

1) Create the picturebox holder (listbox control, or treeview, etc) and then add the event code to handle the leftmounsbutton clicked event
2) label for the listbox should be incremented by the current listbox number (i in this case)
3)To add events dynamically, you will need to create delegates for each picturebox that is added.

This post has been edited by tody4me: 23 December 2008 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 XAssassin007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 08

Re: Dynamically Create Multiple Picture Boxes

Posted 23 December 2008 - 11:27 PM

Thanx...but cud u plz give sum code...that wud be more helpful...


Regards;
Was This Post Helpful? 0
  • +
  • -

#6 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: Dynamically Create Multiple Picture Boxes

Posted 24 December 2008 - 08:21 AM

I would be tempted... but will hold off on posting a link to some "code", however will refer you to the original reply that you got from eclipsed.

This post has been edited by tody4me: 24 December 2008 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Dynamically Create Multiple Picture Boxes

Posted 24 December 2008 - 09:43 AM

XAssassin007 we don't just write code and pass it out to everyone that asks, we are here to help you develop your own code and/or help you fix issues with the code that you have written. We will also provide guidance to get you going in the right direction.

With that said, here are some links that will introduce you to the concepts you need to understand to accomplish your task.

Add Control at Runtime
How to programmatically add controls to Windows forms at run time

Give those a try and see what you are able to accomplish. If you get stuck we are always here to help you out.
Was This Post Helpful? 1

#8 XAssassin007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 08

Re: Dynamically Create Multiple Picture Boxes

Posted 24 December 2008 - 10:45 PM

I think u guys understood me wrong...what i meant was it would me helpful for me to understand from code because i can understand the working more quickly that way...i didn't ask that u guys give the exact code or anything like that...

The main problems is that; i want to create an array of picturebox & size of array should increase at the run time on a particular event & also i want to dynamically define their events at run time or if i define a function or a event for one picturebox that function or event should work for all pictureboxes...


Regards;

This post has been edited by XAssassin007: 24 December 2008 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Dynamically Create Multiple Picture Boxes

Posted 25 December 2008 - 09:02 AM

since you are looking for a dynamic array, I would probably look toward using a generic list of picturebox objects. Similar to..

List<PictureBox> pictureBoxList = new List<PictureBox>();

//You could put the following code inside of a loop

PictureBox pic = new PictureBox();
// code to set the PictureBox properties

pictureBoxList.Add(pic);


Was This Post Helpful? 1

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Dynamically Create Multiple Picture Boxes

Posted 25 December 2008 - 11:07 AM

View PostXAssassin007, on 24 Dec, 2008 - 08:45 PM, said:

I think u guys understood me wrong...what i meant was it would me helpful for me to understand from code because i can understand the working more quickly that way...i didn't ask that u guys give the exact code or anything like that...

The main problems is that; i want to create an array of picturebox & size of array should increase at the run time on a particular event & also i want to dynamically define their events at run time or if i define a function or a event for one picturebox that function or event should work for all pictureboxes...


Regards;

I did not misunderstand you, that is why I provided you with links that include examples in code of the concepts you were asking about.

Did you following the links and read the information that I gave you?

It contains everything you need to know to implement your solution. The only thing it did not include is how to create an array of controls. You can either follow eclipsed4utoo advice on how to create the array or you can use an ArrayList to hold all the instantiated controls.
Was This Post Helpful? 0
  • +
  • -

#11 XAssassin007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-December 08

Re: Dynamically Create Multiple Picture Boxes

Posted 25 December 2008 - 11:37 PM


int Num_PicBxs = 0;
List<PictureBox> Shapes = new List<PictureBox>();

private void Add_PictureBoxes()
		{
			try
			{
				PictureBox PicBox = new PictureBox();
				Shapes.Add(PicBox);
				Shapes[Num_PicBxs].Size = new Size(100, 100);
				Shapes[Num_PicBxs].BackColor = Color.White;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

private void Pnl_Layout_DragEnter(object sender, DragEventArgs e)
{
	  try
	  {
		  this.Add_PictureBoxes();				
		  this.Shapes[Num_PicBxs].Location = new Point(e.X,e.Y);				
		  this.Shapes[Num_PicBxs].DragEnter += new DragEventHandler(Shapes_DragEnter);
		  this.Controls.Add(Shapes[Num_PicBxs]);
		  this.Num_PicBxs++;
	  }

	  catch (Exception ex)
	  {
		  MessageBox.Show(ex.Message);
	   }
}
		

		private void Shapes_DragEnter(object sender, DragEventArgs e)
		{
			try
			{  
				if (e.Data.GetDataPresent(DataFormats.Bitmap))
				{
					e.Effect = DragDropEffects.Copy;
				}
				else
				{
					e.Effect = DragDropEffects.None;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void Shapes_DragDrop(object sender, DragEventArgs e)
		{
			try
			{
				if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
				{
					this.Shapes[Num_PicBxs].Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void Pnl_Layout_DragDrop(object sender, DragEventArgs e)
		{
			this.Shapes[Num_PicBxs].DragDrop += new DragEventHandler(Shapes_DragDrop);
		}




The code that i've written is given above but still i'm unable to Drag & Drop the Image from source picture box to target picture box(dynamically Created)...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1