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. Set event for 30 buttons

Set event for 30 buttons

Scheduled Pinned Locked Moved Visual Basic
question
7 Posts 7 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.
  • C Offline
    C Offline
    ctlqt12
    wrote on last edited by
    #1

    Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

    C D X S R 6 Replies Last reply
    0
    • C ctlqt12

      Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      ctlqt12 wrote:

      It means i must write event for each button and i have 30 functions for 30 buttons.

      No, it doesn't. The sender property on an event is the control that raised it. So, write one event that casts that to a Button, grabs the text and sets it to the textbox.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • C ctlqt12

        Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Have you noticed the Handles clause at the end of the method header for a button click??

        Private Sub Button1_Click(blah, blah,) Handles Button1.Click

        Well, you can tack on as many buttons as you want like this:

        Private Sub Button1_Click(blah, blah,) Handles Button1.Click, Button2.Click, Button3.Click, ...

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        1 Reply Last reply
        0
        • C ctlqt12

          Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

          X Offline
          X Offline
          Xandip
          wrote on last edited by
          #4

          Hey.. What did you mean by a button's TEXT?? :omg:

          The name is Sandeep

          1 Reply Last reply
          0
          • C ctlqt12

            Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

            S Offline
            S Offline
            Sonia Gupta
            wrote on last edited by
            #5

            after tagging the events as Dave Kreskowiak told with the handler detect the particular button dim b a button = ctype(sender,button) select case b.name case "" ..... end select

            1 Reply Last reply
            0
            • C ctlqt12

              Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

              R Offline
              R Offline
              Rupesh Kumar Swami
              wrote on last edited by
              #6

              use follwing code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, ...Button30.Click textbox1.text=sender.text '(or textbox1.text=textbox1.text & sender.text) end sub Hope this helps

              Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

              1 Reply Last reply
              0
              • C ctlqt12

                Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )

                K Offline
                K Offline
                Kiran Kumar B
                wrote on last edited by
                #7

                Hi, In which language are you writing the code for this? vb or vb.net? If in case of classic vb, use buttons as control array and write a single function. if in case of vb.net, write a event as follows : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click Dim kBtn As Button kBtn = sender TextBox1.Text = kBtn.Text End Sub Here you can use the source button's properties using kBtn variable. Thanks, Kiran Kumar

                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