开发者

How to set a textbox input length

开发者 https://www.devze.com 2023-02-01 04:58 出处:网络
Using VB6 using Textbox in my form. I want to limit the Input in the textbox, so maximum value should be 6.

Using VB6

using Textbox in my form.

I want to limit the Input in the textbox, so maximum value should be 6.

User should enter up to six c开发者_开发知识库hars otherwise it should show error message

Button1.click

if Length(textbox1.text) > 6 then
  enter only six chars
else if Length(textbox1.text) < 6 then
  enter up to six chars

How do I make the code for the above condition.


Textboxes in VB6 have a MaxLength property. Set it to 6 and then the user can't enter more than 6.


Well, you're getting close!

First ... use the Len function, not Length, which doesn't exist.

Second ... you can use the function MsgBox to display an error message.

Putting it all together:

If Len(TextBox1.Text) < 6 Then
   MsgBox "Too short!"
Else If Len(TextBox1.Text) > 6 Then
   MsgBox "Too long!"
End If


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


Or you could set the MaxLength property to the desired value.

As for the warning, you could place it in the keyUp/Down or keypress events: If Len(Text1.Text) = Text1.MaxLength Then MsgBox ("WARNING!"), vbExclamation


' working in vb.net

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              TextBox1.MaxLength = 6
    End Sub

'in vb6

Private Sub Text1_Change()
    If Len(Text1.text) > 6 Then
        text1.enabled=false  
    End If
End Sub
Private Sub Text1_DoubleClick
   text1.enable=true
   text1.text=""
end sub


Or you can use this code

If Len(Me.txtusername.Text) And Len(Me.txtpassword.Text)< 5 Then
          Call MsgBox("Login failed !, password character must at list 5 and above")

Else
          Call MsgBox("Error occurred ! Password did not match!")
End If
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号