1

I have an mdichild, and an eventhandler for draganddrop so when I drop an image file in my form, a picturebox ( name = dpic ) is created with that image.

I have another eventhandler which is for dpic_Click, so when I click on the image, my form's text is the name of that image.

After the first time I drop an image, another dpic is created, because you can't have two cotrols with the same name. C# automatically changes the name and that makes my event handlers only work for the last image I dropped in. I think if I could make an event handler for my mdichild that gets the name of the control that is under the mouse pointer I could simply change back the image I am pointing at.

UPDATE

Here is my code , I made an event handler for droping in my mdichild :

void mdiChild_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {                
            dpic = new PictureBox() ;
            string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop);
            Image image = Image.FromFile(filepath[0]);
            dpic.Image = image;
            dpic.Tag = Path.GetFileName(filepath[0]);                
            this.ActiveMdiChild.Controls.Add(dpic);
            dpic.ContextMenuStrip = this.contextMenuStrip1;
            this.ActiveMdiChild.Refresh();
            dpic.BringToFront();
            this.ActiveMdiChild.ActiveControl = dpic;

            dpic.Click += new EventHandler(dpic_Click);  
// _____this helped me do it_________________________________________________
           foreach (Control c in this.ActiveMdiChild.Controls)
            {
                c.Click += new EventHandler(c_Click);
            } 
// ________________________________________________________________________
        } 
    }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Pedrum
  • 634
  • 3
  • 7
  • 16
  • WPF? Silverlight? Winforms? ASP? if you tell us the platform, we have a better idea of a solution. – Muad'Dib May 01 '11 at 03:12
  • 1
    Because you didn't provide a code snippet, people will guess when making answers for you, and their answers might be hard to integrate. Please provide the code so people can provide better answers to your specific problem. – John K May 01 '11 at 03:30
  • ok , here is the code :) – Pedrum May 01 '11 at 03:42
  • Fixed your code formatting (missing line break got it messed up). :) – ssube May 01 '11 at 03:42
  • 1
    @Pedrum Windows Vista is your operating system, not your platform. – Muad'Dib May 01 '11 at 03:44
  • This might be what you're looking for: http://stackoverflow.com/questions/1071579/user-control-click-windows-forms – Jim Mischel May 01 '11 at 04:11
  • unfortunately it didnt work but it got me one step closer :) now i need a code that helps me get the name of the control i clicked on ... in the even that link u game me creates .. – Pedrum May 01 '11 at 04:27
  • ITS WORKING >:D< I LOVE U ! :) I USED MRROY's Code in the code u gave me THX – Pedrum May 01 '11 at 05:11

2 Answers2

0

What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.

PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;

EDIT : You put that in the pic_Click event

MrRoy
  • 1,145
  • 10
  • 9
  • ok , thx but it didnt work i think there is another problem now. – Pedrum May 01 '11 at 03:23
  • i added that and it worked , only when i had only one picturebox – Pedrum May 01 '11 at 03:24
  • You must've put the code at the wrong place. All your pictureboxes must be associated to the same event – MrRoy May 01 '11 at 03:25
  • Also, I said to put the code in pic_Click, but I meant to put it in the event you use to drag and drop it, so it may be another – MrRoy May 01 '11 at 03:31
  • void mdiChild_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { dpic = new PictureBox() ; string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop); Image image = Image.FromFile(filepath[0]); dpic.Image = image; dpic.Tag = Path.GetFileName(filepath[0]); this.ActiveMdiChild.Controls.Add(dpic); this.ActiveMdiChild.ActiveControl = dpic; } } – Pedrum May 01 '11 at 03:35
  • when i drop a pic , dpic is created with that picture in it. if i put an event handler for dpic which is my picture box , it'll only be for the last dpic created becasue the old ones names change cuz u cant have same controls with same names ... – Pedrum May 01 '11 at 03:35
  • Do not reinitialize dpic in the event. Instead, use the code I posted to use a sender. The sender tells the event what picturebox, in this case, called the event. So you will be able to use the code with the image that was sent instead of only one. – MrRoy May 01 '11 at 03:45
  • im sorry , im really stupid :D can u tell me with more details.. , like , wat should i change now ? where do i put the sender ? btw thx for that sender idea im sure it'll work with ur help , thx alot – Pedrum May 01 '11 at 03:55
  • Remove `dpic = new PictureBox() ;` from your code, and replace it with the above (`PictureBox picSender = (PictureBox)sender;`), in the rest of the code, replace dpic by picSender (so, `picSender.Image = image;` instead of `dpic.Image = image`). That should do it – MrRoy May 01 '11 at 04:01
  • I tried it but it didnt work , i couldnt even drop anything anymore ... oh goddd this seems impossible :( – Pedrum May 01 '11 at 04:24
  • @Pedrum are you sure you replaced all the `dpic` by `picSender`? – MrRoy May 01 '11 at 04:59
0

I don't understand exactly what you want to do here, possibly layering new pictureboxes with dropped images on the form?

If that is so you can use a

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

and where you do:

 dpic = new PictureBox() ;

change to

PictureBox dpic = new PictureBox() ;
 picboxes.Add(dpic);
 this.Controls.Add(dpic);

But please note that you cannot have unlimited controls declared.

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61