0

I am facing an issue when I develop my project. I am sorry that my English is not good. Hope you guys can understand what I am talking.

enter image description here

From the image you guys can see one X button. This X button is a picture box. I am trying to add function that make this X button be able to delete the mote(the blue wireless picture box).

How can I do it?
Thanks so much

Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

2

Dividing your problems into 2 parts

  1. Getting Click event of PictureBox
  2. Removing some control on that click event

1. Getting Click event of PictureBox

In order to do that you simply have to Double-click the PictureBox and a method will be generated

Or you can attach new method with click event Like

picOneFaceUpA.MouseClick += new MouseEventHandler(your_event_handler);

Or:

picOneFaceUpA.MouseClick += new MouseEventHandler((o, a) => code here);

Ref:- Adding a mouse click eventhandler to a PictureBox

2. Removing some control on that click event

You have multiple options to remove control from the panel

 foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        panel1.Controls.Remove(item); 
    }


   //to remove control by Name
    foreach (Control item in panel1.Controls.OfType<Control>())
    {
        if (item.Name == "bloodyControl")
            panel1.Controls.Remove(item); 
    }


    //to remove just one control, no Linq
    foreach (Control item in panel1.Controls)
    {
        if (item.Name == "bloodyControl")
        {
             panel1.Controls.Remove(item);
             break; //important step
        }
    }

Ref:- Removing dynamic controls from panel

Hope this helps.!

Happy Coding.!!!

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • The [Clear](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.clear(v=vs.110).aspx) method removes all controls but doesn't free their handles/memory. I expect the same with the RemoveXXX methods. So fo a clean removal one might have to Dispose of the control as well.. – TaW Jul 02 '18 at 10:13
0

Im not sure if I understood your question, but here is what I got. If what you want is to delete a picture box once you clicked on the X button an easy way would be add a local bool variable 'Delete' set by default as false and activated when click on the 'X' button. So when you click on the Wireless Mote button check if 'Delete' is true, if not do what you want, else, delete it ant set the variable again to false.

F. Iván
  • 167
  • 1
  • 1
  • 12