Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
This is my code

MIDL
panel2.Controls.Remove(newlabel);
               panel2.Controls.Remove(newlabel2);


and for some reason it doesn't remove my labels when I click it, nothing happens. I want to be able to remove the labels in 1 whole panel and all the panels and also 1 label at a time. can some one plz help me with my problem?
Posted
Comments
willempipi 28-Mar-11 19:48pm    
Adding and removing controls to a form can be tricky business, you have to ensure you use the right pointers. Maybe it would be a better solution for you to just make them invisible (newlabel.Visible = false; ), are they re-accuring labels?

You removal code is correct. You have the problems somewhere else. You say "when I click it". You don't show that you handle any mouse click events. First, you need to make sure the code where you add the delegate to the click events really runs. If it does, you need to makes sure your event handler is called. When you do this, you need to make sure that the event handle actually calls Control.Remove.

Did you use the debugger? Place breakpoints in all three points to see what's missing. If you still cannot figure out who to make it working, show the relevant fragments of your code: the click handler and the place where the delegate is added to the event; the describe which of the breakpoints were not reached.

[EDIT]
After looking at the source code:
It does not say that the method panel2_MouseClick is not a handler, it's just some method. Where it is called as a handler? Probably this is a problem. This method can become a handler if your write:

C#
panel2.Click += panel2_MouseClick;


If there is not such thing, this is a problem. Also I told you: you need to setup 3 breakpoints and report if the code gets there. Did you do it? You did not answer the question. If you're not going to use the debugger, say bye-bye to you programming.

By the way, this syntax is the old ugly syntax still used in auto-generated code. This is how it should look if you do it right:

C#
panel2.Click += (sender, eventArgs) => {
   //call whatever your want here;
   //not you don't have to pass both parameters sender, eventArgs:
   //pass only what you really need
   //or put some code right here
   //also, you need not to declare types of the parameters, as they are inferred by the compiler
   //from event type.
};


—SA
 
Share this answer
 
v2
Comments
[no name] 28-Mar-11 20:02pm    
Heres the whole code

private void panel2_MouseClick(object sender, MouseEventArgs e)
{

Label newlabel = new Label();Label newlabel2 = new Label();
switch (e.Button)
{
case MouseButtons.Left:
panel2.Controls.Add(newlabel);
newlabel.Text = "0";
newlabel.Size = new Size(10, 15);
newlabel.BackColor = Color.Transparent;
newlabel.Location = new Point(e.Location.X, 7);
break;

case MouseButtons.Right:

panel2.Controls.Add(newlabel2);
newlabel2.Text = "1";
newlabel2.Size = new Size(10, 15);
newlabel2.BackColor = Color.Transparent;
newlabel2.Location = new Point(e.Location.X, 7);
break;

case MouseButtons.Middle:
panel2.Controls.Remove(newlabel);
panel2.Controls.Remove(newlabel2);
break;
}

}
Sergey Alexandrovich Kryukov 28-Mar-11 20:26pm    
You did not provide most important part! No wonder you have problems.
See the update of my Answer.
--SA
Sergey Alexandrovich Kryukov 28-Mar-11 23:35pm    
The real reason is most probably the one pointed out by Henry and JOAT-MON.
--SA
You are unable to remove your labels because your Remove() method refers to the labels you create in your event handler NOT the ones on your Panel.

Your code - annotated
C#
private void panel2_MouseClick(object sender, MouseEventArgs e)
{
  Label newlabel = new Label();  // NEW LABELS CREATED HERE
  Label newlabel2 = new Label(); // AND HERE

  switch (e.Button)
  {
    case MouseButtons.Left:
      panel2.Controls.Add(newlabel);
      newlabel.Text = "0";
      newlabel.Size = new Size(10, 15);
      newlabel.BackColor = Color.Transparent;
      newlabel.Location = new Point(e.Location.X, 7);
      break;
    case MouseButtons.Right:
      panel2.Controls.Add(newlabel2);
      newlabel2.Text = "1";
      newlabel2.Size = new Size(10, 15);
      newlabel2.BackColor = Color.Transparent;
      newlabel2.Location = new Point(e.Location.X, 7);
      break;
    case MouseButtons.Middle:
      panel2.Controls.Remove(newlabel); // newlabel is the one created at the start of this method
      panel2.Controls.Remove(newlabel2); // as is newlabel2 THEY ARE NOT THE ONES (if any) ON YOUR PANEL.
      break;
  }
}


