Calling another method in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I call the "buttonRun_Click()" method in C#?

I can do this so easily in VB but I have yet to figuere out how in C#. I
keep getting errors.

Thanks
CM
 
CM,

Well, how are you trying to call it? What is the error you are getting?
 
How do I call the "buttonRun_Click()" method in C#?

I can do this so easily in VB but I have yet to figuere out how in C#. I
keep getting errors.

And in addition to what Nicholas wrote, what do you mean "call it"?
Even in VB, given that the method name looks a lot like an event
handler, you shouldn't be calling it explicitly. You would just
subscribe it to the Click event on the Button control in question.

You can do this explicitly in your own code using the "+" operator on
the event:

Button btn = ...;

btn.Click += buttonRun_Click();

or even better, just use the VS Designer to hook it up by assigning it
in the properties inspector for the control (you'll have to click on
the lightning-bolt button to show the events for the control).

The former is significantly different from the VB paradigm, but the
latter should be basically the same AFAIK in either language (and will
auto-generate the appropriate code, whether you're writing VB or C#).

Pete
 
I am sorry. For some reason I have a total brain cramp with this. ......
I am trying to in the code below have another buttons click event fired when
text is changed:
private void toolStripStatusLabel3_TextChanged(object sender, EventArgs e)
{
// here is where I want to fire the buttonRun event

}
 
CMartin said:
How do I call the "buttonRun_Click()" method in C#?

I can do this so easily in VB but I have yet to figuere out how in C#. I
keep getting errors.

Thanks
CM

Why dont you put the code in button event method into a different method
and then call this new method when the button event is fired. You can
then call the new method from anywhere in the class, or if you make it
public from anywhere.

buttonRun_Click()
{
NewMethod();
}

private void NewMethod()
{
blar blar
}
 
I have a lot of code in this button click event:
private void buttonRun_Click(object sender, EventArgs e)

Now I want to trigger that event above when someting happens in this text
changed event:
private void toolStripStatusLabel3_TextChanged(object sender, EventArgs e)

I just do not understand whi I cannot simply invoke the button_click
routine. liek below:
private void toolStripStatusLabel3_TextChanged(object sender, EventArgs e)
{
buttonRun_click():
}

But it does not liek it and I am very confused. In VB, this was soooo easy.
 
First off - you can simply set the toolStripStatusLabel3.TextChanged
event to fire buttonRun_Click; either in the IDE by using the pull-
down menu, or through code:
toolStripStatusLabel3.TextChanged += buttonRun_Click;

As for the specific question - you need to give it the args; try:
buttonRun_click(sender, e);

But if that is *all* that toolStripStatusLabel3_TextChanged does, then
the first (shared handler) approach is simpler.

Marc
 
buttonRun_click(sender, e);

or, clearer:

private void DoSomethingIntersting() {
// ...common code
}
private void buttonRun_Click(object sender, EventArgs e) {
// ...code specific to buttonRun
// then:
DoSomethingIntersting();
}
private void toolStripStatusLabel3_TextChanged(object sender,
EventArgs e) {
// ...code specific to toolStripStatusLabel3
// then:
DoSomethingIntersting();
}
 
Couldn't be any easier:

private void toolStripStatusLabel3_TextChanged(object sender, EventArgs e)
{
buttonRun.PerformClick();
}
 

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

Back
Top