C# - Using sender
-
//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
-
//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
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++
-
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++
-
//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
textBox1.Text = Convert.ToString((int)((Panel)sender.Tag));