Pleaes Explain >> Programmatically Clicking Derived ToolStripButton Button (Code Block Included)

T

Tom

The included code block demonstrates programmatically clicking a
non-derived ToolStripButton in the OnShown() method.

When I derive a button from ToolStripButton and override the _private_
OnClick() method (also shown in the code block) ... the button works
as expected when it is manually clicked.

Please explain how I can programmatically click the derived button.
The fact that the derived button's OnClick() method is "private"
prevents me from directly calling it from within the OnShown() method.

My guess is this problem revolves around some sort of delegate/event
handler confusion I have yet to sort out. Hopefully soon another light
will start to glow.

Thank you in advance. This amazing group has never failed to sort out
the many learning hurdles this newb keeps straddling.

-- Tom

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

public class testForm : Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new testForm());
}

testForm()
{
ToolStrip appToolStrip = new ToolStrip();
appToolStrip.Parent = this;
appToolStrip.Dock = DockStyle.Left;

ToolStripButton button_1 = new ToolStripButton();
button_1.Text = "Button #1";
appToolStrip.Items.Add(button_1);
button_1.Click += new EventHandler(button_1_Click);

DerivedButton derivedButton = new DerivedButton();
derivedButton.Text = "The Derived Button";
derivedButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
appToolStrip.Items.Add(derivedButton);
}

protected override void OnShown(EventArgs e)
{
button_1_Click(this, EventArgs.Empty);
}

void button_1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button #1 Has Been Clicked");
}

} // public class testForm : Form

// ####################################################
public class DerivedButton : ToolStripButton
{
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("The DerivedButton Has Been Clicked");
}
}
 
P

Peter Duniho

[...]
Please explain how I can programmatically click the derived button.
The fact that the derived button's OnClick() method is "private"
prevents me from directly calling it from within the OnShown() method.

My guess is this problem revolves around some sort of delegate/event
handler confusion I have yet to sort out. Hopefully soon another light
will start to glow.

Well, DerivedButton.OnClick() is actually "protected". But close enough.
:) Your question is also confusing as you've got two buttons in there,
unrelated to each other, which makes it hard to know exactly what it is
about the sample code that is important to your question and which we
should be considering.

That said...

This has nothing to do with the delegate or event per se, though certainly
one approach relates to them. Assuming you've subscribed a handler to the
Click event for processing clicks on the buttons, you could just call the
handler directly. Alternatively, it's common to create a method that does
the real work and which is called from the event handler. You can also
call this method anywhere you need to in order to cause your code to react
as though the button was pushed even when it wasn't.

Alternatively, the ToolStripButton class has a PerformClick() method
inherited from ToolStripItem. It's a public method and you can call it to
cause a Click event to be raised.

There are lots of other ways around this sort of problem, but the above
are probably the most common ways of addressing it.

Pete
 
T

Tom

Pete --

The PerformClick() method was *exactly* what I needed!!


Definitely my mistake on the "private" vs "protected" access
specification. :(

Also, now I see I can't even spell "Please" in my title. :((

Thanks again, Pete.

-- Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top