Loading controls dinamically

Status
Not open for further replies.

bdani82

Member
Joined
Jun 7, 2004
Messages
22
Programming Experience
3-5
Hi!

I have a problem: i have to generate dinamically 5 button.

Private WithEvents btnTest As Button

Private Sub Form1_Load(...) Handles MyBase.Load
Dim i As Integer

For i = 1 To 5
btnTest = New Button
btnTest.Name = "btnTest"
btnTest.Tag = i
btnTest.Top = iTop
iTop = iTop + 50
btnTest.Parent = Me
Next
End Sub

Private
Sub btnTest_Click(...) Handles btnTest.Click
MsgBox("Hello world!")
End Sub

Why only the last button created shows then message box when I Click on it?

Thaks!!
 
When your loop is completed you have 5 controls with the same name and only one event sub to handle the click event.
To have it work without using addhandler, you should give a different name to each button and have on event procedure for each, this is why I am giving you this code that does it with less work :

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Test_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iTop [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 25

[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bSize [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Size(70, 25)[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 5[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] BtnTest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button
BtnTest.Name = "BtnTest" & i.ToString
BtnTest.Text = "BtnTest" & i.ToString
BtnTest.Top = iTop
BtnTest.Left = 25
BtnTest.Size = bSize
iTop = iTop + 40
BtnTest.Parent = [/SIZE][SIZE=2][COLOR=#0000ff]Me
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][SIZE=2] BtnTest.Click, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] BtnTest_Click
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BtnTest_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[INDENT]MsgBox("You have clicked on a buttton")

[/INDENT][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Thank you Bob Langlade. But I have a problem using your code: how to identifier the clicked button? I think to use the tag property, but the result of this code is always a messagebox with 5. Can you help me?


Private WithEvents btnTest As Button

Private Sub btnTest_Click(...) Handles btnTest.Click
MsgBox(btnTest.Tag)
End Sub

Private Sub Form1_Load(...) Handles MyBase.Load
Dim i As Integer

For i = 1 To 5
btnTest =
New Button
AddHandler btnTest.Click, AddressOf btnTest_Click
btnTest.Name = "btnTest"
btnTest.Tag = i
btnTest.Top = iTop
iTop = iTop + 50
btnTest.Parent =
Me
Next
End Sub
 
Hi bdani82 please try this new sub ;) :

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BtnTest_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)

[/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] sender.name[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] "BtnTest1"
MsgBox("Implements your first action")
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] "BtnTest2"
MsgBox("Implements your second action")
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] "BtnTest3"
MsgBox("Implements your third action")
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] "BtnTest4"
MsgBox("Implements your fourth action")
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] "BtnTest5"
MsgBox("Implements your fith action")
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Thank you very much but... problem number two: the code I sent you is only a test. In my project i don't know the exact number of controls to load, then I can't use the Select Case!

Thank you very much... and excuse me if my English is not very well, I'm an Italian boy!
 
Status
Not open for further replies.
Back
Top