If you make this slight modification:
C#
case MouseButtons.Middle:
  if (panel2.Controls.Contains(newlabel))
  {
      MessageBox.Show("Henry was a muppet");
      panel2.Controls.Remove(newlabel);
  }
  else
  {
      MessageBox.Show("Don't be silly");
  }
  panel2.Controls.Remove(newlabel2);
  break;


And run it, do you want to guess which message will show?
 
Share this answer
 
v2
Comments
JOAT-MON 28-Mar-11 21:24pm    
+5 from me
Sergey Alexandrovich Kryukov 28-Mar-11 23:30pm    
Correct; I did not pay attention it's local variable. Well spotted! My 5.
--SA
After looking at the code you posted in SAKryukov's answer, I can see that you are instantiating newlabel and newlabel2 each time the mouse is clicked. The problem is that these instantiations will not be pointed at the same controls that were added to the panel. Instead, they will be brand new label controls each time.

Option 1 - What you will need to do is specify the Name property of each label and then remove them using the Name:
C#
private void panel2_MouseClick(object sender, MouseEventArgs e)
{
    switch(e.Button)
    {
        case MouseButtons.Left:
            Label newlabel = new Label();
            newlabel.Name = "newlabel";
            newlabel.Text = "0";
            newlabel.Size = new Size(10, 15);
            newlabel.BackColor = Color.Transaparent;
            newlabel.Location = new Point(e.Location.X, 7);
            panel2.Controls.Add(newlabel);
            break;
        case MouseButtons.Right:
            Label newlabel2 = new Label();
            newlabel2.Name = "newlabel2";
            ... // continue in same fashion as newlabel above
        case MouseButtons.Middle:
            panel2.Controls.RemoveByKey("newlabel");
            panel2.Controls.RemoveByKey("newlabel2");
            break;
    }
}



Option 2 - You will need to instantiate the labels outside the event handler so that when you remove them, they will still be pointed at the ones that were added (this assumes you are only adding one of each). You will not need to specify the Name property:
C#
Label newlabel, newlabel2;

private void panel2_MouseClick(object sender, MouseEventArgs e)
{
    switch(e.Button)
    {
        case MouseButtons.Left:
            newlabel = new Label();
            newlabel.Text = "0";
            newlabel.Size = new Size(10, 15);
            newlabel.BackColor = Color.Transaparent;
            newlabel.Location = new Point(e.Location.X, 7);
            panel2.Controls.Add(newlabel);
            break;
        case MouseButtons.Right:
            newlabel2 = new Label();
            ... // continue in same fashion as newlabel above
        case MouseButtons.Middle:
            panel2.Controls.Remove(newlabel);
            panel2.Controls.Remove(newlabel2);
            break;
    }
}


Option 3 - Loop through the controls and remove all Label controls that have been added:
C#
int counter = 1;

private void panel2_MouseClick(object sender, MouseEventArgs e)
{
    switch(e.Button)
    {
        case MouseButtons.Left:
        case MouseButtons.Right:
            Label newlabel = new Label();
            newlabel.Name = "newlabel" + counter.ToString();
            newlabel.Text = "0";
            newlabel.Size = new Size(10, 15);
            newlabel.BackColor = Color.Transaparent;
            newlabel.Location = new Point(e.Location.X, 7);
            panel2.Controls.Add(newlabel);
            counter++;
            break;
        case MouseButtons.Middle:
            foreach(Control c in panel2.Controls)
            {
                if(c is Label)
                {
                    panel2.Controls.Remove(c);
                }
            }
            break;
    }
}
 
Share this answer
 
v3
Comments
Henry Minute 28-Mar-11 21:25pm    
Reciprocated.

Your answer was far more thorough than mine.
Sergey Alexandrovich Kryukov 28-Mar-11 23:33pm    
Great, my 5. Unfortunately, I failed to pay attention at OP's use of local variables. (Serious problem with understanding identity.)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900