抓住在文本框上提出休假事件的控件(按钮单击) [英] Catch the control (button click) who raised the leave event on a textbox

查看:144
本文介绍了抓住在文本框上提出休假事件的控件(按钮单击)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextBox 离开事件。

TextBox.Leave sub,我想显示一个确认MsgBox,要求用户保存更改,但如果我显示一个 MsgBox ,则 Button.Click 事件未触发(在$ code之后)TextBox.Leave 事件:请参阅我之前的问题Here

I have a TextBox with Leave event.
In the TextBox.Leave sub, I would like to show a confirmation MsgBox to ask user for saving changes but, if I show a MsgBox, the Button.Click event isn't fired (after TextBox.Leave event: see my previous question Here)

所以我想在(TextBox.Leave Sub)中插入一个代码,用于捕获点击并触发TextBox.Leave事件的按钮的名称(或另一个rif)。

So I thought to insert (in the TextBox.Leave Sub) a code for catching the name (or another rif) of the button that was clicked and fired TextBox.Leave event.

拥有按钮名称,我可以提高点击事件:

Having the button name, I'll be able to raise the click event:

DirectCast(Me.Controls("ButtonName"), Button).PerformClick()


推荐答案

您可以使用方法更好地组织代码而不是调用事件。调用代码的一个问题是,迟早你最终想知道用户或代码是如何调用的。

Rather than invoking events, you can use methods to organize the code better. One of the problems of invoking code that way is that sooner or later you end up wanting to know how it was invoked by the user or code.

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    ' arguably, if the user clicks a save button, 
    ' you dont ask them
    SaveMyStuff()
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    If QueryUserSave("Customer") Then
        SaveMyStuff()
    End If
End Sub
Private Function QueryUserSave(whereMsg As String) As Boolean
    Dim dlgR As DialogResult
    Dim msg = String.Format("Some stuff has changed in {0}. Save it now?", whereMsg)

    dlgR = MessageBox.Show(msg, "Sorry to bother you...", MessageBoxButtons.YesNo)

    Return dlgR = Windows.Forms.DialogResult.Yes
End Function

Private Sub SaveMyStuff()
    '...
End Sub

如果你编写一个单一的东西的方法(其中包括事件),你可以将它们从其他方法链接起来,以重用它们一个又一个。

If you write methods (which includes events) which do one single thing, you can link them together from other methods to reuse them over and over.

这篇关于抓住在文本框上提出休假事件的控件(按钮单击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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