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. C# - Using sender

C# - Using sender

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorialquestion
4 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.
  • K Offline
    K Offline
    kezza6543
    wrote on last edited by
    #1

    //I have an array of panels public Panel[] a = new Panel[10]; //During the creation of 10 panels i set the tag for each panel as below int i = 0; for (i = 0; i < 9; i++) { a[i] = new Panel(); a[i].Parent = a_Holder; a[i].Tag = i; a[i].Top = (i * 16); a[i].Left = 10; a[i].Height = 15; a[i].Width = 250; a[i].Click += delegate { abcd( a[i] ) }; } //on clicking a panel the below method is called public void abcd (object sender) { textbox1.text = convert.tostring(sender.tag); } I basically want to be able to click on a panel and have that panels tag come up in the textbox. The above example does not work and im sure im way of base, can anyone help? Cheers

    C P 2 Replies Last reply
    0
    • K kezza6543

      //I have an array of panels public Panel[] a = new Panel[10]; //During the creation of 10 panels i set the tag for each panel as below int i = 0; for (i = 0; i < 9; i++) { a[i] = new Panel(); a[i].Parent = a_Holder; a[i].Tag = i; a[i].Top = (i * 16); a[i].Left = 10; a[i].Height = 15; a[i].Width = 250; a[i].Click += delegate { abcd( a[i] ) }; } //on clicking a panel the below method is called public void abcd (object sender) { textbox1.text = convert.tostring(sender.tag); } I basically want to be able to click on a panel and have that panels tag come up in the textbox. The above example does not work and im sure im way of base, can anyone help? Cheers

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This code does not work because object does not have a tag property. First cast it to Panel ( which is what it will be ). Of course, you should replace your delegate with a proper OnClick handler ( I'm sure that's not the right syntax, don't you need an eventargs, and you sure have the syntax for adding an event handler all wrong. ). 'The above example does not work' is not very helpful - in future try telling us why. I doubt this would compile, so report the compiler errors. If it crashes, report which line crashes and what the exception is. Working that stuff out might actually help you to fix the problem yourself ( not that we mind helping, but surely you want to learn how to do it yourself ? ) Christian Graus - Microsoft MVP - C++

      K 1 Reply Last reply
      0
      • C Christian Graus

        This code does not work because object does not have a tag property. First cast it to Panel ( which is what it will be ). Of course, you should replace your delegate with a proper OnClick handler ( I'm sure that's not the right syntax, don't you need an eventargs, and you sure have the syntax for adding an event handler all wrong. ). 'The above example does not work' is not very helpful - in future try telling us why. I doubt this would compile, so report the compiler errors. If it crashes, report which line crashes and what the exception is. Working that stuff out might actually help you to fix the problem yourself ( not that we mind helping, but surely you want to learn how to do it yourself ? ) Christian Graus - Microsoft MVP - C++

        K Offline
        K Offline
        kezza6543
        wrote on last edited by
        #3

        Sorry, i've only been using C# for about a week and i've still got a lot to teach myself. However your comments helped and it now makes a lot more sense to me :) Thanks for your help!

        1 Reply Last reply
        0
        • K kezza6543

          //I have an array of panels public Panel[] a = new Panel[10]; //During the creation of 10 panels i set the tag for each panel as below int i = 0; for (i = 0; i < 9; i++) { a[i] = new Panel(); a[i].Parent = a_Holder; a[i].Tag = i; a[i].Top = (i * 16); a[i].Left = 10; a[i].Height = 15; a[i].Width = 250; a[i].Click += delegate { abcd( a[i] ) }; } //on clicking a panel the below method is called public void abcd (object sender) { textbox1.text = convert.tostring(sender.tag); } I basically want to be able to click on a panel and have that panels tag come up in the textbox. The above example does not work and im sure im way of base, can anyone help? Cheers

          P Offline
          P Offline
          Philip Price
          wrote on last edited by
          #4

          textBox1.Text = Convert.ToString((int)((Panel)sender.Tag));

          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