Hi,
I have a very simple question.
In a Windows Form, I have a series of Labels which have several Event Handlers defined.
When a button is clicked, I would like to loop through all these Labels and dispatch the Click event on each.
Now, if they were buttons, I could simply call label.PerformClick(), but this doesn't exist for Labels (and, no, I can't change them to buttons!).
So, how do I dispatch the Click event (or any of the other built-in events) for a control?
(From an ActionScript perspective, this would be simply label.dispatchEvent(new Event(MouseEvent.CLICK)) )
Thanks for your help.
Loading

Christian Wirth8 reputationPosted Jun 25, 2009, 9:49 PM EDT
But why don´t you call the eventhandler methods directly instead? They are just methods after all...
public Constructor() { Label l = new Label(); l.Click += new EventHandler(l_Click); } //HANDLER void l_Click(object sender, EventArgs e) { //Do something } //instead of raising the clickevent just call l_Click(l, new EventArgs());Jitendra Kumar Thakur160 reputationPosted Jul 22, 2009, 12:24 PM EDT
using System; using System.Drawing; using System.Windows.Forms; namespace Scan2FM { /// /// Summary description for CLabel. /// public class CLabel: System.Windows.Forms.Control { //Implements Label as I expect it to be (more or less...) private BorderStyle borderStyle = BorderStyle.None; private ContentAlignment textAlignment = ContentAlignment.TopLeft; // Accessing the font object of the Base Class throws a nasty exception! // So, I am using my own font. private string fontName = "Tahoma"; private int fontSize = 10; private FontStyle fontStyle = FontStyle.Regular; public event EventHandler labelClick; public event PaintEventHandler labelPaint; public CLabel() { } public BorderStyle BorderStyle { get {return this.borderStyle;} set {this.borderStyle = value;} } public string FontName { get {return this.fontName;} set {this.fontName = value;} } public int FontSize { get {return this.fontSize;} set {this.fontSize = value;} } public FontStyle FontStyle { get {return this.fontStyle;} set {this.fontStyle = value;} } public ContentAlignment TextAlignment { get {return this.textAlignment;} set {this.textAlignment = value;} } protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) { Graphics graphics = pe.Graphics; Pen pen = new Pen(Color.Black); SolidBrush brush = new SolidBrush(ForeColor); Font font = new Font(this.fontName, this.fontSize, this.fontStyle); SizeF s; if (this.BorderStyle == BorderStyle.FixedSingle) graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); else if (this.BorderStyle == BorderStyle.Fixed3D) graphics.DrawRectangle(pen, this.ClientRectangle); s = graphics.MeasureString(this.Text, font); switch (this.textAlignment) { case ContentAlignment.TopCenter: graphics.DrawString(this.Text, font, brush, (this.Width - s.Width) / 2, (this.Height - s.Height) / 2); break; case ContentAlignment.TopLeft: graphics.DrawString(this.Text, font, brush, 2, (this.Height - s.Height) / 2); break; case ContentAlignment.TopRight: graphics.DrawString(this.Text, font, brush, this.Width - s.Width - 2, (this.Height - s.Height) / 2); break; } pen.Dispose(); brush.Dispose(); font.Dispose(); if (labelPaint != null) labelPaint(this, pe); base.OnPaint(pe); } protected override void OnClick(System.EventArgs e) { if (labelClick != null) labelClick(this, e); base.OnClick(e); } } }