From: Rich P on
Hi Pete,

Thanks for this suggestion. I do believe this is the one I will go with.
Note (question) can I do the anonymous method thing in the Form designer
where the controls get registered/initialized? And yes, I take note of
correct terminoloty. I did mean to say Event Handler - just like not
sure if a control gets registered or initialized in the form Designer
window. But is this where I would do the anonymous method thing with
the 3rd arg?

Also, I am drawing the graphs from scratch on each panel (drawing an
ellipse for each point and line segments connecting the ellipses). The
users wanted everything on a sort of continuous form.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: Peter Duniho on
Rich P wrote:
> Hi Pete,
>
> Thanks for this suggestion. I do believe this is the one I will go with.
> Note (question) can I do the anonymous method thing in the Form designer
> where the controls get registered/initialized?

I'm not sure what you're asking. If you mean, can you assign an
anonymous method to be the event handler for each Button instance's
Click event, then no�the method has to have a name for the Designer to
use it.

> And yes, I take note of
> correct terminoloty. I did mean to say Event Handler - just like not
> sure if a control gets registered or initialized in the form Designer
> window. But is this where I would do the anonymous method thing with
> the 3rd arg? [...]

Sorry, that last sentence didn't parse. Is what where you would do what
anonymous method thing?

Pete
From: Rich P on
form1.Designer.cs
>
#region Windows Form Designer generated code
...

#endregion

private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
...
<

Is this where I could add the anonymous method to each button? Or do I
need to do that in form1.cs?

Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: kndg on
Peter Duniho wrote:
> [...]
> An example of that approach would be something like this:
>
> class Form1 : Form
> {
> GraphData[] _data = /* ...initialized as appropriate... *;
>
> public Form1()
> {
> InitializeComponent();
>
> button1.Click += (sender, e) => GeneralPurposeClickHandler(sender,
> e, _data[0]);
> button2.Click += (sender, e) => GeneralPurposeClickHandler(sender,
> e, _data[1]);
> button3.Click += (sender, e) => GeneralPurposeClickHandler(sender,
> e, _data[2]);
> // etc.
>
> }
>
> void GeneralPurposeClickHandler(object sender, EventArgs e,
> GraphData gd)
> {
> Control ctl = (Control)sender;
> Panel panel = (Panel)ctl.Parent;
>
> // No need to retrieve the graph data from the "panel".
> // It's been passed to the method as the third argument.
> }
> }
>
> I like the latter approach in that I find it simple to implement, and
> easier to read. But, it can get a bit tedious to do if you wind up with
> a large number of event handlers you have to include, since of course
> you are in a sense writing a new event handler for each control (each
> "(sender, e) => GeneralPurposeClickHandler(sender, e, _data[x]);" is
> basically a new method). If you can create a convenient data mapping
> from each Panel instance to the appropriate GraphData instance, then the
> former approach can work very well, and has the added benefit that there
> really is only just the one method ever.
>

Hi Pete,

Not to say that I don't agree with you... but I do think that the
anonymous method is overuse here. If the OP have 50 buttons, it will end
up having 50 anonymous methods in the compiled code. Not that the
increase of the executable size will effect the performance, but I have
read somewhere that the JIT will optimize the method that is called
frequently better that the less frequent method (50 buttons, 1 method vs
50 buttons, 50 methods).

Happy Holiday!
From: Peter Duniho on
Rich P wrote:
> form1.Designer.cs
> #region Windows Form Designer generated code
> ...
>
> #endregion
>
> private System.Windows.Forms.Timer timer1;
> private System.Windows.Forms.Button button1;
> private System.Windows.Forms.Button button2;
> private System.Windows.Forms.Button button3;
> ...
> <
>
> Is this where I could add the anonymous method to each button? Or do I
> need to do that in form1.cs?

The latter. Editing the *.Designer.cs file by hand is just asking for
all your code to disappear next time you use the Designer to modify the
form's layout.

Pete
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: ExecuteScalar
Next: Fluent interface