Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click a button in code

Status
Not open for further replies.

pdbowling

Programmer
Mar 28, 2003
267
US
Hi all.
I am inheriting a form and the StartButton_Click() event is private.

Is there a way to click the button in code as soon as the application starts up? I thought that I could access private members since I am inheriting the class's functionality but I must be mistaken. StartButton_Click() does not show up in 'Code Complete'.

StartButton.Click() is expecting the += delegation so that didn't work, either.

Thanks,everyone.
Patrick

Gather or post content for free.
 
You have a form that responds to a click event and you want to call that event from a different part of your code?

Why is your button doing work rather than a controller(other class) of some sort. Even a different method would be good.

Example:

private void StartButtonClick(object sender, EventArgs e)
{
PerformAnAction();
}

private void Form_Shown(object sender, EventArgs e)
{
PerformAnAction();
}

private void PerformAnAction()
{
//Do your work here!
}



If you find that you absolutely need to make the button click then you can either call the method directly "this.StartButton_Click(StartButton, EventArgs.Empty)" or invoke the click

this.Invoke(new EventHandler(StartButton_Click), new object[] {EventArgs.Empty});


As an added note - your form should never be responsible for doing work. It should only be a view of data that exists in another class. Take the time to google the Model View Controller design pattern.

Hope that helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top