Auto incremenet variable

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
yo_daz_ukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CDCOPCommented:
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
0
CDCOPCommented:
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
0
Fernando SotoRetiredCommented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

0
CDCOPCommented:
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)
0
PockyMasterCommented:
Uhm... recall my statement :D
0
broadbentCommented:
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

0
broadbentCommented:
oops
Dim textbox As New MyTextBox
0
broadbentCommented:
more oops
           Dim textbox(i) As New System.Windows.Forms.TextBox
            textbox(i).Index = i
0
yo_daz_ukAuthor 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
0
yo_daz_ukAuthor 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.
0
CDCOPCommented:
Just use:
MsgBox("txt_" & i)
0
PockyMasterCommented:
You should add :
if not textboxes("txt_" & I.ToString()) is nothing then
 ctype... etc.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CDCOPCommented:
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
0
PockyMasterCommented:
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
0
yo_daz_ukAuthor 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.
0
yo_daz_ukAuthor Commented:
Sorry forgot to put what the first one did ....

After clicking the button it did nothing.
0
PockyMasterCommented:
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)
0
Fernando SotoRetiredCommented:
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
0
yo_daz_ukAuthor 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
0
CDCOPCommented:
@PookyMaster

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

I'll play with it, I see what you mean yo_daz_uk.
0
yo_daz_ukAuthor Commented:
Thats working thanks fernado
0
CDCOPCommented:
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?
0
PockyMasterCommented:
It does, it shows tb.Text
0
yo_daz_ukAuthor 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 :-)

0
PockyMasterCommented:
I was surprised already of getting the points... credits go to fernando
0
yo_daz_ukAuthor Commented:
Apologies.
0
Fernando SotoRetiredCommented:
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
0
yo_daz_ukAuthor Commented:
Thanks,

daz
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.