Need to extract the Click event from tsMenuItem.Events

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

newItem and tsMenuItem are ToolStripMenuItems.

I'd like to add the click event in tsMenuItem to newItem.



Can't do this because Events is Protected:

newItem.Events.AddHandlers(tsMenuItem.Events);

Can't do this because Click must be on the left side:

newItem.Click += tsMenuItem.Click;



Is there some way I can extract the Click event from tsMenuItem.Events so
that I can do this:

newItem.Click += ??



Thanks in advance
 
Well, now I have my collection of code that doesn't work and your statement
that does.

I'm quite new to C#. Could you add a few words about the syntax and how it
works.

I not sure if you are defining a Delagate or declaring one.


Thanks very much


newItem and tsMenuItem are ToolStripMenuItems.

I'd like to add the click event in tsMenuItem to newItem.

So you want the tsMenuItem.Click event handlers to be called in response
to a Click event on newItem?

What about this:

newItem.Click += delegate(object sender, EventArgs e)
{ tsMenuItem.PerformClick(); };

Pete
 
The trouble I'm having is that if I look at the methods and constructors I
can't find one that fits:
newItem.Click += delegate(object sender, EventArgs e)
{ tsMenuItem.PerformClick(); };

Thanks again



Well, now I have my collection of code that doesn't work and your
statement
that does.

I'm quite new to C#. Could you add a few words about the syntax and how
it
works.

I not sure if you are defining a Delagate or declaring one.

Sure. It seems to me that the terms "defining" and "declaring" are not
sufficiently unambiguous. However, in this case the delegate type is
already defined (it's the type used by the Click event, which is
EventHandler), and you are declaring an anonymous method to be used to
create the delegate instance added to the Click event.

As the name suggests, it's basically a method without a name. You could
do the exact same thing by writing a named method that calls
"tsMenuItem.PerformClick()", and adding that named to the event instead.
For example, a method like this:

void ClickRedirect(object sender, EventArgs e)
{
tsMenuItem.PerformClick();
}

and then elsewhere a line of code that looks like:

newItem.Click += ClickRedirect;

Using the anonymous method allows the code to be more self-contained IMHO,
but otherwise it's more a matter of preference, at least in this
particular situation.

Pete
 
I understand: anonymous method

I would like to read about the statement
delegate(object sender, EventArgs e) { tsMenuItem.PerformClick(); };

I can see it doesn't fit the format of a declaration (which you verify
below)
Nor that of a Delegate Constructor

You see what I mean? I look at MSDN library and can't find a format that
fits your construction.


Thanks






(my apologies for the late reply...I only just now noticed that this
message, along with five others, somehow got stuck in my Outbox)

The trouble I'm having is that if I look at the methods and constructors
I
can't find one that fits:
newItem.Click += delegate(object sender, EventArgs e)
{ tsMenuItem.PerformClick(); };

I don't know what that means. Are you trying to find a method or
constructor named "delegate"? If so, you're right...you're not going to
find it.

The word "delegate" is a C# keyword. In one context, it is used to
declare a new delegate type. But in the above context, it's used to
declare an anonymous method. By definition, the anonymous method has no
name and so you won't find it listed in any list of methods.

Pete
 
Peter Duniho said:
Are you sure? :)
No, but I thought that if Foo was a function that returned a string
"anonymous" would apply if I wrote
Console.Writeline(Foo);
Is that not true?

Anyway, I appreciate the pointer to the site.
That explained a lot.


thanks
 
Peter Duniho said:
That's not true. Ignoring for the moment that if Foo is a function (i.e.
"method"), writing "Console.WriteLine(Foo);" doesn't make sense... The
method "Foo" has a name. Something with a name isn't anonymous, by
definition.

But the string variable does not have a name.

Thanks for all the help
 
AAaron123 said:
But the string variable does not have a name.

I just had ocassion to read what I said.
I do see the difference between the function return being anonomyous and the
body being anonomous.
Thanks again
 
Back
Top