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. C#
  4. Get Button

Get Button

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 5 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.
  • T Offline
    T Offline
    The underdog
    wrote on last edited by
    #1

    I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?

    A L M L 4 Replies Last reply
    0
    • T The underdog

      I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?

      A Offline
      A Offline
      Ashok Dhamija
      wrote on last edited by
      #2

      Have a common (i.e., the same) event-handler for all the buttons on your form, say ButtonClick. Implement this common event-handler as under, for example:

      private void ButtonClick(object sender, System.EventArgs e)
      {
      if((Button)sender == button1)
      {
      button1.BackColor = Color.Green;
      MessageBox.Show("button1 was clicked", "Form1");
      }
      else if((Button)sender == button2)
      {
      button2.BackColor = Color.Pink;
      MessageBox.Show("button2 was clicked", "Form1");
      }
      else if((Button)sender == button3)
      {
      button3.BackColor = Color.Blue;
      MessageBox.Show("button3 was clicked", "Form1");
      }
      else if((Button)sender == button4)
      {
      button4.BackColor = Color.Yellow;
      MessageBox.Show("button4 was clicked", "Form1");
      }
      }

      It works properly and identifies the correct button pressed. You can add your own code in place of the sample code above. If the number of buttons is too large, you can even consider having an ArrayList of the buttons.

      1 Reply Last reply
      0
      • T The underdog

        I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        Hello you don't have to declare 2 string variables here. Try: textBox1.Text = sender.ToString().Substring(35); As to get the button, have you tried: Button myButton = (Button)sender; -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        1 Reply Last reply
        0
        • T The underdog

          I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          The sender argument of your Click event handler _is_ the button that was pressed. So you can easily cast it to Button and then do with it whatever you want:

          private void button_Click(object sender, System.EventArgs e)
          {
          Button b = sender as Button;

          if (b != null) // if null the event handler has been bound to a non-Button
          {
          // Reset all other Buttons
          foreach (Control c in this.Controls)
          {
          if (c is Button)
          c.BackColor = SystemColors.Control;
          }
          // Colorize the button pressed
          b.BackColor = Color.Red;
          }
          }

          Regards, mav

          1 Reply Last reply
          0
          • T The underdog

            I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?

            L Offline
            L Offline
            Luis Alonso Ramos
            wrote on last edited by
            #5

            And no one has suggested it yet... but this might help:

            Button btn = (Button) sender;
            MessageBox.Show(btn.Text); // Display button's caption
            

            I hope this helps! -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            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