J
Jon Hyland
Ok, I'm a little rusty on this, it should be a simple problem but I
can't figure it out. How can I handle form events in my main code
page??
I'm creating a Windows App in C#. Rather than make my main form the
startup object, I'd rather put my Main() function in a class or code
file. Why? Because thats what I always do in standard C++ or VB. In
my opinion, writing all my application logic in the form itself is
sloppy and only for quick and dirty apps.
So what I did was create a class called MyMain, put my Main() function
in it, and set my startup object to that class. In the Main()
function I am creating an instance of my main form (MainForm) and
showing it.
Here's my Main funciton (in my class)
//main entry point for application
[STAThread]
static void Main()
{
//init the context
context = new ApplicationContext();
//set the idle event handler
Application.Idle += new EventHandler(AppIdle);
//any other inits
//..
//display main form
if(context.MainForm == null)
{
try
{
context.MainForm = new NexusForm();
context.MainForm.Show();
}
catch (Exception error)
{
MessageBox.Show(error.Message, "", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
Application.Exit();
}
}
}
When I run the application, the main form is displayed as expected.
Also, when I close the form, the application closes. My question is
this... I want to be able to fire off other functions in my class
(MyMain) from the Form events. For example, if I had a command
button, and I clicked on it, it would fire off the event handler for
that button. From that function all I want to do is call the real
function, which would reside in my main class (its parent object). I
don't want any functionality in this event.
I can't create an instance of my main class from the form -- the form
is already a child of that class. How would I do it? I tried
something like this:
this.Parent.MyRealFunction();
however, it doesn't work.
All the examples I can find concerning windows forms in .NET are
putting all the application logic in their main form. This does not
help me, I want my app independant of whatever forms I chose to
instanciate.
Thanks in advance,
Jon
can't figure it out. How can I handle form events in my main code
page??
I'm creating a Windows App in C#. Rather than make my main form the
startup object, I'd rather put my Main() function in a class or code
file. Why? Because thats what I always do in standard C++ or VB. In
my opinion, writing all my application logic in the form itself is
sloppy and only for quick and dirty apps.
So what I did was create a class called MyMain, put my Main() function
in it, and set my startup object to that class. In the Main()
function I am creating an instance of my main form (MainForm) and
showing it.
Here's my Main funciton (in my class)
//main entry point for application
[STAThread]
static void Main()
{
//init the context
context = new ApplicationContext();
//set the idle event handler
Application.Idle += new EventHandler(AppIdle);
//any other inits
//..
//display main form
if(context.MainForm == null)
{
try
{
context.MainForm = new NexusForm();
context.MainForm.Show();
}
catch (Exception error)
{
MessageBox.Show(error.Message, "", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
Application.Exit();
}
}
}
When I run the application, the main form is displayed as expected.
Also, when I close the form, the application closes. My question is
this... I want to be able to fire off other functions in my class
(MyMain) from the Form events. For example, if I had a command
button, and I clicked on it, it would fire off the event handler for
that button. From that function all I want to do is call the real
function, which would reside in my main class (its parent object). I
don't want any functionality in this event.
I can't create an instance of my main class from the form -- the form
is already a child of that class. How would I do it? I tried
something like this:
this.Parent.MyRealFunction();
however, it doesn't work.
All the examples I can find concerning windows forms in .NET are
putting all the application logic in their main form. This does not
help me, I want my app independant of whatever forms I chose to
instanciate.
Thanks in advance,
Jon