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. daynamic event handlers....

daynamic event handlers....

Scheduled Pinned Locked Moved C#
question
8 Posts 3 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.
  • X Offline
    X Offline
    Xelalem
    wrote on last edited by
    #1

    i created 100 buttons ( each with different text) with click even handlers daynamically.how can i display the text of the clicked button (how can i know which button is being clicked)?

    K P 2 Replies Last reply
    0
    • X Xelalem

      i created 100 buttons ( each with different text) with click even handlers daynamically.how can i display the text of the clicked button (how can i know which button is being clicked)?

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Depending on how you did it the event handler is probably of the form

      void Button_OnClick(object sender, eventargs args)
      {
      //......
      }

      the variable sender is most likely the button (dependant upon your implementation), so you can probably do something like

      void Button_OnClick(object sender, eventargs args)
      {
      Button buttonClicked = sender as Button;
      if(buttonClicked == null)
      return;
      string buttonText = buttonClicked.Text;
      // use buttonText here or wherever....
      }

      If this doesn't answer your question, post some code and please remember the <pre> tags!

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      1 Reply Last reply
      0
      • X Xelalem

        i created 100 buttons ( each with different text) with click even handlers daynamically.how can i display the text of the clicked button (how can i know which button is being clicked)?

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

        The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample

        private void ButtonEventHandler(object sender, EventArgs e)
        {
        Button button = sender as Button;
        if (button == null) return;
        txtClicked.Text = button.Text;
        }

        "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

        K X 2 Replies Last reply
        0
        • P Pete OHanlon

          The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample

          private void ButtonEventHandler(object sender, EventArgs e)
          {
          Button button = sender as Button;
          if (button == null) return;
          txtClicked.Text = button.Text;
          }

          "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

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          Now that is just frightening! :-)

          Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

          X 1 Reply Last reply
          0
          • K Keith Barrow

            Now that is just frightening! :-)

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

            X Offline
            X Offline
            Xelalem
            wrote on last edited by
            #5

            tnx alot

            K 1 Reply Last reply
            0
            • P Pete OHanlon

              The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample

              private void ButtonEventHandler(object sender, EventArgs e)
              {
              Button button = sender as Button;
              if (button == null) return;
              txtClicked.Text = button.Text;
              }

              "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

              X Offline
              X Offline
              Xelalem
              wrote on last edited by
              #6

              tnx

              P 1 Reply Last reply
              0
              • X Xelalem

                tnx

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

                You're welcome.

                "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

                1 Reply Last reply
                0
                • X Xelalem

                  tnx alot

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  No problem!

                  Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                  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