We help IT Professionals succeed at work.

Auto incremenet variable

yo_daz_uk
yo_daz_uk asked
on
Medium Priority
296 Views
Last Modified: 2010-04-23
Hi,

I have a problem with some code.  I am trying to programtaicaly add an un determined amount of text boxes, to a form.

The i want to be able to get the data back from these text boxes.

      For i = 0 To 5
            Dim test1 As New System.Windows.Forms.TextBox
            test1.Location = New System.Drawing.Point(x, 3)
            test1.Size = New System.Drawing.Size(195, 20)
            test1.Name = "test" & i
            Me.Panel1.Controls.Add(test1)
            x = x + 200
        Next

This code creates the 5 text boxes as required, but declares them all as test1, so i cannot refernece them to find out what user values have been put into them.  Also, even if i could reference them how would i then access the data from say a different button, as at compile time the other code would not know the text boxes are there and produce an error?

Daz
Comment
Watch Question

Commented:
I had the same problem.

I believe it is the way you are referenceing your items.

See, you actually say test1.name, well that says it right there that the object is test1.

Read through my post able labels. It is the same thing:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21767713.html

Commented:
Actually,

I looked it over and your code works.

Where did you dim i and x?

Try this:

Public Class Form1
    Dim i As Integer
    Dim x As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i = 0 To 5
            Dim test1 As New System.Windows.Forms.TextBox
            test1.Location = New System.Drawing.Point(3, x)
            test1.Size = New System.Drawing.Size(195, 20)
            test1.Name = "test" & i
            test1.Text = "test" & i
            Me.Controls.Add(test1)
            x = x + 25
        Next

    End Sub
End Class
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi yo_daz_uk;

Here is some code doing what you want. I used your code in the Sub Button1_Click event. In your statement, "This code creates the 5 text boxes as required", This will contain 6 TextBoxes because you start at 0.

    Private TextBoxes As New Hashtable

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer
        Dim x As Integer = 10
        Dim y As Integer = 10

        For i = 0 To 5
            Dim test1 As New System.Windows.Forms.TextBox
            test1.Location = New System.Drawing.Point(x, 3)
            test1.Size = New System.Drawing.Size(195, 20)
            test1.Name = "test" & i
            TextBoxes.Add(test1.Name, test1)
            Me.Panel1.Controls.Add(test1)
            x = x + 200
        Next

    End Sub


    Private Sub AccessTextBoxes_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles AccessTextBoxes.Click

        For i As Integer = 0 To 5
            Dim tb As TextBox = CType(TextBoxes("test" & i.ToString()), TextBox)
            MessageBox.Show(tb.Name & " = " & tb.Text)
        Next

    End Sub

Fernando
Fernando: You forgot in the loop:
AddHandler test1.Click, addressof AccessTextBoxes_Click

Commented:
I played with it a bit more and made it a sub so you can add as many as you need:

'Dim these at main/head
    Dim i As Integer
    Dim x As Integer = 0
    Dim textbox As System.Windows.Forms.TextBox

'Add this new sub, this will create the boxes
Sub AddTextBox(ByVal numberofboxes As Integer)
        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(3, x)
            textbox.Size = New System.Drawing.Point(195, 20)
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            Me.Controls.Add(textbox)
            x = x + 25
        Next
    End Sub

'Add this call to a button or form load to populate the boxes:
'This will add 5 boxes (starting with 0 and ending with 4)
AddTextBox(5)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Uhm... recall my statement :D
This is much simpler.

Create a new usercontrol, and then edit the second line.

Public Class Mytextbox
Inherits System.Windows.Forms.TextBox

-- now its a textbox, so add the property

Private idx as Integer
Public Property Index() as Integer
 Get
  Return idx
 End Get
 Set(byvale Value as Integer)
  idx = Value
 End Set
End Property

now back to your code
        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Index = i
next i

oops
Dim textbox As New MyTextBox
more oops
           Dim textbox(i) As New System.Windows.Forms.TextBox
            textbox(i).Index = i

Author

Commented:
Hi got it working as CDCOP said although having trouble accessing the data, and tried to modify sernadosoto code to access it, it doesnt like the textboxes part in button 2 is says it is not declared ...

Public Class Form1
    Dim i As Integer
    Dim x As Integer = 0
    Dim textbox As System.Windows.Forms.TextBox
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddTextBox(5)
    End Sub
    Sub AddTextBox(ByVal numberofboxes As Integer)
        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(3, x)
            textbox.Size = New System.Drawing.Point(195, 20)
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            Me.Controls.Add(textbox)
            x = x + 25
        Next
    End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click
        For i As Integer = 0 To 5
            Dim tb As TextBox = CType(textboxes("txt_" & i.ToString()), TextBox)
            MessageBox.Show(tb.Name & " = " & tb.Text)
        Next
    End Sub
End Class

Author

Commented:
sorry missed the hashtable bit, got it compiling.  When button 2 is clicked to show the data it produces an error.

It says NullReferneceException was unhandled

on the message box line.

Commented:
Just use:
MsgBox("txt_" & i)
You should add :
if not textboxes("txt_" & I.ToString()) is nothing then
 ctype... etc.

Commented:
Yea you can't call it since you generate them dynamically.

So you will have to programatically call them since you can't reference them directly.

I tried it using (my same code above plus):
For i = 0 To 5 - 1
            MsgBox("txt_" & i)
