שתף באמצעות


VB.Net Dynamically Referencing a Variable or Control

Question

Tuesday, June 2, 2015 12:58 AM

It would take too long to try an explain the actual application I am building.

Lets just say, I have 3 textboxes on my form. I want to set each one of them to a value of the numerical index. This is how I would normally do it.

    txt1.Text = "1"
    txt2.Text = "2"
    txt3.Text = "3"

Now, if I had 100 of these textboxes, I would want to do something more like this.

    For i as Integer = 1 to 3
        txt[i].Text = i
    Next 

Is this possible?

All replies (7)

Tuesday, June 2, 2015 1:12 AM ✅Answered | 1 vote

Hello,

Try this out where the variable boxes returns all TextBox controls on the form then we cycle through them and set the text on each one where we start with zero

Dim boxes As List(Of TextBox) = Me.Controls.OfType(Of TextBox).ToList
For x As Integer = 0 To boxes.Count - 1
    boxes(x).Text = x.ToString
Next

Here we increment x within the for/next to start at one

Dim boxes As List(Of TextBox) = Me.Controls.OfType(Of TextBox).ToList
For x As Integer = 0 To boxes.Count - 1
    boxes(x).Text = CStr(x + 1)
Next

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.


Tuesday, June 2, 2015 5:03 AM ✅Answered

..

Option Strict On

Public Class Form1

    Dim TBox As TextBox

    Dim TBoxList As New List(Of TextBox)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Width = 800
        Me.Height = 460
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))

        Dim x As Integer = 20
        Dim y As Integer = 45

        For i = 1 To 100
            TBox = New TextBox
            With TBox
                .Name = "TextBox" & i.ToString
                .Multiline = False
                .Font = New Font("Book Antiqua", 11)
                .Width = CInt(Me.ClientRectangle.Width / 10) - 24
                .Height = 26
                .BorderStyle = BorderStyle.Fixed3D
                .Location = New Point(x, y)
            End With
            x += CInt(Me.ClientRectangle.Width / 10) - 4
            If Me.ClientRectangle.Width - x < CInt(Me.ClientRectangle.Width / 10) - 4 Then x = 20 : y += 36
            TBoxList.Add(TBox)
            Me.Controls.Add(TBox)
        Next

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i = 0 To TBoxList.Count - 1
            TBoxList(i).Clear()
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For i = 0 To TBoxList.Count - 1
            TBoxList(i).Text = (i + 1).ToString
        Next
    End Sub

End Class

La vida loca


Tuesday, June 2, 2015 7:43 PM ✅Answered

It would take too long to try an explain the actual application I am building.

Lets just say, I have 3 textboxes on my form. I want to set each one of them to a value of the numerical index. This is how I would normally do it.

    txt1.Text = "1"
    txt2.Text = "2"
    txt3.Text = "3"

Now, if I had 100 of these textboxes, I would want to do something more like this.

    For i as Integer = 1 to 3
        txt[i].Text = i
    Next 

Is this possible?

No answers so far are what you were looking for?

You can look at this thread Use Textbox text as variable/constant name or maybe the code below can help. It uses Lists to store information and a Dictionary to store information where a key (string) provided returns a value.

Option Strict On

Public Class Form1

    Dim StringList As New List(Of String)
    Dim IntegerList As New List(Of Integer)
    Dim MultiDict As New Dictionary(Of String, Object)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))

        For i = 1 To 10
            StringList.Add("Number " & i.ToString & ".")
        Next

        For j = 1 To 10
            IntegerList.Add(j)
        Next

        MultiDict.Add("1st", 4 * 32.5)
        MultiDict.Add("2nd", "Second")
        MultiDict.Add("3rd", Me.Location)
        MultiDict.Add("4th", StringList(3))
        MultiDict.Add("5th", "Hola")
        MultiDict.Add("6th", "")
        MultiDict.Add("7th", Timer1.Interval)
        MultiDict.Add("8th", "Animated Gifs")
        MultiDict.Add("9th", PicCounter)
        MultiDict.Add("10th", "Last")

        NumericUpDown1.Minimum = 0
        NumericUpDown1.Maximum = StringList.Count - 1
        NumericUpDown1.Value = 0
        NumericUpDown1.Increment = 1

        NumericUpDown2.Minimum = 0
        NumericUpDown2.Maximum = IntegerList.Count - 1
        NumericUpDown2.Value = 0
        NumericUpDown2.Increment = 1

    End Sub

    Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        TextBox1.Text = StringList(CInt(NumericUpDown1.Value))
    End Sub

    Private Sub NumericUpDown2_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown2.ValueChanged
        TextBox2.Text = IntegerList(CInt(NumericUpDown2.Value)).ToString
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        Try
            TextBox4.Text = MultiDict(TextBox3.Text).ToString
        Catch ex As Exception
        End Try
    End Sub

    Dim PicCounter As Integer = 1

End Class

La vida loca


Tuesday, June 2, 2015 1:37 AM

Now, if I had 100 of these textboxes, I would want to do something more like this.
    For i as Integer = 1 to 3
        txt[i].Text = i
    Next 
Is this possible?

If you have 100 textboxes you are likely creating them at run time.

Each time you create a new text box, add it to a List (Of Textbox).  Then reference the list items using an index, where the index will relate directly to the order in which the textboxes were created.


Tuesday, June 2, 2015 2:29 AM

Hi Brian,
Are there any specifil reasons to use 100 of TextBox?

I'm sorry I don't under stand your intention (spec of your application).
But if I were you, I would use DataGridView, and set loop index to each row(column 0).
If there was a loop in which get each TextBox control, some TextBox for other purpose could not be used in the form.

Best regards,


Tuesday, June 2, 2015 2:41 AM

You should look at the below.

https://msdn.microsoft.com/en-us/magazine/cc947916.aspx

Actually, you an use XML to define your control  by using XSLT to take the XML, transform it into XAML and XML can load the control too all in one shot.

Go talk to the WPF people.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf


Tuesday, June 2, 2015 12:59 PM

Taking your original question at face value - - - you are looking for the DirectCast keyword ? ?

see - -

https://msdn.microsoft.com/en-us/library/vstudio/7k6y2h6x(v=vs.120).aspx

( I am sorry - - I am a bit rusty on it - - - maybe someone else can give you a simple but direct example ? ? )

Edit:   the examples given in the library are often not beginner friendly :-) :-)

Cheers

Top Tip: Toothache? Cut paper towel to 2"square. Smear with olive oil. Sprinkle on Cayenne Pepper. Fold over few times to form small wad. Tuck in between wall of mouth and gum. Leave 1 - 2 hrs. You will thank me!