简体   繁体   English

我所有的textBox都有一个事件。 (VC#Windows窗体应用程序)

[英]One Event for all my textBoxes. (VC# Windows Form Application)

I've searched a lot on this site and found similar questions but none of the answers could help me. 我在该网站上进行了很多搜索,发现了类似的问题,但是没有答案可以帮助我。 So I'm going to ask myself. 所以我要问自己。 I have this code that doesn't work when I want to put all my textBoxes to the same event, here it is: 当我想将所有文本框都置于同一个事件中时,此代码不起作用,这里是:

private void OnMouseUp(object sender, MouseEventArgs e)
{
  TextBox textBox = (TextBox)sender;
  textBox.SelectionLength = 0;
}

I made this code by looking at other answers on this site to similar questions. 我通过查看本网站上类似问题的其他答案来编写此代码。 I also made this one by combining answers I found here on this site: 我还结合了我在此站点上找到的答案来完成了这一任务:

private void OnMouseUp(object sender, MouseEventArgs e)
{
  foreach (Control x in this.Controls)
  {
    if (x is TextBox)
     {
      ((TextBox)x).SelectionLength = 0;
     }
  }
}

Which also doesn't work... Can someone please tell me the easiest way to give the same event to all your textBoxes? 哪一种也不起作用...有人可以告诉我将相同事件发送到所有textBox的最简单方法吗?

在表单类中添加textbox1.OnMouseUp+= OnMouseUp

Assuming you want every Textbox on your form to have this event: 假设您希望表单上的每个 Textbox都发生此事件:

private void AssignEvent(Control.ControlCollection controls)
{   
    foreach (Control c in controls)
    {
        if (c is Textbox)
            c.MouseUp += OnMouseUp;

        if (c.HasChildren)
            AssignEvent(c.Controls);
    }
}

Call this in your Form_Load like so: 像这样在Form_Load调用它:

AssignEvent(this.Controls);

This method just loops over every control on your form, and every child control (ie Form has a Groupbox , Groupbox has a Textbox ). 此方法仅循环遍历窗体上的每个控件以及每个子控件(即Form有一个GroupboxGroupbox有一个Textbox )。 If it's a Textbox , it assigns the event. 如果是Textbox ,则分配事件。 If it's not, it'll simply move on to the next one. 如果不是,它将继续前进到下一个。

I fixed it by doing this: 我通过执行此操作来修复它:

private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control x in this.Controls)
        {
            if (x is TextBox)
            {
                ((TextBox)x).MouseUp += SLMethod;
            }
        }
    }

    static void SLMethod(object sender, MouseEventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        textBox.SelectionLength = 0;
    }

What this code is doing is making me unable to select the code within my box with my mouse. 该代码正在执行的操作使我无法使用鼠标在框中选择代码。 Which is what I want. 这就是我想要的。 I did not really understand your answers but thank you anyways! 我不太了解您的答案,但是还是谢谢您!

EDIT: If you want to make allt your textbox "unselectable" using a standard .Net way : 编辑:如果您想使用标准的.Net方式使文本框“不可选择”:

foreach (Control x in this.Controls)
    {
        if (x is TextBox)
        {
            ((TextBox)x).Enabled = false;
        }
    }

If it's only to get them unselectable by mouse, then keep the controls attached to your event. 如果仅是使鼠标无法选择它们,则使控件与事件保持联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM