Introduction
The code presented (valid for VS2010 and higher with Option Strict On
) below can easily be done using conventional means using for-next statements using code pre-Lambda and LINQ, yet I want to show that it can be done with Lambda and LINQ along with structuring the code so that the code is easy to read rather than keep things on one line of code which makes it difficult to come back to at a later date.
To demonstrate the sample code creates a container of newly created button objects. Working from outside to inside, at form level (since we need to access the buttons in an event) an empty array of type Button
is declared. In the form load event this array starts off by creating a range, in this case 0 to 9 which in the end create 10 button objects using Enumerable.Range
. In turn a language extension method "Select" utilizes a function (please note you should Google Option Infer
in regards to typing variable types) that in this case is of type integer and there is no question about this although with Option Infer On
we can leave the type out.
Like any function you should indicate the type returned but we can like the declaration of the parameter going into the function leave the return type off. Inside the function a button is created for each item from Enumerable.Range
, each button is added to the form and then wire up a Click
event that is shared by all the buttons in MyButtons
array. Special note, we need to declare the local variable outside of the LINQ for it to iterate correctly. Overall, note no conventional string concatenation is used, instead String.Concat
is used, which is a better option.
Moving to the shared click event, the first line strips alpha characters and leaves a numeric value which represents the button which invoked this event then for proof the last line changes the button text.
Hopefully this is helpful to one or more developers who find this information.
Background
I spend a good deal of time responding to questions on various forums where believe it or not my code gets mistaken for C# and I believe this comes from a good deal of the developer community not exploring what is possible (or that some samples on the web are not done for day to day type of task a developer may encounter), especially with new ways of coding starting with VS2008 and more so with VS2010 and higher.
Using the code
Create a new form in a Windows form project, add the code below or download the VS2012 prioject.
Option Infer Off
Public Class Demo
Private MyButtons As Button() = Nothing
Private ButtonPreFix As String = "btn"
Private ButtonBaseName As String = "Option"
Private ButtonCount As Int32 = 9
Private Sub Demo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Base As Integer = 0
MyButtons = Enumerable.Range(0, ButtonCount).Select(
Function(Index As Integer) As Button
Dim b As New Button With
{
.Name = String.Concat(ButtonPreFix, ButtonBaseName, Index),
.Text = String.Concat(ButtonBaseName, " ", Index.ToString),
.Location = New Point(100, Base),
.Parent = Me
}
Me.Controls.Add(b)
AddHandler b.Click, AddressOf ForAllButtons_Click
Base += 24
Return b
End Function).ToArray
End Sub
Private Sub ForAllButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Index As Int32 = CInt(CType(sender, Button).Name.Replace(ButtonPreFix & ButtonBaseName, ""))
Select Case Index
Case 0
Case 1
Case 2
Case 3
Case 4
Case 5
Case 6
Case 7
Case 8
End Select
MyButtons(Index).Text = "Clicked"
End Sub
End Class
History
09/03/2013 initial post