[RESOLVED] Dynamic Textbox MaxLength property
Hello
I am struggling with something quite elementary and simple. I am creating 200 dynamic textboxes with the following code :
Code:
Private Sub AddTxtBoxes()
Dim I As Integer
Dim txtHeight As Integer
Dim txtWidth As Integer
Dim txt As New TextBox
txtHeight = 10
txtWidth = 10
txt.Name = "TextBox1"
txt.Text = ""
txt.Location = New Point(txtWidth, txtHeight)
txt.Multiline = False
txt.MaxLength = 5
Me.Panel1.Controls.Add(txt)
For I = 2 To 200
Dim txt2 As New TextBox
txtHeight += 30
txt2.Name = "TextBox" & I
txt2.Text = ""
txt2.Location = New Point(txtWidth, txtHeight)
txt.Multiline = False
txt.MaxLength = 5
Select Case I
Case 21, 41, 61, 81, 101, 121, 141, 161, 181
txtHeight = 10
txtWidth += 110
txt2.Location = New Point(txtWidth, txtHeight)
End Select
Me.Panel1.Controls.Add(txt2)
Next I
End Sub
Now, I need to restrict the user to type a maximum of 5 characters inside. The MaxLength property is supposed to help me with that, but the user is still allowed to type more than five characters!
can anyone help?
Later on, after I have crossed this bridge, I'll need to allow a minimum of five as well - so that the user will always be able to enter only five characters inside each textbox. Another problem in the future will be to allow only numeric digits. These are product codes, whic are always numeric, and always 5 characters in length
Re: Dynamic Textbox MaxLength property
You're setting the MaxLength of 'txt' over and over when you're adding 'txt2' to the form.
I'd suggest using a MaskedTextBox, but the fact that you're using such an old version of VB means you only have the ActiveX option.
Re: Dynamic Textbox MaxLength property
That solved it. I needed to have txt2.MaxLength = 5 didn't noticd it, thanks.