Home All Groups Group Topic Archive Search About

Adding a button click event to my Main function

Author
10 Mar 2006 4:38 PM
lavu
I am trying to start a C# GUI App and automatically trigger a button
click event.
However when I add a statement  btn.PerformClick to my Main() I get an
error that a class was expected .

This is what I am trying to do.
static void Main(string[] args)
{
........
Application.Run(new frmMain());
btnTest.PerformClick();
}

and the error is

MyApp.frmMain.btnTest denotes a 'field' where a 'class' was expected.

I do not want to add the code to frmMain() since GUI comes up only
after the button click has already been executed. Waht would be the
best way to do it ?

Any ideas are appreciated.

Author
10 Mar 2006 4:49 PM
Marc Gravell
It really depends what is happening in the Click event-handler, but to do
what you want /without the form/, you need to (as somebody suggested the
other day) refactor this bit of code out into a separate method, which you
can call either on it's own, or via the UI; example:

**old**
private void btn_Click(object sender, EventArgs e) {
  // do lots of things
}

**new**
private void btn_Click(object sender, EventArgs e) {
  DoLotsOfThings();
}

internal static void DoLotsOfThings() {
  // do lots of things
}

then in your Main() you can call WhateverClass.DoLotsOfThings();

*however*, if your code (aka // do lots of things) is genuinely working with
the form (instance) elements, than you will struggle to move it out without
a lot more work.

Note that the VS2005 IDE can help with most of this just by selecting the
code you want to split out, right-click, refactor, extract method..., give
it a name - job done.

Marc


Show quoteHide quote
"lavu" <thrik***@yahoo.com> wrote in message
news:1142008722.268702.268880@i39g2000cwa.googlegroups.com...
>I am trying to start a C# GUI App and automatically trigger a button
> click event.
> However when I add a statement  btn.PerformClick to my Main() I get an
> error that a class was expected .
>
> This is what I am trying to do.
> static void Main(string[] args)
> {
> .......
> Application.Run(new frmMain());
> btnTest.PerformClick();
> }
>
> and the error is
>
> MyApp.frmMain.btnTest denotes a 'field' where a 'class' was expected.
>
> I do not want to add the code to frmMain() since GUI comes up only
> after the button click has already been executed. Waht would be the
> best way to do it ?
>
> Any ideas are appreciated.
>
Are all your drivers up to date? click for free checkup

Author
10 Mar 2006 7:27 PM
lavu
Marc, thanks  for your suggestion.
However my code works a bit with form elements and using VS2005 is not
an option for my project right now.
So I will have to keep  looking for some alternate method.

Bookmark and Share