Question How to make Auto Textbox or any idea ?

SaintLouise

New member
Joined
Oct 14, 2008
Messages
3
Programming Experience
Beginner
Hello Guys.. I do really need your help. Do you guys know how to make Auto Textbox. I mean, The textbox is created based on how many input it has. For example if there is 3 Input then The textbox should be 3 too but it is possible to add new textbox if the input are 4.
At the moment I tried to make more than 5 textbox but the input is always random, it can be more then 5 textbox or less than 5 textbox.
Another example to make it Clear..
I have Medicine Label. In Medicine part, I only put 5 textbox while data input is random. the medicine used can be more than 5 or less than 5. How if the input 6 ?
I want to know is it possible if the textbox created depend on the input. If there is 3 input then it should have 3 textbox and so on...

I am really appreciate your help. Sorry for My English.
Thank you..
 
Hello SaintLouise,

I have done something like this in the past. Just wait on a bit and I will have a search for some code that will allow you to do this. Back soon.

Tarablue
 
Hi SaintLouise,

Try this bit of code. You will need to play around with some of the values like M and N but it should get you started.

VB.NET:
Public Class Form1

    Public lastTxt As TextBox
    Public Txt As Integer
    Public M, N, A, TextNumberLeft, TextNumberRight As Integer
    Public TextBox(18) As TextBox
    Public TextLeft As Integer = 6
    Public TextRight As Integer = 7

    Private Sub TextBox2_enter(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles TextBox2.Enter

        TextLeft = 6
        TextRight = 7

        M = Val(TextBox1.Text)
        N = M + 1
        A = 26

        If N > 3 Then

            For i As Integer = 1 To M - 2
                Dim tb1 As New TextBox
                TextNumberLeft = TextLeft + i
                With tb1
                    .Name = "TextBox" & TextNumberLeft
                    .TextAlign = HorizontalAlignment.Right
                    .TabStop = True
                    .TabIndex = TextNumberLeft
                    .Size = New Size(50, 20)
                    .Location = New Point(12, (133 + (i * A)))
                End With
                Me.Controls.Add(tb1)

                Dim tb2 As New TextBox
                TextLeft = TextLeft + 1
                AddHandler tb1.Enter, AddressOf TextBoxes_Enter

                TextNumberRight = TextRight + i
                With tb2
                    .Name = "TextBox" & TextNumberRight
                    .TextAlign = HorizontalAlignment.Right
                    .TabStop = True
                    .TabIndex = TextNumberRight
                    .Size = New Size(50, 20)
                    .Location = New Point(68, (133 + (i * A)))
                End With
                Me.Controls.Add(tb2)

                TextRight = TextRight + 1
                AddHandler tb2.Enter, AddressOf TextBoxes_Enter

            Next

        End If

        Me.TextBox2.Focus()
        Me.TextBox2.SelectAll()

    End Sub

End Class


The program I was designing had 2 extra TextBoxes added when N was greater than 3. If you have problems I'll see what I can come up with for you. Please be patient though because I have a lot of extra work coming up this week and it may be a few days before I could reply.

Best Rgds,
Tarablue
 
Last edited by a moderator:
I have error with the TextBoxes_Enter.
Method 'Friend Dim WithEvents TextBoxes_Enter as System.Windows.Forms.TextBox ' doesn't have the same signature as delegate ....
What should i do ?
 
Hi SaintLouise,

I would need to look at your code to see how the error comes about. My code works on having a form with 7 TextBoxes already there and more are added in pairs.

Best Rgds,
Tarablue
 
Back
Top