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. How to pass a parameter in an event

How to pass a parameter in an event

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    Silvyster
    wrote on last edited by
    #1

    Hi.. i'm making a program and this is how it goes.. I have actually two buttons and having the same event handler... how will i know which button triggered the event?.. what i was planning to do is just to pass a parameter to the method but it seems like the only parameters that is only allowed is the (object sender, EventArgs e)... am not that really proficient in C# so i guess you could give me solutions and workarounds on how to do this

    P 1 Reply Last reply
    0
    • S Silvyster

      Hi.. i'm making a program and this is how it goes.. I have actually two buttons and having the same event handler... how will i know which button triggered the event?.. what i was planning to do is just to pass a parameter to the method but it seems like the only parameters that is only allowed is the (object sender, EventArgs e)... am not that really proficient in C# so i guess you could give me solutions and workarounds on how to do this

      P Offline
      P Offline
      PandemoniumPasha
      wrote on last edited by
      #2

      The object 'sender' is the button that triggered the event. just cast it to button type and check if it is the one you require. e.g. Button b = sender as Button; if(b == button1) .....; else ...; or you could assign a value in the "Tag" property of the button and check it in the event handler.

      regards :)

      S 1 Reply Last reply
      0
      • P PandemoniumPasha

        The object 'sender' is the button that triggered the event. just cast it to button type and check if it is the one you require. e.g. Button b = sender as Button; if(b == button1) .....; else ...; or you could assign a value in the "Tag" property of the button and check it in the event handler.

        regards :)

        S Offline
        S Offline
        Silvyster
        wrote on last edited by
        #3

        Thanks a lot.. can i also cast like using the java syntax like "Button b = (Button) sender" <--- is that possible?

        J 1 Reply Last reply
        0
        • S Silvyster

          Thanks a lot.. can i also cast like using the java syntax like "Button b = (Button) sender" <--- is that possible?

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Silvyster wrote:

          is that possible?

          Did you try it? What happened?

          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