Open other form and close existing

R

RP

I have a login form. I want to open the main MDI form if the login was
successful and want to close the login form. I tried below code, but
it is not working:
======================================
private void OK_Click()
{
//If login successful
try
{
MainForm MainWindow = new MainForm();
MainWindow.Show();
}
Catch (Exception ex)
{
.................
}
Finally
{
this.close();
}
}
=======================================
In the above code, this represents Login Form. But it closes the next
form i.e., the MainForm since we initialized its instance above. How
to close the Login Form and open the MainForm.
 
N

Nicholas Paldino [.NET/C# MVP]

RP,

You should have two calls to the static Run method in your program. The
first will show the login form, which will have a property indicating
whether or not the login was successful. The second will run the main
program form, assuming that it was successful, like so:

// The login form.
using(LoginForm login = new LoginForm())
{
// Show the form.
Application.Run(login);

// If the login was successful, then dispose of the old form, and then
// run the new one.
if (login.Success)
{
// Dispose the old form.
login.Dispose();

// Run the new form.
using (MainForm mainForm = new MainForm())
{
Application.Run(new MainForm());
}
}
}
 
R

Roman Wagner

Which form you use in the Application.Run call?
Is it you login form? If so do the following:

Use a new Instance of MainForm as parameter for Application.Run

Subscribe to the Load Event of the MainForm u can do this in the ctor
of MainForm
inside the eventhadler do the following

LoginForm login = new LoginForm();
if(login.ShowDialog != DialogResult.OK)
{
Application.Exit();
}
 
A

Arnshea

I have a login form. I want to open the main MDI form if the login was
successful and want to close the login form. I tried below code, but
it is not working:
======================================
private void OK_Click()
{
//If login successful
try
{
MainForm MainWindow = new MainForm();
MainWindow.Show();
}
Catch (Exception ex)
{
.................
}
Finally
{
this.close();
}}

=======================================
In the above code, this represents Login Form. But it closes the next
form i.e., the MainForm since we initialized its instance above. How
to close the Login Form and open the MainForm.

If you just want to close a login window then open the main window you
probably don't have to use MDI.

The visual studio help has a document that may help you - "Developing
with visual studio.net - Creating Windows Applications - Windows Forms
- Creating Windows Forms - Making a Startup Window Form Invisible". A
code sample from that help document is below:

// C#
// All methods must be contained in a class.
// This class is added to the namespace containing the Form1 class.
class MainApplication
{
public static void Main()
{
// Instantiate a new instance of Form1.
Form1 f1 = new Form1();
// Display a messagebox. This shows the application
// is running, yet there is nothing shown to the user.
// This is the point at which you customize your form.
System.Windows.Forms.MessageBox.Show("The application "
+ "is running now, but no forms have been shown.");
// Customize the form.
f1.Text = "Running Form";
// Show the instance of the form modally.
f1.ShowDialog();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top