|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Adding a button click event to my Main functionclick 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. 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. >
Other interesting topics
Anonymous Methods As ThreadStarts
Increase all digits in a number(string) one step Sockets, to be continued... string dictionary and memory issue. Refering to Object Members in A Collection in an ASP.NET BoundColu finding the current user name accessing an ASP.NET page Form Minimize What is wrong with this Producer-Consumer sample? Desing problem Regular Expresions |
|||||||||||||||||||||||