Button Array
' I am looking for a better way to create an array of buttons as was
' the case with VB6
' The following snippet is the method I currently use in VB2008
' ** ======================= **
' ** ======================= **
' ** Author M O'Rourke **
' ** December 2009 **
' ** ======================= **
' ** ======================= **
Public Class frmButtons
Inherits System.Windows.Forms.Form
Private btnEvents() As System.Windows.Forms.Button
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents lblStatus As System.Windows.Forms.Label
Friend WithEvents grpMenu As System.Windows.Forms.GroupBox
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblStatus = New System.Windows.Forms.Label
Me.grpMenu = New System.Windows.Forms.GroupBox
Me.SuspendLayout()
'
'lblStatus
'
Me.lblStatus.BackColor = System.Drawing.SystemColors.Info
Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblStatus.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblStatus.Location = New System.Drawing.Point(169, 79)
Me.lblStatus.Name = "lblStatus"
Me.lblStatus.Size = New System.Drawing.Size(233, 18)
Me.lblStatus.TabIndex = 1
'
'grpMenu
'
Me.grpMenu.Location = New System.Drawing.Point(12, 136)
Me.grpMenu.Name = "grpMenu"
Me.grpMenu.Size = New System.Drawing.Size(556, 87)
Me.grpMenu.TabIndex = 2
Me.grpMenu.TabStop = False
'
'frmButtons
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(585, 312)
Me.Controls.Add(Me.grpMenu)
Me.Controls.Add(Me.lblStatus)
Me.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Name = "frmButtons"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Buttons"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub frmButtons_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.Width = 1000
.Left = 5
End With
CreateButtonArray() 'buttons generated on the fly
SetButtonText() 'adds text to the individual buttons
SetButtons("1100111111")
End Sub
#Region "Button Creation, Event Handling, and Positioning Code"
' *****************************
' ** Create the Button Array **
' *****************************
Private Sub CreateButtonArray()
Dim iButtonIndex As Integer
ReDim btnEvents(10)
For iButtonIndex = 0 To 9
btnEvents(iButtonIndex) = New System.Windows.Forms.Button
With btnEvents(iButtonIndex)
.Text() = iButtonIndex.ToString() & "X" 'Set text caption
.Size() = New Size(60, 30) 'Set the button size
.FlatStyle() = FlatStyle.Standard ' = FlatStyle.Popup 'Set the button flat style
.BackColor() = System.Drawing.Color.Aqua 'Set the background color
AddHandler .Click, AddressOf Me.btnEvents_Click 'Attach handler reference
End With
Next
' Add the buttons to the groupBox/Frame
Me.grpMenu.Controls.AddRange(btnEvents)
ButtonStackOrder()
End Sub
' *************************
' ** Set The Button Text **
' *************************
Private Sub SetButtonText()
btnEvents(0).Text = "Add"
btnEvents(1).Text = "Edit"
btnEvents(2).Text = "Undo" 'Undo Edit
btnEvents(3).Text = "Save"
btnEvents(4).Text = "Find" '
btnEvents(5).Text = "Misc1"
btnEvents(6).Text = "Misc 2"
btnEvents(7).Text = "Misc 3"
btnEvents(8).Text = "Misc 4"
btnEvents(9).Text = "Close"
End Sub
' *************************
' ** Line Up the buttons **
' *************************
Private Sub ButtonStackOrder()
btnEvents(0).Location = New System.Drawing.Point(8, 10) 'Add (btnEvents(1).Top() + (btnEvents(1).Height()) + 4))
btnEvents(1).Location = New System.Drawing.Point((btnEvents(0).Left() + btnEvents(0).Width()) + 5, 10) 'Edit
btnEvents(2).Location = New System.Drawing.Point((btnEvents(1).Left() + btnEvents(1).Width()) + 5, 10) ' Undo
btnEvents(3).Location = New System.Drawing.Point((btnEvents(2).Left() + btnEvents(2).Width()) + 5, 10)
btnEvents(4).Location = New System.Drawing.Point((btnEvents(3).Left() + btnEvents(3).Width()) + 5, 10)
btnEvents(5).Location = New System.Drawing.Point((btnEvents(4).Left() + btnEvents(4).Width()) + 5, 10)
btnEvents(6).Location = New System.Drawing.Point((btnEvents(5).Left() + btnEvents(5).Width()) + 5, 10)
btnEvents(7).Location = New System.Drawing.Point((btnEvents(6).Left() + btnEvents(6).Width()) + 5, 10)
btnEvents(8).Location = New System.Drawing.Point((btnEvents(7).Left() + btnEvents(7).Width()) + 5, 10)
btnEvents(9).Location = New System.Drawing.Point((btnEvents(8).Left() + btnEvents(8).Width()) + 5, 10) 'Close
' Set up the groupbox based on size of buttons
With grpMenu
.Top = Me.Height - 80
.Width = btnEvents(0).Width * (10 + 1)
.Height = btnEvents(0).Height + 15
End With
End Sub
' ********************************************
' ** Button clicked so carry out the event **
' ********************************************
Private Sub btnEvents_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Click
Dim cString As String = sender.Text()
'Dim myObject As Object = sender
Select Case cString
Case "Add"
lblStatus.Text = "Add Button Clicked"
SetButtons("0011000000") 'only leave save and undo enabled
Case "Edit"
lblStatus.Text = "Edit Button Clicked"
SetButtons("0011000000") 'only leave save and undo enabled
Case "Undo"
lblStatus.Text = "Undo Button Clicked"
SetButtons("1100111111") 'back to default buttons enabled
Case "Save"
lblStatus.Text = "Save Button Clicked"
SetButtons("1100111111") 'Afer 'Save then again default buttons enabled
Case "Find"
lblStatus.Text = "Find Button Clicked"
SetButtons("1100111111") 'Setting dependent on for what programmer uses this button
Case "Close"
lblStatus.Text = "Close Button Clicked"
Me.Close()
Case "Misc1" 'Etc. etc. '
lblStatus.Text = "Misc_1 Button Clicked"
Case Else
SetButtons("1100111111") 'case
lblStatus.Text = cString & " Case Else Called"
End Select
End Sub
' **************************************************************************************
' Enable/Disable the individual buttons depending on the 1/0 setting in sButtonString **
' **************************************************************************************
Private Sub SetButtons(ByVal sButtonString As String)
Dim iIndex As Integer, iButtonLength As Integer
' sButtonString = "110010000"
iButtonLength = Len(sButtonString)
For iIndex = 1 To iButtonLength
If Mid(sButtonString, iIndex, 1) = "1" Then
btnEvents(iIndex - 1).Enabled = True
Else 'else this is a 0
btnEvents(iIndex - 1).Enabled = False
End If
Next
End Sub
#End Region
End Class
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.