VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 3254|回复: 3

关于vb.net中的Textbox问题求助

[复制链接]
发表于 2019-2-16 08:22:29 | 显示全部楼层 |阅读模式
10金钱
以下是关于Text两种用法,下面VB6的写法想写回vb.net用法怎么写,没搞懂请知道的同仁指点一二,谢谢

如果在多个文本中应用为空验证 应该加一个开关 以免产生多个同时验证
Dim tState As Boolean '开关
Private Sub Text1_GotFocus()'获取焦点时
tState = True
End Sub
Private Sub Text2_GotFocus()'获取焦点时
tState = True
End Sub
Private Sub Text1_LostFocus()'失去焦点时
If Text1.Text = "" And tState Then
MsgBox "该项不能为空"
Text1.SetFocus
End If
tState = False
End Sub
Private Sub Text2_LostFocus()
If Text2.Text = "" And tState Then
MsgBox "该项不能为空"
Text2.SetFocus
End If
tState = False
End Sub

另一种是vb6怎么写回vb.net的用法
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Text1 = "" Then MsgBox "不能为空!": Text1.SetFocus
End If
End Sub
Private Sub Text1_LostFocus()
If Text1 = "" Then MsgBox "不能为空!": Text1.SetFocus
End Sub

发表于 2020-7-25 21:44:08 | 显示全部楼层
Public Class Form1
    Dim tState As Boolean '开关
    Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
        tState = True
    End Sub
    Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
        tState = True
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        If TextBox1.Text = "" And tState Then
            MsgBox("该项不能为空")
            TextBox1.Focus()
        End If
        tState = False
    End Sub
    Private Sub TextBox2_LostFocus(sender As Object, e As EventArgs) Handles TextBox2.LostFocus
        If TextBox2.Text = "" And tState Then
            MsgBox("该项不能为空")
            TextBox2.Focus()
        End If
        tState = False
    End Sub
End Class
'====================================
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If (Asc(e.KeyChar)) = 13 Then
            If TextBox1.Text = "" Then MsgBox("不能为空!") : TextBox1.Focus()
        End If
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        If TextBox1.Text = "" Then MsgBox("不能为空!") : TextBox1.Focus()
    End Sub
回复

使用道具 举报

发表于 2020-7-25 21:46:53 | 显示全部楼层
Public Class Form1
    Dim tState As Boolean '开关
    Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
        tState = True
    End Sub
    Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
        tState = True
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        If TextBox1.Text = "" And tState Then
            MsgBox("该项不能为空")
            TextBox1.Focus()
        End If
        tState = False
    End Sub
    Private Sub TextBox2_LostFocus(sender As Object, e As EventArgs) Handles TextBox2.LostFocus
        If TextBox2.Text = "" And tState Then
            MsgBox("该项不能为空")
            TextBox2.Focus()
        End If
        tState = False
    End Sub
End Class
'====================================
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If (Asc(e.KeyChar)) = 13 Then
            If TextBox1.Text = "" Then MsgBox("不能为空!") : TextBox1.Focus()
        End If
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        If TextBox1.Text = "" Then MsgBox("不能为空!") : TextBox1.Focus()
    End Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-5-18 21:29

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表