在其他控件中使用控件事件 [英] Using Events of Control in Other Controls

查看:87
本文介绍了在其他控件中使用控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在mousedown,keydown,TextChanged和....中定义方法来自定义我的表单中的TextBox控件。

i需要其他形式的其他相同控件执行相同的行为和

为所有其他控件在事件中写入此代码

这是怎么做的?

I Customize a TextBox Control in my form by define methods in mousedown ,keydown,TextChanged and ....
i need other same controls in other forms act same behavior and
write this codes in events once for all other controls
How do this?

推荐答案

1.如果你想重复使用它/ similar TextBox 在其他表单中的控件应该定义并实现基类中的常见行为,例如 BasicTextBox 并从 TextBox 派生,然后在表单/控件中使用此类或子类(从中派生的类)代替系统 TextBox 对象。



2.通过使用类似的步骤,上面解释的这个想法也可以扩展到其他控件。



3.因为你的 BasicTextBox 类将扩展系统 TextBox 控件,它将继承其所有方法和属性。为了在任何情况下添加自定义行为,您必须在 BasicTextBox 类中覆盖它,如下一个示例所示:

1.If you want to reuse the same/similar TextBox controls in others forms you should define and implement the common behaviors into a base class, named for example BasicTextBox and derived from TextBox, then use this class or children of it (classes derived from it) in your forms/controls in place of the system TextBox objects.

2.This idea, explained above, could be extended also for other controls, by using similar steps.

3.Because your BasicTextBox class will extend the system TextBox control, it will inherit all its methods and properties. In order to add your custom behavior in any event you have to overwrite it in your BasicTextBox class like in the next example:
protected override void OnClick(EventArgs e)
{
    //Add your custom behavior here:
    //this.SelectionStart = 0;
    //this.SelectionLength = this.Text.Lenght;
    this.SelectAll();
    //
    // You must invoke the existing functionality from parent!
    base.OnClick(e);
}


您可以创建自定义文本框控件并将其用于项目中的所有位置

请查看以下文章:编写自定义控件:一步一步 [ ^ ]
you can create custom textbox control and use it all the places in your project
check below article :Writing your Custom Control: step by step[^]


如果在Visual Studio中在设计时选择多个控件,并使用上下文菜单或F4键调出属性浏览器,则选择事件:



您将看到所有选定控件共享的事件列表。



您可以加倍 - 单击其中一个事件,Visual Studio将为您创建一个EventHandler的存根,其中包含所需的签名(对象发送者,SomeKindOfEventArgs e)。



或者,你可以在代码中编写EventHandler,然后选择Co ntrols,然后打开属性浏览器,然后选择你编写的EventHandler绑定到选定的控件。



你可以使用不同容器控件中的控件执行此操作,例如一个控件在表单表面,另一个面板内的另一个面板内的控件等等。



或者,在您的代码中,您可以指定多个控件来使用相同的EventHandler :如果我想在每次单击表单中的面板中的任何按钮时记录操作,该怎么办?而且,让我们假设我有五个按钮,'panel1中的button1~button5:
If you select several Controls at design-time in Visual Studio, and use the context-menu, or the F4 key, to bring up the Property Browser, then select 'Events:

You'll see the list of Events that all selected Controls share.

You can double-click one of those Events, and Visual Studio will create a "stub" of an EventHandler for you with the required signature (object sender, SomeKindOfEventArgs e).

Or, you could write the EventHandler in code, then select the Controls, then open the Property Browser, then choose the EventHandler you wrote to bind to the selected Controls.

You do this with Controls inside different Container Controls, like one Control on the Form "surface," another Control inside a Panel inside another Panel, etc.

Or, in your code, you can assign multiple Controls to use the same EventHandler: what if I want to record an action every time any Button in a Panel in a Form is clicked ? And, let's imagine I have five Buttons, button1~button5 in 'panel1:
private void Form1_Load(object sender, EventArgs e)
{
    foreach (Button aButton in panel1.Controls.OfType<Button>())
    {
        aButton.MouseClick += AButtonOnMouseClick;
    }
}

private void AButtonOnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
    Button theButton = sender as Button;

    // do something here with every Button ?
    // ????

    // do something here with specific Button(s) 
    switch (theButton.Name)
    {
        case "button1":
            MessageBox.Show("button 1");
            break;
        case "button2":
        case "button3":
            MessageBox.Show("button 2, or 3");
            break;
        default:
            MessageBox.Show("any button not 1,2,3");
            break;
    }
}

如果我想为两个按钮使用相同的EventHandler,并且'panel1中的两个标签:

What if I wanted to use the same EventHandler for both the Buttons, and two Labels in 'panel1:

private void Form1_Load(object sender, EventArgs e)
{

    foreach (Button aButton in panel1.Controls.OfType<Button>())
    {
        aButton.MouseClick += SomeControlOnMouseClick;
    }

    foreach (Label aLabel in panel1.Controls.OfType<Label>())
    {
        aLabel.MouseClick += SomeControlOnMouseClick;
    }
}

private void SomeControlOnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
    if (sender is Label) handleLabel(sender as Label);
    if (sender is Button) handleButton(sender as Button);
}

private void handleLabel(Label theLabel)
{
    MessageBox.Show("clicked on Label: " + theLabel.Name);
}

private void handleButton(Button theButton)
{
    // do something here with every Button ?
    // ????

    // do something here with specific Button(s) 
    switch (theButton.Name)
    {
        case "button1":
            MessageBox.Show("button 1");
            break;
        case "button2":
        case "button3":
            MessageBox.Show("button 2, or 3");
            break;
        default:
            MessageBox.Show("any button not 1,2,3");
            break;
    }
}

请注意,您可以通过多种方式构建此类代码:我的首选方法是为每个我想要共享的控件类型定义一个EventHandler EventHandler:我觉得创建了更易于维护的代码,并且避免必须测试控件类型以决定执行哪些代码。

Note there are any number of ways you could structure this kind of code: my preference would be to define one EventHandler for each Control Type that I wanted to share an EventHandler: I feel that created more maintainable code, as well as avoiding having to test for the 'Type of the Control in order to decide which code to execute.


这篇关于在其他控件中使用控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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