Link to home
Start Free TrialLog in
Avatar of parveenmehta
parveenmehta

asked on

Deleting control in runtime

I am adding a label to an image in PictureBox during in run time.
How can i delete it in runtime only if it exists ? I am coding in C#.
Thanks
Avatar of mytonytiger
mytonytiger

Assuming you have the label in the picturebox:

pictureBox1.Controls.Remove(lbl);


if not, whatever container you have it in you will need to access the Controls collection and remove it.
Avatar of parveenmehta

ASKER

This is exactly what i have done. I want only one label box displaying at a given time.If user click insert text again then previous lable should be delted and another one should be added.

public void insert_text_Click(object sender, System.EventArgs e)
            {                  
                        Label lbl = new Label();                  
                        lbl.Size=new Size(130,40);                        
                        lbl.Text = textBox2.Text;
                        lbl.BackColor = Color.White;
                        lbl.Location = new Point(336, 150);                  
                        lbl.MouseDown += new MouseEventHandler(lbl_MouseDown);
                        lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
                  if(pictureBoxEx1.Controls.Contains(lbl))
                  {pictureBoxEx1.Controls.Remove(lbl);
                  }
                        pictureBoxEx1.Controls.Add(lbl);                        
                        insert_text.Enabled=false;                  

            }
Any help is appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial