Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim WithEvents button1 As Button
    Dim WithEvents button2 As Button
    Dim WithEvents button3 As Button
    Friend WithEvents button As Button



    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        button1 = New Button
        button2 = New Button
        button3 = New Button

        AddHandler button1.Click, AddressOf Button4_Click
        AddHandler button2.Click, AddressOf Button4_Click
        AddHandler button3.Click, AddressOf Button4_Click

        button1.Size = New Size(100, 50)
        button1.Location = New Point(28, 186)
        button1.Text = "Button1"

        button2.Size = New Size(100, 50)
        button2.Location = New Point(165, 186)
        button2.Text = "Button2"

        button3.Size = New Size(100, 50)
        button3.Location = New Point(314, 186)
        button3.Text = "Button3"

        Controls.Add(button1)
        Controls.Add(button2)
        Controls.Add(button3)

    End Sub

    Private Sub button_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If sender Is button1 Then
            TextBox1.Text = "You clocked button1"
        ElseIf sender Is button2 Then
            TextBox1.Text = "You have clicked button2"
        ElseIf sender Is button3 Then
            TextBox1.Text = "you have clicked button3"
        End If

    End Sub

End Class
Posted
Updated 24-Jun-15 1:11am
v2
Comments
[no name] 24-Jun-15 7:23am    
Just dumping a bunch of code into a forum is not a question.
larkspurklose3 24-Jun-15 7:26am    
sry if it hurted you..!! I'm new to this.. didn't know there are rules too..
[no name] 24-Jun-15 7:39am    
Why would you think that anything you could do would hurt me? You could have simply read the rules that were on the same page that was displayed to you want you was posting your code dump. Only stands to reason that just dumping a bunch of code in a posting does not tell us what your problem could possibly be. None of us are mind readers.
larkspurklose3 24-Jun-15 7:42am    
ok.. my mistake..!!
will remember from nxt tym

1 solution

Um.
Look at your code:
VB
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    button1 = New Button
    button2 = New Button
    button3 = New Button

    AddHandler button1.Click, AddressOf Button4_Click
    AddHandler button2.Click, AddressOf Button4_Click
    AddHandler button3.Click, AddressOf Button4_Click

Every time you click Button 4, you create three new buttons and add a handler to them. But the handler you add is the address of the routine that creates the four buttons, rather than the one that displays a message...
Perhaps you want to do this instead:
VB
AddHandler button1.Click, AddressOf button_click
AddHandler button2.Click, AddressOf button_click
AddHandler button3.Click, AddressOf button_click
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900