Solved

Create Dynamic variables in VB.Net

Posted on 2007-12-06
4
8,300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now