Results 1 to 3 of 3

Thread: [RESOLVED] Dynamic Textbox MaxLength property

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Dynamic Textbox MaxLength property

    That solved it. I needed to have txt2.MaxLength = 5 didn't noticd it, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width