473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Adding a button click event to my Main function

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.

Mar 10 '06 #1
2 5780
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
"lavu" <th******@yahoo.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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.

Mar 10 '06 #2
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.

Mar 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Picho | last post by:
Hi all, Is there any .NET way (I am not rulling out API usage) to add button(s) to a form's title bar? I found some non-.NET solutions that did actually work in VB6 but not in the ..NET...
2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
3
by: Philip Townsend | last post by:
I have an aspx page that contains 2 user controls, each containing a seperate textbox and button. I would like to specify that one of the buttons recieve focus when the page loads. Also, I would...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
0
by: Klaus Jensen | last post by:
Hi! This has been annoying me for a while now, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
21
by: John Henry | last post by:
Hi list, I have a need to create class methods on the fly. For example, if I do: class Dummy: def __init__(self): exec '''def method_dynamic(self):\n\treturn self.method_static("it's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.