C#删除控件..................................... [英] C# Removing Controls......................................

查看:234
本文介绍了C#删除控件.....................................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

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



由于某种原因,当我单击它时,它并没有删除我的标签,因此什么也没发生.我希望能够一次删除1个整个面板和所有面板中的标签以及1个标签.



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?

推荐答案

您的删除代码是正确的.您在其他地方遇到了问题.您说当我单击它时".您不会显示您处理了任何鼠标单击事件.首先,您需要确保将委托添加到click事件的代码确实在运行.如果是这样,则需要确保事件处理程序已被调用.执行此操作时,需要确保事件句柄实际调用Control.Remove.

您是否使用了调试器?将断点放在所有三个点中,以查看缺少的内容.如果仍然无法确定该使谁工作,请显示代码的相关片段:单击处理程序以及将委托添加到事件的位置;描述未达到的断点.


看完源代码后:
它并不表示方法panel2_MouseClick不是处理程序,它只是某种方法.它在哪里被称为处理程序?可能这是一个问题.如果您这样写,则此方法可以成为处理程序:

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.


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:

panel2.Click += panel2_MouseClick;



如果没有这样的东西,那就成问题了.我还告诉过您:您需要设置3个断点并报告代码是否到达那里.你做了吗?您没有回答问题.如果您不打算使用调试器,请再说一遍编程.

顺便说一句,此语法是自动生成代码中仍在使用的旧的丑陋语法.如果正确操作,它应该是这样的样子:



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:

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



—SA


您无法删除标签,因为您的Remove()方法引用了在事件处理程序中创建的标签 >面板上的内容.

您的代码-带注释的
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
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:

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?


查看您在SAKryukov答案中发布的代码后,我可以看到您正在实例化 newlabel newlabel2 每次单击鼠标.问题在于,这些实例化将不会指向添加到面板中的相同控件.相反,它们每次都是全新的标签控件.

选项1-您需要做的是指定每个标签的Name属性,然后使用Name:
删除它们
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:
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;
    }
}




选项2-您将需要在事件处理程序外部实例化标签,以便在删除它们时,它们仍将指向已添加的标签(假设您仅添加了其中一个标签).您无需指定Name属性:




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:

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;
    }
}



选项3-遍历控件并删除所有已添加的Label控件:



Option 3 - Loop through the controls and remove all Label controls that have been added:

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;
    }
}


这篇关于C#删除控件.....................................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