如何在vb 6.0中限制文本框的值

时间:2013-03-23 18:07:57

标签: vb6 textbox integer

我正在做一个标记更新数据库系统。我需要限制每个文本框的值小于100,当它超过100时,或者当它不是数字时,会弹出一个消息框,并且在用户更改错误之前不会保存数据。我该怎么做?

2 个答案:

答案 0 :(得分:2)

我同意Hiren Pandya,但我认为我也会加入自己的拍摄。

请注意,将字符串转换为数值并非易事,但VB6中的ValCInt,CDBl等函数都可以为您提供接近您想要的行为。 (其中一些链接适用于VB.Net,但仍然有价值)。当您自己验证用户输入时,您需要确保考虑数字分组,正/负,小数分隔符等。大多数情况下,内置函数足够好。

Private Sub Text1_Change()
    On Error GoTo Err_Handler
    Dim text As String
    text = Text1.text
    If IsNumeric(text) = True Then

        'If you only want integers...
        Dim value As Integer
        value = Val(text)
            If value <= 100 And value > 0 Then
            'The value is good so whatever stuff you need to do
            'And then leave the procedure
            Exit Sub
        End If
    End If

    'Let everything else fall through here...
Err_Handler:
    MsgBox "Invalid input."
    'Other stuff to prevent saving
End Sub

答案 1 :(得分:1)

在文本框的属性中,将MaxLength设置为2。

如果您想要一条消息,请在文本框中更改事件,您可以执行...

If Len(txtBox.Text)>2 then msgbox...

然后在消息框中添加您的消息。

如果你需要,我可以详细介绍。有些事情如下......

Private Sub Text1_Change()
If Len(Text1) > 6 Then
    Text1 = " "    
    MsgBox "Not more than six"    
    Text1.SetFocus    
End If
End Sub