绕过textchange事件 [英] bypass the textchange event

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

问题描述

当txtsearch.text = String.Empty行执行时,如何绕过TextChange事件.这是两次执行消息框,请建议该怎么做

How can I bypass TextChange event when txtsearch.text=String.Empty line executes. This is executting messagebox two time, please suggest what to do

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If rdoName.Checked = True Then
            some codes here
        Else
            MessageBox.Show("Select any Search Criteria First,MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtSearch.Text = String.Empty
        End If
End Sub

推荐答案

这是不良的UI设计.而不是在用户尝试在其中键入消息时显示消息框,而是将文本框的Enabled属性绑定到它依赖的复选框的Checked属性. (要么将真正的数据绑定到中间层业务对象,要么简单地通过在rdoName的CheckedChanged事件中对其进行调整,具体取决于逻辑的复杂性以及它是否会影响UI之外的任何事物.)

对于最初的问题,分配文本将触发TextChanged,因此您需要在TextChanged处理程序代码中进行检查,以检查是否是您真正感兴趣的事件.但这是解决问题的错误方法.
This is bad UI design. Instead of bringing up a message box when the user tries to type in there, bind the Enabled property of the text box to the Checked property of the checkbox it depends on. (Either with genuine data binding to a middle-tier business object, or simply by adjusting it in rdoName''s CheckedChanged event, depending on the complexity of the logic and whether it affects anything outside the UI.)

As for the original question, assigning Text will fire TextChanged, so you would need to put a check in the TextChanged handler code to check if it''s an event you are actually interested in. But this is the wrong way to solve your problem.


Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If rdoName.Checked = True Then
            some codes here
        Else
            MessageBox.Show("Select any Search Criteria First",MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            RemoveHandler txtSearch.TextChanged,AddressOf txtSearch_TextChanged
            txtSearch.Text = String.Empty
            AddHandler txtSearch.TextChanged,AddressOf txtSearch_TextChanged
        End If
End Sub







or

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If txtSearch.Text = String.Empty Then Exit Sub
        If rdoName.Checked = True Then
            some codes here
        Else
            MessageBox.Show("Select any Search Criteria First",MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtSearch.Text = String.Empty
        End If
End Sub


这篇关于绕过textchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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