How to perform different actions with one handler
-
Hi Please tell me in some lines of code.How can I perform differnt actions for controls that I have created at runtime.e.g. display the name of control. Code snippet is Dim x, y As Integer For i As Integer = 1 To 10 y += 25 btn = New Button btn.Location = New Point(x, y + 25) btn.Size = New Size(70, 25) btn.Name = "Button" & i.ToString btn.Text = "Button" & i.ToString Controls.Add(btn) AddHandler btn.Click, AddressOf btn_Click Next i Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Display the control name that was clicked End Sub reman
-
Hi Please tell me in some lines of code.How can I perform differnt actions for controls that I have created at runtime.e.g. display the name of control. Code snippet is Dim x, y As Integer For i As Integer = 1 To 10 y += 25 btn = New Button btn.Location = New Point(x, y + 25) btn.Size = New Size(70, 25) btn.Name = "Button" & i.ToString btn.Text = "Button" & i.ToString Controls.Add(btn) AddHandler btn.Click, AddressOf btn_Click Next i Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Display the control name that was clicked End Sub reman
sender is a Button
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi Please tell me in some lines of code.How can I perform differnt actions for controls that I have created at runtime.e.g. display the name of control. Code snippet is Dim x, y As Integer For i As Integer = 1 To 10 y += 25 btn = New Button btn.Location = New Point(x, y + 25) btn.Size = New Size(70, 25) btn.Name = "Button" & i.ToString btn.Text = "Button" & i.ToString Controls.Add(btn) AddHandler btn.Click, AddressOf btn_Click Next i Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Display the control name that was clicked End Sub reman
Cast
sender
to a Button.Dim btn As Button = DirectCast(sender, Button)
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007