Next
LOL@CDCOP

I can do the same without even creating a textbox :D

For i = 0 To 5 - 1
            MsgBox("txt_" & i)
Next

That doesn't even have any relation to the textbox anymore :D
So sure, it will work :D

Author

Commented:
Ok first i tried this .....

        For i As Integer = 0 To 5
            If Not TextBoxes("txt_" & i.ToString()) Is Nothing Then

                Dim tb As TextBox = CType(TextBoxes("txt_" & i.ToString()), TextBox)
                MessageBox.Show(tb.Name & " = " & tb.Text)

            End If
       Next

Then i tried this .....

For i As Integer = 0 To 5
     MsgBox(TextBoxes("txt_" & i.ToString()))
  Next

It just had 6 empty message boxes so then i tried this ............

        For i As Integer = 0 To 5
            Dim tb As TextBox = CType(TextBoxes("txt_" & i.ToString()), TextBox)

            Try
                MsgBox(tb.Text)
            Catch ex As Exception
                MsgBox("An Error")
            End Try

        Next

Hust had 6 An Error messages appear.

Author

Commented:
Sorry forgot to put what the first one did ....

After clicking the button it did nothing.
If you are using the code Fernando provided to add to the hashtable the name is like:
    test1.Name = "test" & i

so change into :
     Dim tb As TextBox = CType(TextBoxes("test" & i), TextBox)
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Hi yo_daz_uk;

I made corrections to the code you posted on Date: 04/10/2006 11:14AM PDT.

    Dim i As Integer
    Dim x As Integer = 0
    Dim textbox As System.Windows.Forms.TextBox
    ' Store the reference of the TextBox here for later use
    Dim tbHashTable As New Hashtable

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        AddTextBox(5)

    End Sub

    Sub AddTextBox(ByVal numberofboxes As Integer)

        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(3, x)
            ' You must create a Point then give that to Drawing size.
            textbox.Size = New System.Drawing.Size(New System.Drawing.Point(195, 20))
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            tbHashTable.Add(textbox.Name, textbox)
            Me.Controls.Add(textbox)
            textbox.Show()
            x = x + 25
        Next

    End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click

        ' tbHashTable.Count is the number of TextBoxes placed in the HashTable
        For i As Integer = 0 To tbHashTable.Count - 1
            Dim tb As TextBox = CType(tbHashTable("txt_" & i.ToString()), TextBox)
            MessageBox.Show(tb.Name & " = " & tb.Text)
        Next

    End Sub

Fernando

Author

Commented:
Yes im using Fernados to access it but i was using CDCOP's way to access it.  I have changed the code to the panel, but that shouldnt change anything as it is crating the text boxes with the txt_ i with i being the number in the text.

All the code is below .....

Public Class Form1
    Dim i As Integer
    Dim x As Integer = 6
    Dim textbox As System.Windows.Forms.TextBox
    Private TextBoxes As New Hashtable
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddTextBox(5)
    End Sub
    Sub AddTextBox(ByVal numberofboxes As Integer)
        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(x, 3)
            textbox.Size = New System.Drawing.Point(195, 20)
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            Me.Panel1.Controls.Add(textbox)
            x = x + 200
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For i As Integer = 0 To 5
            If Not TextBoxes("txt_" & i) Is Nothing Then

                Dim tb As TextBox = CType(TextBoxes("txt_" & i.ToString()), TextBox)
                MessageBox.Show(tb.Name & " = " & tb.Text)

            End If
        Next
  end sub
end class

Commented:
@PookyMaster

Yea so I see. I went a little too fast.

I'll play with it, I see what you mean yo_daz_uk.

Author

Commented:
Thats working thanks fernado

Commented:
The problem is that if the value of the text box is changed, it is not reflected in the message box. Does you code show  changes Fernando?
It does, it shows tb.Text

Author

Commented:
Completly messed that up.  Fernado was supposed to be the accepted answer, with CDCOP and  Pocky Master help.

Just to clearup Fernado code does show if a text box is done.  Only problem now is i need to do the same thing but for combo boxes :-)

I was surprised already of getting the points... credits go to fernando

Author

Commented:
Apologies.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi yo_daz_uk;

The code you used from CDCOP :

Sub AddTextBox(ByVal numberofboxes As Integer)
        For i = 0 To numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(3, x)
            textbox.Size = New System.Drawing.Point(195, 20)
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            tbHashTable.Add(textbox.Name, textbox)
            Me.Controls.Add(textbox)
            x = x + 25
        Next
End Sub

This code should only be executed once because every time you go into it, it will names the TextBoxes 0 - numberofboxes - 1. Which means that when you add them to the HashTable you will get a Unhandlered Exception Duplicate name. You may want to try to modify the code lik this.

Dim tbHashTable As New Hashtable
Dim idx As Integer = 0

Sub AddTextBox(ByVal numberofboxes As Integer)
        For i = idx To idx + numberofboxes - 1
            Dim textbox As New System.Windows.Forms.TextBox
            textbox.Location = New System.Drawing.Point(3, x)
            textbox.Size = New System.Drawing.Point(195, 20)
            textbox.Name = "txt_" & i
            textbox.Text = "txt_" & i
            Me.Controls.Add(textbox)
            x = x + 25
        Next

        idx += numberofboxes
End Sub

Fernando

Author

Commented:
Thanks,

daz
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.