PDA

Click to See Complete Forum and Search --> : [2.0] Forms Question.


thegreatone
Mar 24th, 2006, 11:05 PM
Ok, this is really embarrassing, but i'm new to C# and i'm just porting over an application from VB (It's the first EVER port i've done, going smoothly actually)

I'm stuck on form manipulation so to speak, whenever my start-up form is closed, even if i open another form my appliacation exits, is this standard behaviour in the C# environment ? if so, is there a way around it ?

The first form to load is labelled "Form2" and shortly after i launch either Form1, or Form3, depending on a users choice. However when i try and open Form1 or 3 and close Form2, my entire application closes.

Here is my start...

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form frm2 = new Form2();
Application.Run(frm2);
}
}
}


And when my Form2 closes this is how...

private void label3_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.Show();
this.Close(); //For some reason it closes the entire application, not just the form, as documented.
}


Any help would be appreciated. :thumb:

jmcilhinney
Mar 25th, 2006, 06:59 AM
It's basically the same behaviour as VB.NET, although the application framework in VB 2005 allows you to specify that the application will not close until the last form closes. C# doesn't benefit from that feature though. VB.NET or C#, when you call Application.Run and pass a reference to a form instance it will block until that form instance is disposed. Your calling Close on that form diposes it so Application.Run returns, the Main method completes and your app exits. I've provided a VB.NET CodeBank submission (http://www.vbforums.com/showthread.php?t=353165) that allows you to switch main forms mid-stream that could easily be ported to C#. It will only work if you want to close the current main form and switch to a new main form that hasn't been displayed yet. I feel like there should be a way to handle the situation where you want to switch to a form that has been displayed already but I haven't found it yet.

wossname
Mar 25th, 2006, 08:53 AM
Well don't use Application.Run() then.

Just write it the way you want it to run. Application.Run is nothing special, its not actually required.
void Main() is not a special method either its just like any other. You tell it what to do not the other way around :D You can stick some extra code after Run() that instantiates more forms as you wish.

C# doesn't benefit from that feature though.
Yes it does. Check out ...

using(...)
{
//blah
}

;)

thegreatone
Mar 25th, 2006, 09:47 AM
I obviously don't understand.

I tried this, thinking that it would be ok, based on leaving Application.Run out of it... Instead it shows the form breifly then exits. Where am i going wrong ?


static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form frm2 = new Form2();
frm2.Show();
//Application.Run(frm2);
}

jmcilhinney
Mar 25th, 2006, 07:04 PM
Yes it does. Check out ...

using(...)
{
//blah
}

;)I think you misunderstand what I mean. The application framework in a VB 2005 app is implemented through the My.MyApplication class. You choose to enable the application framework or not in the project properties. It automatically controls things like visual styles, single instance applications and shutdown mode, as well as providing application events. These things are still handled through a Main method in C# 2005. This is part of the new .NET way of making VB easier to use, and is an area where VB has it over C#. In VB 2005 the answer to this question would amount to selecting an option from a drop-down list.