Solved

Create Dynamic variables in VB.Net

Posted on 2007-12-06
4
8,312 Views
Last Modified: 2013-11-26
Hello,
   I would like to be able to change this:
        Dim lbl1 as label
        Dim lbl2 as label
        Dim lbl3 as label

        lbl1.text = "Yes"
        lbl2.text = "Yes"
        lbl3.text = "Yes"

to a dynamic sub where I pass in 1, 2 or 3 and it create it in a loop. Any ideas?
0
Comment
Question by:mcdermon
4 Comments
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 50 total points
ID: 20418894
Here is an example how to create control array and assign event to it

Private ButtonArray(9) As Button

Public Sub AddButtons()
    For x As Integer = 0 To 9
        ButtonArray(x) = New Button
        With ButtonArray(x)
            .Tag = x
            .Text = "Button " & x
            .Size = New Size(60,20)
            .Location = New Point(20, (x * 20) + 4)
            .Parent = Me
            .Visible = True
            AddHandler .Click, AddressOf ButtonHandler
        End With
    Next        
End Sub

'Then, create a ButtonHandler to handle the button clicks

Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
    MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
End Sub

For the label you do not need handler, but I showed it for the general knowledge
0
 
LVL 4

Accepted Solution

by:
J1H1W1 earned 100 total points
ID: 20419082
Did you want something like this:
Dim WithEvents C As Control
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Static I As Integer
	I += 1
	C = GetNewLabel(I)
End Sub
Function GetNewLabel(ByVal LabelNumber As Integer) As Label
	GetNewLabel = New Label
	With GetNewLabel
		.Name = "Lbl" & LabelNumber.ToString
		.Parent = Me
		.Left = 0
		.Top = (LabelNumber) * (GetNewLabel.Height)
		.Text = "Yes"
	End With
End Function

Open in new window

0
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 50 total points
ID: 20419666
Private Labels As New List(Of Label)
Sub CreateLabels(ByVal LabelCount As Integer)
    Labels.Clear()
    Dim l As Label
    For i As Integer to LabelCount
        l = New Label
        l.Name = "Label" & i
        Labels.Add(l)
    Next i
End Sub
0
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 50 total points
ID: 20420522
Here's an alternative method, that you can call as follows:

        For Each lbl As Label In GetLabels(3)
            'Process...
            Debug.WriteLine(lbl.Name)

        Next


'And the function:
    Private Function GetLabels(ByVal HowMany As Int16) As Label()
        Dim newlbl As Label = Nothing
        Dim lbl() As Label = Nothing

        For i As Int16 = 0 To (HowMany - 1)
            ReDim Preserve lbl(i)

            'Configure label
            newlbl = New Label()
            newlbl.Name = "lbl" & i
            newlbl.Text = "Yes"
            newlbl.Visible = True

            lbl(i) = newlbl

        Next

        Return lbl

    End Function

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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