【问题标题】:What is wrong with the following leave event?以下请假事件有什么问题?
【发布时间】:2013-09-17 17:22:10
【问题描述】:
Private Sub txtAddress_Leave (ByVal sender As Object, ByVal e As 
        System.EventArgs) Handles txtAddress.Leave
  If Len (txtAddress.Text) >= 0 Then
         MsgBox ("Need to enter address", MsgBoxStyle.OKOnly, _
         txtAddress.Focus()
  End if
End Sub

任何帮助将不胜感激。

【问题讨论】:

  • 您的问题是什么?
  • 什么意思?你遇到了什么问题?
  • 我不认为有什么问题。这是一个很好的活动,你可以从它的行为中看出它是在一个充满关怀和爱的环境中长大的。还是它没有给您预期的结果,在这种情况下,您希望它做什么而它没有做什么?

标签: vb.net


【解决方案1】:

我假设你想检查用户是否输入了文本,那么你应该改变这个

if Len (txtAddress.Text) >= 0 Then

if Len (txtAddress.Text) = 0 Then

但是,您最好使用 .NET 方法:

If String.IsNullOrEmpty(txtAddress.Text) Then ' or String.IsNullOrWhiteSpace

您还应该使用Select 而不是Focus

txtAddress.Select()

textbox.Focus() not working in C#

【讨论】:

  • OP 的焦点部分也发生在 MsgBox 调用中。
  • @LarsTech:你什么意思?我已经提到了Focus 问题。
  • 查看焦点线上方的下划线。 OP 缺少右括号,并且 MsgBox 的标题变为“True”或“False”,因为 Focus() 正在将参数传递给 MsgBox。应该是编译器错误。
【解决方案2】:
If Len(txtAddress.Text) >= 0 Then

应该是

If txtAddress.Text.Trim = "" Then

这样,如果有人决定只输入“空格”作为地址,它就会考虑在内。现在,如果他们确实输入了某些内容,您的代码将导致弹出消息框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多