简体   繁体   English

vb.net如何退出验证返回焦点到空文本框?

[英]vb.net how to exit validation return focus to the empty textbox?

I am working in vb.net and I want to exit a validation and return the focus to the textbox with the error. 我在vb.net工作,我想退出验证并将焦点返回到带有错误的文本框。

it raises the error but as soon as I click ok, it continues with the rest of the program. 它会引发错误,但是一旦我单击确定,它就会继续执行程序的其余部分。 Any idea's? 有任何想法吗? thank you. 谢谢。

Should I also mention that this is in a class. 我是否还应该提到这是在课堂上。

   'number of tickets property
Public Property NumberOfTickets() As String
    Get
        Return NumTickets
    End Get
    Set(value As String)
        If (String.IsNullOrEmpty(value)) Then
            MessageBox.Show("Please Enter Number of Tickets")
            Exit Property
        Else
            NumTickets = value
        End If
    End Set
End Property

You could use Validating event of your Textbox, and in that, say: 您可以使用文本框的验证事件,并在其中说:

e.Cancel = True

This will prevent user from leaving the TextBox. 这将阻止用户离开TextBox。 However, this would also prevent user from closing the form, and performing any other action. 但是,这也会阻止用户关闭表单并执行任何其他操作。 Restricting users like that is considered bad practice by some. 限制这样的用户被一些人认为是不好的做法。

Please consider using ErrorProvider instead and just notify user about the errors. 请考虑使用ErrorProvider而只是通知用户有关错误的信息。

Always do your best to let them finish what they were doing. 始终尽力让他们完成他们正在做的事情。

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

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