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

Creating a Control Array in VB.Net

Author: DevASP
Download Source Code : 433_Control

In this article I will explain you the way to create Button Control Array with Event Handler. Source code is given with this article.

Button control array with event handling. This code will show you how to create control arrays in Visual Basic.Net and have each array member respond to events by using the Address Of operator. Though it looks like a calculator, it has no calculation functions. The purpose of this project is not to show how to create a calculator. The purpose is that how we can create Control Array.


Write the lines of code given below in forms load event.

Sets application default settings and properties


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


'Create buttons



'Size and position text filed

With txtNumDisplay


  .Width() = 71

  .Location() = New System.Drawing.Point(btnTestNumButtons(7).Left(), (btnTestNumButtons(7).Top() - txtNumDisplay.Height()) - 5)

  .TextAlign() = HorizontalAlignment.Right


End With


End Sub


After that write the routine below This method creates a button array and assigns event handlers to the objects.This method is related to Method variable declarations, Resize the dynamic array, Create button object and assign event handling, Create new object instance, Object properties, Set text caption , Set the button size, Set the button flat, Set the background color, Attach handler reference,Add control array to the form superclass, Position controls on the form.


Private Sub CreateButtonArray()

Dim shtIndex As Short

ReDim btnTestNumButtons(10)


For shtIndex = 0 To 9


 btnTestNumButtons(shtIndex) = New System.Windows.Forms.Button()


 With btnTestNumButtons(shtIndex)


  .Text() = shtIndex.ToString()   

  .Size() = New Size(40, 40)    

  .FlatStyle() = FlatStyle.Popup 

  .BackColor() = System.Drawing.Color.Blue      

  AddHandler .Click, AddressOf Me.btnTestNumButtons_Click               


 End With







End Sub


This method shows the stack Order.This method positions the newly created buttons on the form.Position the buttons by columns.


Private Sub ButtonStackOrder()


 'First column indices 0, 1, 4, 7

 btnTestNumButtons(7).Location = New System.Drawing.Point(10, 50)


 btnTestNumButtons(4).Location = New System.Drawing.Point(10, (btnTestNumButtons(7).Top() + (btnTestNumButtons(7).Height()) + 4))


 btnTestNumButtons(1).Location = New System.Drawing.Point(10, (btnTestNumButtons(4).Top() + (btnTestNumButtons(4).Height()) + 4))


 btnTestNumButtons(0).Location = New System.Drawing.Point(10, (btnTestNumButtons(1).Top() + (btnTestNumButtons(1).Height()) + 4))


 'Second column indices 2, 5, 8

 btnTestNumButtons(8).Location = New System.Drawing.Point((btnTestNumButtons(7).Left() + btnTestNumButtons(7).Width()) + 5, 50)


 btnTestNumButtons(5).Location = New System.Drawing.Point((btnTestNumButtons(4).Left() + btnTestNumButtons(4).Width()) + 5, (btnTestNumButtons(8).Top() + (btnTestNumButtons(8).Height()) + 5))


 btnTestNumButtons(2).Location = New System.Drawing.Point((btnTestNumButtons(1).Left() + btnTestNumButtons(1).Width()) + 5, (btnTestNumButtons(5).Top() + (btnTestNumButtons(5).Height()) + 5))


 'Third column indices 3, 6, 9


 btnTestNumButtons(9).Location = New System.Drawing.Point((btnTestNumButtons(8).Left() + btnTestNumButtons(8).Width()) + 5, 50)


 btnTestNumButtons(6).Location = New System.Drawing.Point((btnTestNumButtons(5).Left() + btnTestNumButtons(5).Width()) + 5, (btnTestNumButtons(9).Top() + (btnTestNumButtons(9).Height()) + 5))


 btnTestNumButtons(3).Location = New System.Drawing.Point((btnTestNumButtons(2).Left() + btnTestNumButtons(2).Width()) + 5, (btnTestNumButtons(6).Top() + (btnTestNumButtons(6).Height()) + 5))


End Sub


This method handles button object clicks.Display a message box with the text value of the button.


Private Sub btnTestNumButtons_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Click




End Sub

Source code is given woth that article for your understanding.

Article Comments
i want to source code about vb.NET

Posted on 3/20/2007 8:32:28 AM by prabhavathi

i want to source code about vb.NET

Posted on 3/20/2007 8:32:32 AM by prabhavathi

please send me Best Practics VB Or C# Sample(2005)
very very tanx

Posted on 3/23/2007 8:45:28 AM by Javad

Thanks for your source.This is really usefull sample for me

Posted on 12/29/2007 4:29:55 AM by Ahmet

you are given proper example

Posted on 2/7/2008 2:03:27 AM by vimal


Posted on 4/3/2008 5:48:06 AM by Avi

Sir i want know about solution for this error
optSommer(i).enabled = aBool
this is regarding control array pls help me how to achive this by using control arrays

Posted on 4/8/2008 9:36:06 AM by srinivasa reddy

can you help to create a calculator in Visual basic. net incling the following bottons Memory ( clear; + ; - ); square root and percentage...

Posted on 4/10/2008 1:03:25 PM by Lucas

actually i want to set dynamic button control on bottom position or any where
how could i do this please set me proper codind

Posted on 8/23/2008 7:34:08 AM by suvarna

I've defined the no of buttons across and down using ButtonNo ( Dim ButtonNo As New ButtonArraySize).

Private Sub CreateButtonArray()
ReDim Buttons(ButtonNo.Xcount * ButtonNo.Ycount)
Dim iPosition As New System.Drawing.Point

For iRowNo As Integer = 1 To ButtonNo.Ycount
For iColNo As Integer = 1 To ButtonNo.Xcount
' calc array no
Dim iArrayNo As Integer = (iRowNo - 1) * ButtonNo.Xcount + iColNo

' create button
Buttons(iArrayNo) = New System.Windows.Forms.Button()

' calc location of button
iPosition.Y = iGapOutside + (iRowNo - 1) * iButtonSize.Height + iGapInside * (iRowNo - 1)
iPosition.X = iGapOutside + (iColNo - 1) * iButtonSize.Width + iGapInside * (iColNo - 1)

With Buttons(iArrayNo)
' attributes
.Text() = (iArrayNo).ToString()
.Size() = iButtonSize
.Name() = "Button" + Format(iRowNo, "00") + "-" + Format(iColNo, "00")

' handler
AddHandler .Click, AddressOf Me.Button_Click

' position
.Location = iPosition
End With

End Sub

Public Class ButtonArraySize
Private iXcount As Integer
Public Property Xcount() As Integer
Return iXcount
End Get
Set(ByVal value As Integer)
iXcount = value
End Set
End Property

Private iYcount As Integer
Public Property Ycount() As Integer
Return iYcount
End Get
Set(ByVal value As Integer)
iYcount = value
End Set
End Property
End Class

Posted on 11/20/2008 6:15:03 AM by richard b

This article is really clear, it is fine I have already tested, it is perfect, Thank you

Posted on 5/25/2010 6:17:25 PM by Jose Antonio

Add Article Comment:
Name :
Email Address :
Comments :
<< Displaying Bitmap Images in Menu Control

Disclaimer - Privacy
© 2002-2017