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. Using event handler - how to identify which control was clicked when several controls 'share' the same eventhandler?

Using event handler - how to identify which control was clicked when several controls 'share' the same eventhandler?

Scheduled Pinned Locked Moved C#
questioncssdata-structurestutorial
8 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.
  • S Offline
    S Offline
    spd69
    wrote on last edited by
    #1

    I have created an 'grid' of text boxes 17 (rows) by 20 (columns) within a groupbox. This is like using a conrtol array in VB6. I gave each a unique name e.g. txtboxR08C12 (row 8 column 12) Just prior to adding the control to the group box I added an EventHandler to the textbox. How can I identify in the EventHandler exactly which of the textboxes (preferably by name). So that I can amend the background colour and the contents to reflect a count (determined elsewhere in my code and stored in an array)

    G A M 3 Replies Last reply
    0
    • S spd69

      I have created an 'grid' of text boxes 17 (rows) by 20 (columns) within a groupbox. This is like using a conrtol array in VB6. I gave each a unique name e.g. txtboxR08C12 (row 8 column 12) Just prior to adding the control to the group box I added an EventHandler to the textbox. How can I identify in the EventHandler exactly which of the textboxes (preferably by name). So that I can amend the background colour and the contents to reflect a count (determined elsewhere in my code and stored in an array)

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      spd69, The standard event param syntax is:

      Event(object sender, EventArgs e);

      Sender is the object which has raised the event, so in your case, it would be the textbox.

      Regards, Gareth. (FKA gareth111)

      1 Reply Last reply
      0
      • S spd69

        I have created an 'grid' of text boxes 17 (rows) by 20 (columns) within a groupbox. This is like using a conrtol array in VB6. I gave each a unique name e.g. txtboxR08C12 (row 8 column 12) Just prior to adding the control to the group box I added an EventHandler to the textbox. How can I identify in the EventHandler exactly which of the textboxes (preferably by name). So that I can amend the background colour and the contents to reflect a count (determined elsewhere in my code and stored in an array)

        A Offline
        A Offline
        Ashfield
        wrote on last edited by
        #3

        dim x as textbox = ctype(sender,textbox) if x.name = "txtboxR08C12" .... (I think my vb syntax is correct)

        Bob Ashfield Consultants Ltd

        S 1 Reply Last reply
        0
        • A Ashfield

          dim x as textbox = ctype(sender,textbox) if x.name = "txtboxR08C12" .... (I think my vb syntax is correct)

          Bob Ashfield Consultants Ltd

          S Offline
          S Offline
          spd69
          wrote on last edited by
          #4

          Thanks for your answer I mentioned vb6 as that is where I know control arrays from. But I was looking for a c# method. Thank you for your time

          T A 2 Replies Last reply
          0
          • S spd69

            Thanks for your answer I mentioned vb6 as that is where I know control arrays from. But I was looking for a c# method. Thank you for your time

            T Offline
            T Offline
            That Asian Guy
            wrote on last edited by
            #5

            TextBox tb = sender as Textbox; if (tb.Name == ...

            1 Reply Last reply
            0
            • S spd69

              I have created an 'grid' of text boxes 17 (rows) by 20 (columns) within a groupbox. This is like using a conrtol array in VB6. I gave each a unique name e.g. txtboxR08C12 (row 8 column 12) Just prior to adding the control to the group box I added an EventHandler to the textbox. How can I identify in the EventHandler exactly which of the textboxes (preferably by name). So that I can amend the background colour and the contents to reflect a count (determined elsewhere in my code and stored in an array)

              M Offline
              M Offline
              MarkB777
              wrote on last edited by
              #6

              TextBox box = sender as TextBox; box will then become the textbox that has fired the event, and you can handle it accordingly.

              Mark Brock Click here to view my blog

              S 1 Reply Last reply
              0
              • S spd69

                Thanks for your answer I mentioned vb6 as that is where I know control arrays from. But I was looking for a c# method. Thank you for your time

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #7

                Sorry, I amswer questions in several forums. The same logic applies, cast sender to the right type and you can get the name.

                Bob Ashfield Consultants Ltd

                1 Reply Last reply
                0
                • M MarkB777

                  TextBox box = sender as TextBox; box will then become the textbox that has fired the event, and you can handle it accordingly.

                  Mark Brock Click here to view my blog

                  S Offline
                  S Offline
                  spd69
                  wrote on last edited by
                  #8

                  thanks this worked a treat. Sorry about the delay other matters took up my time until now (2:13am - lol) Cheers

                  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