Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Auto incremenet variable

Posted on 2006-04-10
29
Medium Priority
?
238 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
0
Comment
Question by:yo_daz_uk
  • 9
  • 7
  • 7
  • +2
29 Comments
 
LVL 9

Expert Comment

by:CDCOP
ID: 16418498
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
 
LVL 9

Expert Comment

by:CDCOP
ID: 16418616
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16418741
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:PockyMaster
ID: 16418769
Fernando: You forgot in the loop:
AddHandler test1.Click, addressof AccessTextBoxes_Click

0
 
LVL 9

Assisted Solution

by:CDCOP
CDCOP earned 160 total points
ID: 16418778
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16418780
Uhm... recall my statement :D
0
 
LVL 4

Expert Comment

by:broadbent
ID: 16419604
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16419615
oops
Dim textbox As New MyTextBox
0
 
LVL 4

Expert Comment

by:broadbent
ID: 16419625
more oops
           Dim textbox(i) As New System.Windows.Forms.TextBox
            textbox(i).Index = i
0
 

Author Comment

by:yo_daz_uk
ID: 16419808
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
 

Author Comment

by:yo_daz_uk
ID: 16419860
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
 
LVL 9

Expert Comment

by:CDCOP
ID: 16419874
Just use:
MsgBox("txt_" & i)
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 80 total points
ID: 16419877
You should add :
if not textboxes("txt_" & I.ToString()) is nothing then
 ctype... etc.

0
 
LVL 9

Expert Comment

by:CDCOP
ID: 16419896
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16419918
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
 

Author Comment

by:yo_daz_uk
ID: 16420002
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
 

Author Comment

by:yo_daz_uk
ID: 16420011
Sorry forgot to put what the first one did ....

After clicking the button it did nothing.
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16420044
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
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 160 total points
ID: 16420111
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
 

Author Comment

by:yo_daz_uk
ID: 16420113
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
 
LVL 9

Expert Comment

by:CDCOP
ID: 16420148
@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
 

Author Comment

by:yo_daz_uk
ID: 16420150
Thats working thanks fernado
0
 
LVL 9

Expert Comment

by:CDCOP
ID: 16420171
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16420178
It does, it shows tb.Text
0
 

Author Comment

by:yo_daz_uk
ID: 16420219
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16420252
I was surprised already of getting the points... credits go to fernando
0
 

Author Comment

by:yo_daz_uk
ID: 16420292
Apologies.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16420397
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
 

Author Comment

by:yo_daz_uk
ID: 16420704
Thanks,

daz
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question