Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net e、 Cancel激发,但在发生单击事件时也激发其他代码_Vb.net_Events_Focus_Validation - Fatal编程技术网

Vb.net e、 Cancel激发,但在发生单击事件时也激发其他代码

Vb.net e、 Cancel激发,但在发生单击事件时也激发其他代码,vb.net,events,focus,validation,Vb.net,Events,Focus,Validation,表单有许多按钮。其中一个按钮包含更新TableAdapterManager的代码。许多TextBox控件中的一个在验证事件处理程序中包含代码。有确保美国电话号码格式正确的代码 如果用户从文本框中弹出标签,则验证代码工作正常,如果电话号码格式不正确且焦点位于有问题的文本框中,则向用户显示消息 如果用户单击的按钮包含更新TableAdapterManager的代码,则会触发验证代码,但不会将重点放在有问题的文本框上,按钮单击处理程序中的代码也会触发 我想阻止按钮代码触发 以下是文本框验证事件的代码:

表单有许多按钮。其中一个按钮包含更新TableAdapterManager的代码。许多TextBox控件中的一个在验证事件处理程序中包含代码。有确保美国电话号码格式正确的代码

如果用户从文本框中弹出标签,则验证代码工作正常,如果电话号码格式不正确且焦点位于有问题的文本框中,则向用户显示消息

如果用户单击的按钮包含更新TableAdapterManager的代码,则会触发验证代码,但不会将重点放在有问题的文本框上,按钮单击处理程序中的代码也会触发

我想阻止按钮代码触发

以下是文本框验证事件的代码:

Private Sub TextBoxPrimaryPhone_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBoxPrimaryPhone.Validating

    ' Make sure the phone is formatted correctly.
    '--------------------------------------------
    If PhoneFormat(TextBoxPrimaryPhone.Text) = "Fix Phone Number" Then

        ' Alert the user.
        '----------------
        MessageBox.Show("Please enter a 7 or 10 digit phone number.", _
                        "Entry Error", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)

        e.Cancel = True
    Else

        ' Format according to the length of the phone number entered by the user.
        '------------------------------------------------------------------------
        TextBoxPrimaryPhone.Text = PhoneFormat(TextBoxPrimaryPhone.Text)
    End If
End Sub

我需要包括哪些额外的编码才能使焦点保持在文本框上?

调用e.Cancel=True后,执行此操作以设置有问题文本框的焦点:

TextBoxPrimaryPhone.Focus()
在按钮单击事件处理程序中,可以执行以下操作:

    TextBoxPrimaryPhone_Validating(sender, New System.ComponentModel.CancelEventArgs)

    If Not TextBoxPrimaryPhone.Focused Then

        'Do Work

    End If