Reputation: 1351
When I open a new form from my login screen, it logs the user in, shows the form then instantly disappears???
Here is my code for the relevant parts:
string checkAdmin = "select * from Logins where Name = @userName and UserType ='Admin'";
string checkUser = "select * from Logins where Name = @userName and UserType ='User'";
using (var connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginsTest;Trusted_Connection=yes"))
{
using (var checkAdminCommand = new SqlCommand(checkAdmin, connection))
{
//guessing at the column length here. Use actual column size instead of 20
checkAdminCommand.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = userNameBox.Text;
connection.Open();
if (checkAdminCommand.ExecuteScalar() != null)
{
adminScreen admnscrn = new adminScreen();
admnscrn.Show();
this.Close();
return;
}
else
{
}
}
using (var connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginsTest;Trusted_Connection=yes"))
{
using (var checkUserCommand = new SqlCommand(checkUser, connection))
{
//guessing at the column length here. Use actual column size instead of 20
checkUserCommand.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = userNameBox.Text;
connection.Open();
if (checkUserCommand.ExecuteScalar() != null)
{
userScreen usrscrn = new userScreen();
usrscrn.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid details, try again");
passwordBox.Text = "";
}
}
}
So when I open usrscrn or admnscrn, it flicks up and goes straight back off again? I've tried researching, I've used Application.Run(userScreen()); etc, it just throws an exception saying something about a second message loop?
I have created a new screen, set it as MDI, and entered this code:
private void MDI_Form_Load(object sender, EventArgs e)
{
Form1 loginscrn = new Form1();
loginscrn.Show();
}
my program.cs file contains this code:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDI_Form());
}
will this work??
Upvotes: 1
Views: 3417
Reputation: 11252
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
This is the boilerplate bootstrap code in most Windows Forms projects. It creates a Form which should act as your main window and it calls Application.Run
on it. This causes it to run a message loop which allows the Form to execute in an event-driven manner (waiting for user input). When you call Close
on this main form, your Application.Run
method returns and your Main
method runs to completion which causes your program to exit.
It sounds to me like what you want to do is show a form like a splash screen or login window, then when it closes you want to show another form and run the second form as your main form.
To do that, there are at least a couple approaches:
Main
method so that it creates the login form, calls Application.Run
on it and waits for it to close, then analzes the result through a property such as the DialogResult
property to know whether it was a success. Then it could later create your main form and again call Application.Run
on it.In this case your code might look like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm loginForm = new LoginForm();
Application.Run(loginForm);
if (loginForm.DialogResult == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
// Error handling
}
}
Main
method to run the main form instead of the login form, then have the main form show the login window as a dialog using the Form.ShowDialog
method. You could try doing this before the main form is even shown yet, or have the main form Visible
property set to false so that it appears that your login window is actually the main window.Here is how you'd do that:
private void MainForm_Load(object sender, EventArgs e)
{
this.Visible = false;
using (var loginForm = new LoginForm())
{
if (loginForm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
this.Visible = true;
}
else
{
// Error handling
Close();
}
}
}
You just need to make sure that your LoginForm correctly sets DialogResult = DialogResult.OK
or something else as needed to signal a successful login.
Upvotes: 3
Reputation: 766
You need MDI Form (Multi Document Interface) that will let other forms to be inside it , so when you close one form it will not terminate all the application .
Or make your login form not the starup form , by creating another hidden form and make it the startup one
Upvotes: -2