简体   繁体   English

是否有一种简单的方法可以在C#中向所有表单对象添加事件

[英]Is there an easy way to add an event to all form objects in C#

I have a form with many labels and text boxes. 我有一个包含许多标签和文本框的表单。 I'd like to have the title highlighted red if any of the fields are modified. 如果修改了任何字段,我想将标题突出显示为红色。 Is there an easy way to do this or do you need to add the event callback to each form object individually? 有没有一种简单的方法可以执行此操作,还是需要将事件回调单独添加到每个表单对象? Thanks! 谢谢!

Off the top of my head you could do something like this in the form load event to add the events... 在我的头顶你可以在表单加载事件中做这样的事情来添加事件......

foreach (var control in this.Controls)
{
    if (control is Label)
    {
        ((Label)control).TextChanged += Controls_TextChanged;
    }
    else if (control is TextBox)
    {
        ((TextBox)control).TextChanged += Controls_TextChanged;
    }
}

When you're looking at the events associated with each control, you notice that you can 'drop down' a list of all events coded in the form. 当您查看与每个控件关联的事件时,您会注意到您可以“下拉”表单中编码的所有事件的列表。 If you double click on a 'Text Changed' event, for example, it will be controlname_textchanged(...) . 例如,如果双击“Text Changed”事件,它将是controlname_textchanged(...) However, if you have a generic handler for all, then you could call it textchangedevent(...) . 但是,如果你有一个通用的处理程序,那么你可以称之为textchangedevent(...) The generic event handler will need to have the sender and event object associated with that event in the parameters. 通用事件处理程序需要在参数中将该事件与发送方和事件对象相关联。

If you're doing this in a user control and have to update the main form, then you will bubble this up through a public event eventobject youreventname , and bind that event on the main form. 如果您在用户控件中执行此操作并且必须更新主窗体,那么您将通过public event eventobject youreventname将其public event eventobject youreventname ,并将该事件绑定在主窗体上。

Something like this should work: 这样的事情应该有效:

foreach (System.Windows.Forms.Control cont in this.Controls)
            cont.Validating += new System.Windows.Forms.ValidationEventHandler(this.Control_Validating_Method)

Then you could test for the control type in the event code and compare the value with the original value. 然后,您可以在事件代码中测试控件类型,并将值与原始值进行比较。

Yes you can easily add controls to the same event. 是的,您可以轻松地将控件添加到同一事件中。 This example puts the 'Labels' and 'Textboxes' in the same event. 此示例将“标签”和“文本框”放在同一事件中。

private void Form1_Load(object sender, EventArgs e)
    {
 foreach (Control x in this.Controls)
            {
                if(x is Label)
                    ((Label)x).MouseHover+=new EventHandler(AllLabels_HoverEvent);
                    else if(x is TextBox)
                    ((TextBox)x).MouseHover+=new EventHandler(AllTextboxes_HoverEvent);
            }
}

void AllLabels_HoverEvent(object sender, EventArgs e)
        {
              Label label = (Label)sender;
           // label.dowhateveryouwant...
        }
        void AllTextboxes_HoverEvent(object sender, EventArgs e)
        {
            Textbox textbox = (Textbox)sender;
           // textbox.dowhateveryouwant...
        }

If you need any more clarification please comment below and I will add on to my answer or modify it to suite any particular needs you have. 如果您需要进一步澄清,请在下面发表评论,我将添加我的答案或修改它以满足您的任何特定需求。 Be careful though if you have controls nested in other containers they won't be in this.Controls . 但要小心,如果你有控件嵌套在其他容器中,它们将不在this.Controls

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

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