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. Sending the button name to a method.

Sending the button name to a method.

Scheduled Pinned Locked Moved C#
tutorial
9 Posts 6 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.
  • O Offline
    O Offline
    outerhell
    wrote on last edited by
    #1

    Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }

    M P L P 4 Replies Last reply
    0
    • O outerhell

      Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }

      M Offline
      M Offline
      Michael Bookatz
      wrote on last edited by
      #2

      the sender is the buttun that sent the object so you can do

      button x = (button)sender;

      PS please put code in code blocks makes it easier to read!

      O 1 Reply Last reply
      0
      • O outerhell

        Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You already have this information - in the sender parameter. All you need do is cast it to a Button and then use the name from that, e.g.

        string name = ((Button)sender).Name;

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        O 1 Reply Last reply
        0
        • O outerhell

          Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, on top of that all, you don't have to use the Name property (if the Form got designed with Visual Designer it won't like you change a Control's Name). You could use its Text property instead, or store something in the Tag property. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          P 1 Reply Last reply
          0
          • P Pete OHanlon

            You already have this information - in the sender parameter. All you need do is cast it to a Button and then use the name from that, e.g.

            string name = ((Button)sender).Name;

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            O Offline
            O Offline
            outerhell
            wrote on last edited by
            #5

            Oww yeah... I'm quite fresh in coding. But thank you very much for the quick response and right what I needed :)

            1 Reply Last reply
            0
            • M Michael Bookatz

              the sender is the buttun that sent the object so you can do

              button x = (button)sender;

              PS please put code in code blocks makes it easier to read!

              O Offline
              O Offline
              outerhell
              wrote on last edited by
              #6

              "PS please put code in code blocks makes it easier to read!" Thanks for the suggestion :) will use it next time. And thank you for the quick response.

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, on top of that all, you don't have to use the Name property (if the Form got designed with Visual Designer it won't like you change a Control's Name). You could use its Text property instead, or store something in the Tag property. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Yeah, I prefer to use the Tag too, that way I can associate an object with the control, much cleaner.

                1 Reply Last reply
                0
                • O outerhell

                  Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }

                  P Offline
                  P Offline
                  padmanabhan N
                  wrote on last edited by
                  #8

                  private void btn0_Click(object sender, EventArgs e) { Button btn = (Button)sender; check(); if (btn.Name == btn0.Name) { txtcalculate.Text = txtcalculate.Text + "0"; } if (btn.Name == btn1.Name) { txtcalculate.Text = txtcalculate.Text + "1"; } if (btn.Name == btn2.Name) { txtcalculate.Text = txtcalculate.Text + "2"; } } //make all the button event to be btn0_Click

                  Padmanabhan

                  O 1 Reply Last reply
                  0
                  • P padmanabhan N

                    private void btn0_Click(object sender, EventArgs e) { Button btn = (Button)sender; check(); if (btn.Name == btn0.Name) { txtcalculate.Text = txtcalculate.Text + "0"; } if (btn.Name == btn1.Name) { txtcalculate.Text = txtcalculate.Text + "1"; } if (btn.Name == btn2.Name) { txtcalculate.Text = txtcalculate.Text + "2"; } } //make all the button event to be btn0_Click

                    Padmanabhan

                    O Offline
                    O Offline
                    outerhell
                    wrote on last edited by
                    #9

                    Thank you. Got it working now :)

                    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