Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I detect which button is clicked on the form?

How can I detect which button is clicked on the form?

Scheduled Pinned Locked Moved Visual Basic
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JUNEYT
    wrote on last edited by
    #1

    Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.

    Public Sub AddButtons()
    
    For I = 1 to 10
        Dim myButton as new Button
            myButton.text = "Hello"
            me.Controls.Add(myButton)
    
            AddHandler myButton.click, AddressOf myButtonEvent
    Next
    
    End Sub
    
    ' Here is the event handler for the button
    Private sub myButtonEvent (Byval Sender as Object, e as EventArg)
        
         'Here I want to know which button has been clicked because there are 10 
         'buttons on the form.
    
    End Sub
    

    What a curious mind needs to discover knowledge is noting else than a pin-hole.

    M K T 3 Replies Last reply
    0
    • J JUNEYT

      Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.

      Public Sub AddButtons()
      
      For I = 1 to 10
          Dim myButton as new Button
              myButton.text = "Hello"
              me.Controls.Add(myButton)
      
              AddHandler myButton.click, AddressOf myButtonEvent
      Next
      
      End Sub
      
      ' Here is the event handler for the button
      Private sub myButtonEvent (Byval Sender as Object, e as EventArg)
          
           'Here I want to know which button has been clicked because there are 10 
           'buttons on the form.
      
      End Sub
      

      What a curious mind needs to discover knowledge is noting else than a pin-hole.

      M Offline
      M Offline
      M Hall
      wrote on last edited by
      #2

      in your button event: dim b as Button b= Ctype(sender,Button) the b will be the button that was clicked. If you are wanting to run different code depending on what button was clicked you can check for the name, or assign a value to the tag property and use that for identification. Hope that helps:) -- modified at 11:34 Tuesday 13th March, 2007

      J 1 Reply Last reply
      0
      • M M Hall

        in your button event: dim b as Button b= Ctype(sender,Button) the b will be the button that was clicked. If you are wanting to run different code depending on what button was clicked you can check for the name, or assign a value to the tag property and use that for identification. Hope that helps:) -- modified at 11:34 Tuesday 13th March, 2007

        J Offline
        J Offline
        JUNEYT
        wrote on last edited by
        #3

        Yep! it is working now. Thank you very much. :cool:

        What a curious mind needs to discover knowledge is noting else than a pin-hole.

        1 Reply Last reply
        0
        • J JUNEYT

          Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.

          Public Sub AddButtons()
          
          For I = 1 to 10
              Dim myButton as new Button
                  myButton.text = "Hello"
                  me.Controls.Add(myButton)
          
                  AddHandler myButton.click, AddressOf myButtonEvent
          Next
          
          End Sub
          
          ' Here is the event handler for the button
          Private sub myButtonEvent (Byval Sender as Object, e as EventArg)
              
               'Here I want to know which button has been clicked because there are 10 
               'buttons on the form.
          
          End Sub
          

          What a curious mind needs to discover knowledge is noting else than a pin-hole.

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          You would want to set the button id value. Something like myButton.Id = "btn"+I.ToString(); Then in the event CType(Sender, Button).Id to figure out which button. Hope that helps. Ben

          1 Reply Last reply
          0
          • J JUNEYT

            Hello! In shown sample below, I have a loop what ceates instances of a button. What I want to know is which one gets clicked in runtime? Thank you.

            Public Sub AddButtons()
            
            For I = 1 to 10
                Dim myButton as new Button
                    myButton.text = "Hello"
                    me.Controls.Add(myButton)
            
                    AddHandler myButton.click, AddressOf myButtonEvent
            Next
            
            End Sub
            
            ' Here is the event handler for the button
            Private sub myButtonEvent (Byval Sender as Object, e as EventArg)
                
                 'Here I want to know which button has been clicked because there are 10 
                 'buttons on the form.
            
            End Sub
            

            What a curious mind needs to discover knowledge is noting else than a pin-hole.

            T Offline
            T Offline
            testy_proconsul
            wrote on last edited by
            #5

            Private Sub AddButtons Dim btn As Button For i As Integer = 1 To 10 btn = New Button() btn.Name = "GBtn" & i.ToString() btn.Text = "Push " & i.ToString() btn.Top = btn.Height * i AddHandler btn.Click, AddressOf GBtn_Click Me.Controls.Add(btn) Next End Sub Private Sub GBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show(sender.ToString()) Select Case DirectCast(sender, Button).Name Case "GBtn1" Case "GBtn2" End Select End Sub

            solidIT.de - under construction Components for Microsoft .Net audittrail, objectcomparer, deepcopy and much more ...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups