简体   繁体   English

C#winform清除鼠标事件arg

[英]C# winform Clear mouse event arg

// Is it possible to create 1 mouse even for 2 text box? //是否可以为2个文本框创建1个鼠标?

Lets say i have 2 text boxes, (TxtBox1, TxtBox2) 可以说我有2个文本框,(TxtBox1,TxtBox2)

what i want is 1 Clear function that will clear only the text button which was clicked, without the need to create 2 clear functions for each one : TxtBox1.Clear(); 我想要的是1清除函数,将仅清除单击的文本按钮,而无需为每个按钮创建2清除函数:TxtBox1.Clear(); TxtBox1.Clear(); TxtBox1.Clear();

Here is another explanation of what i thought C# would support : 这是我认为C#支持的另一种解释:

    private void Clear(object sender, MouseEventArgs e)
    {
        this.Clear();
              }

The sender is the UI element that was clicked so the following should work: sender是被单击的UI元素,因此以下各项应适用:

private void TextBoxOnClick(object sender, MouseEventArgs e)
{
    var theTextBox = sender as TextBox;
    if (theTextBox != null)
    {
        theTextBox.Text = string.Empty;
    }
}

The as and check for null is just defensive programming. as和check为null只是防御性编程。 If you're sure that this will only ever be called from a TextBox then you can do a direct cast. 如果确定只能从TextBox调用它,则可以进行直接强制转换。

Then you need to add this to the click event handler for each text box: 然后,需要将其添加到每个文本框的click事件处理程序中:

TxtBox1.OnClick += TextBoxOnClick;
TxtBox2.OnClick += TextBoxOnClick;

etc. for all your text boxes. 等所有您的文本框。

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

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