Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Create a Dynamic Array of Controls in Visual Basic.Net(VB.Net)

Author: DevASP
Download Source Code : 317_array_of_controls.zip

This is a simple article to tell that how can you create dynamic array of controls by taking size of array on run time and use these controls dynamically.

This is very Simple in VS.net. you can create dynamic array of any type of controls by taking size of the array on the run time by user. Create a vb.net project now create a label on form1 having text No of Labels. Then create a NumericUpDown control having name nudtotal with initial value 10. Then create a button having text create and named button1.

 

  • Now write code on form load event this is for the declaration of the array of controls.

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

   Dim temp As Label()

 

End Sub

 

  • Now write code button click event.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

Me.AutoScroll = True

 

Dim temp As Label() = New Label(NUDTOTAL.Value) {}

Dim CNT As Integer = 0

Dim X As Integer = 30

Dim Y As Integer = 300

 

 While CNT < NUDTOTAL.Value

 

  temp(CNT) = New Label

  temp(CNT).Location = New System.Drawing.Point(X, Y)

  temp(CNT).Size = New System.Drawing.Size(150, 20)

  temp(CNT).TextAlign = ContentAlignment.MiddleCenter

  temp(CNT).ForeColor = System.Drawing.Color.Black

  temp(CNT).BackColor = System.Drawing.Color.LightSalmon

  temp(CNT).Text = "DYNAMIC LABEL NO" & CNT + 1

 

  Me.Controls.Add(temp(CNT))

  CNT += 1

  Y = Y + 60

 

 End While

End Sub

 

  • In these statements first statement

 

        Me.AutoScroll = True

 

  • This is for form scroll bar option.

 

Dim temp As Label() = New Label(NUDTOTAL.Value) {}

 

  • Next statement is to create array of labels according to user request.

 

Dim CNT As Integer = 0

      Dim X As Integer = 30

      Dim Y As Integer = 300

 

  • This is just variable declaration.

 

While CNT < NUDTOTAL.Value

 

 temp(CNT) = New Label

 temp(CNT).Location = New System.Drawing.Point(X, Y)

 temp(CNT).Size = New System.Drawing.Size(150, 20)

 temp(CNT).TextAlign = ContentAlignment.MiddleCenter

 temp(CNT).ForeColor = System.Drawing.Color.Black

 temp(CNT).BackColor = System.Drawing.Color.LightSalmon

 temp(CNT).Text = "DYNAMIC LABEL NO" & CNT + 1

 Me.Controls.Add(temp(CNT))

 CNT += 1

 Y = Y + 60

 

End While

 

Now this is important code it assigns memory to the labels one by one set its location on the form set its size on the form its text fore color,backcolor and text also and in the last add this controls one by one to the controls of the form.

 

NOTE:

 

Now you can set its location, size, and all the properties according to your requirements.

 

Article Comments
Plz tell me abt how can we use labelcontrol array with fixed number.

Posted on 12/25/2006 1:55:57 AM by neeta hajare

I have tried to do this with a Picturebox element, while the actions of the program indicate the items are present on the form, they don't appear to be visible.

Posted on 3/14/2007 9:23:18 AM by Lonefox

Thank you very much for this code. Fantastic knowing people out there to help pepole like me.

Could you kindly tell me, how to introduce these labels on a groupbox control which is on the form?

Posted on 4/14/2008 4:54:04 AM by Behzad

to add labels to group box do the following:

groupbox1.Controls.Add(temp(CNT))

Posted on 12/21/2009 1:51:32 AM by ashish

Hi, this code is really usefull to me..But I want to create link label dinamicallly. I have created link label using this code. Could you please explain how to add Click event ??

Posted on 12/29/2009 3:59:42 AM by Parul

Hi Parul,

This method will solve your problem

Yourlinkbutton.Attributes.Add("onClick", "YourFunction();")

Thanks.

Posted on 12/29/2009 5:08:23 AM by zunnair

I can do this without any problems howver I need to take it a step futher. I am creating some comboboxs. What I need to know now is how to add code to the seleteded.Index changed event for each of these boxes. The code will be the same for each.

Posted on 5/5/2010 9:25:11 AM by Mike

how can i declare a control array of command buttons...... if same procedure is followed then how do i write event handler

Posted on 5/12/2010 10:51:55 AM by anurag

what is the meaning of "Label(NUDTOTAL.Value) {}"

Posted on 7/21/2010 3:17:49 AM by murali

Hi murali,

NUDTOTAL.value means Nud stands for Numeric updown control, and in this statement we are creating and array of label which size is equal to value in Numeric Updown Control.

Thanks...

Posted on 7/22/2010 3:19:15 AM by Jack

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Check that how many colors your System supports

Disclaimer - Privacy
© 2002-2017 DevASP.net