On Jul 25, 8:01*pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
This code in your form should do what you want, if I understand correctly..
Yes, thanks FTM! It worked fine. Just to complicate things and as a
teaching exercise to show for me how delegates work, I added a helper
function that is based on a user defined delegate. The helper
function shows a MessageBox with the button's name, when the button is
clicked. I used two different delegate/event based variables, one
being static, the other being non-static, to fire this helper
function.
It's interesting that you cannot define easily name a delegate Click
since there is a Click delegate (namely, the standard button click
event) already named in .NET, so it's forbidden to do so (compiler
error) unless you want to overload Click, which I don't want to touch,
so I used another delegate name. The location of the 'firing' of the
non-static and static-versions of the event is the same, but the
'registration' of the event is different for non-static (must be
declared once) versus static (must be declared inside of the _Click
function).
Below is the code.
RL
// 7/26/2008
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MDIForms
{
public partial class Form6 : Form
{
public delegate void ShowNameButEventHandler(object source,
EventArgs e);
//instance class version
public class ButtonHelper
{
public event ShowNameButEventHandler ShowNameBut;
public void OnShowNameBut(Button be, EventArgs e)
{
if (ShowNameBut != null) ShowNameBut(be,
EventArgs.Empty);
}
}
//static class version
public static class staticButtonHelper
{
public static event ShowNameButEventHandler
staticShowNameBut;
public static void staticOnShowNameBut(Button be,
EventArgs e)
{
if (staticShowNameBut != null) staticShowNameBut(be,
EventArgs.Empty);
}
}
Button[] buttons; //Button must be declared here to have
proper scope
/*
// *&* public event EventHandler Click; //not needed since
system has one predefined nongeneric EventHandler delegate called
“Click” already (button click), hence get compiler warning:
'MDIForms.Form6' already contains a definition for 'b_Click'
*/
public Form6()
{
InitializeComponent();
buttons = new Button[] { this.button1,
this.button2,this.button3,this.button4,this.button 5,this.button6,this.button7,this.button8,this.butt on9 };
//buttons 1-9 created using designer wizard prior to runtime
ButtonHelper buttonHelper = new ButtonHelper();
staticButtonHelper.staticShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut);
/*
//registration (old-style format); placing registration here is
mandiatory for static version // to prevent more than once
instantiation (compare with non-static version)
*/
foreach (Control c in Controls) //note convention
{
if (c is Button)
{
Button b = (Button)c;
//note convention "Click" is Delegate for signature function "_Click"
b.Click += b_Click; //new style registration;
// b.Click += new EventHandler(b_Click); //oldstyle format also
works
/*
// staticButtonHelper.staticShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //register,(modern
style) but don't register here--get nine instances!
// buttonHelper.ShowNameBut +=new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //works but should
not be registered here...you get nine popup Message boxes when form
constructed, not what you want
*/
// buttonHelper.OnShowNameBut(b,EventArgs.Empty);
//works but should not be fired here…get up to nine instances of
MessageBox
}
}
}
void b_Click(object sender, EventArgs e) {
Button b = (Button)sender;
Color old = b.BackColor;
b.BackColor = Color.Coral;
// NON-STATIC VERSION OF EVENT (works fine)
// ButtonHelper buttonHelper1 = new ButtonHelper(); //
create instances of ButtonHelper for non-static version of event
delegate
// buttonHelper1.ShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //register the non-
static public event
// buttonHelper1.OnShowNameBut(b, EventArgs.Empty); //fire
here, non-static instance version (works)
//STATIC VERSION OF EVENT (works fine)
// staticButtonHelper.staticShowNameBut += buttonHelper_ShowNameBut;
//WRONG PLACE TO register static version (gives up to nine
instantiations, everytime the loop is traversed another added)
staticButtonHelper.staticOnShowNameBut(b, EventArgs.Empty);
//fire here, static version;
/*
// both static and non-satic versions of ButtonHelper fired from
inside b_Click, which is automatically fired when buttons are clicked
(depends on button click, see *&* above)
*/
}
//static member function used by delegate/events (same for both static
and non-static delegate/events
static void buttonHelper_ShowNameBut(object sender, EventArgs
e)
{
Button b = (Button)sender;
MessageBox.Show(b.Name + "!");
}
} //end bracket, form 6
} //end bracket, namespace