c# WinForms - Showing hidden window from static reference - c#

I have problem with passing references to my windows in WinForms project and then retrieving them.
I have Login form when I create another (Main) form and hidding Login form in button event(I don't want to destroy Login reference here so I keep it in static class) :
private void btnLog_Click(object sender, EventArgs e)
{
if (CheckIfPasswordIsCorrect((int)DataSetUsers.Tables[0].Rows[cmbUsers.SelectedIndex][0]
, txtPassword.Text.Trim()))
{
GlobalData.LoginFormRef = this; //passing reference to Login form into static class
this.Hide(); //hidding Login form
MainForm mainForm = new MainForm();
mainForm.Show();
}
else
{
MessageBox.Show("Wrong password");
txtPassword.Focus();
}
}
GlobalData is static class with static object GlobalData.LoginFormRef so I can keep my Login form reference.
Then if I log out from my main form and want to back to login form I get an error or simply my application turns off :
private void btnLogOut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
GlobalData.LoggedUser = null;
if (GlobalData.LoginFormRef != null)
{
GlobalData.LoginFormRef.Show(); //trying to show my login form back
}
this.Close(); //closing Main form
this.Dispose(true); //destroying main form
}
When I'm closing my Main form and trying to invoke Login form back it disappears immediately or I'm getting an error System.ObjectDisposedException . I don't understand why. I have my reference to Login form in static class .... How can I destroy main form and show again Login form ?

Did you maybe start your application by using Application.Run(new MainForm())?
If you did, then it registered the closing event for MainForm with an event handler that closes the UI thread.
This will dispose all the forms including your login form (and will exit the application assuming you don't have any living non background threads).
See the remarks in msdn for Application.Run(Form): https://msdn.microsoft.com/en-us/library/ms157902(v=vs.110).aspx

Related

How to Open another Form without closing the whole Application c#

I have Forms (Admin, login, user). There is no error when accessing user form, it is simply opening it without user and pass.
The error I encountered is when I login and Admin form is present, when I click in my toolstripbutton the whole application exits.
This is the method I applied in every toolstripbutton:
public void CloseAllActiveForms()
{
List<Form> openForms = new List<Form>();
foreach (Form f in Application.OpenForms)
openForms.Add(f);
foreach (Form f in openForms)
{
if (f.Name != "FrmAdmin")
{
f.Close();
}
}
}
And this is the code in every toolstipbutton:
private void toolStripButton4_Click(object sender, EventArgs e)
{
CloseAllActiveForms();
FrmDashboard objFORM = new FrmDashboard();
objFORM.MdiParent = this;
objFORM.TopLevel = false;
objFORM.FormBorderStyle = FormBorderStyle.None;
objFORM.Dock = DockStyle.Fill;
pnlMain.Controls.Add(objFORM);
objFORM.Show();
}
This is design sample.
Every click in toolstripbutton supposed to go in pnlMain, but the problem occurs that after logging in and click one of the toolstripbutton it quits the whole windows application.
I tried to search about these and I found almost the same as my problem but the solution I think is not for my problem because I think it is for two forms only and the main form will be put in program.cs, but i have 2 and I think but not sure that my main form is the login. Please enlighten me.
Thank you
Because of this line in your program.cs
Application.Run(new FrmLogin());
the form FrmLogin is the main form of your application.
When the main form of an application is closed, the application is terminated. I suppose that after Login, you do not close but only hide the login form.
However, your code in CloseAllActiveForms will also close the hidden login form.
A quick solution is probably to skip all hidden forms in CloseAllActiveForms or explicitly skip FrmLogin in CloseAllActiveForms.
public void CloseAllActiveForms()
{
List<Form> openForms = new List<Form>();
foreach (Form f in Application.OpenForms)
openForms.Add(f);
foreach (Form f in openForms)
{
if (f.Visible && !(f is FrmAdmin) && !(f is FrmLogin))
{
f.Close();
}
}
}
There are alternative cleaner solutions:
Show the login form from the main form. See how to show login form on front & Mainform Behind Login Form?
Pass an ApplicationContext instead of a Form to Application.Run. This allows you to have more control over when the application is closed. See Windows Forms: create the main application after login, which form to run?

How to hide/close Current Form and opens a new form using a button inside the UserControl ? C#

This is how I made the main form
Basically, I want to open a new form (dashboard) using a button inside the usercontrol. The usercontrol is fill in the mainform's panel (login).
private void btnSignIn_Click(object sender, EventArgs e)
{
if (txtUsername.Text == "admin" && txtPassword.Text == "admin")
{
MessageBox.Show("Login Successfully");
DASHBOARD dashboard = new DASHBOARD();
dashboard.ShowDialog();
}
}
I did try this block of code but it won't close the mainform
MainForm mainform = new MainForm();
mainform.Hide();
DASHBOARD dashboard = new DASHBOARD();
dashboard.ShowDialog();
mainform.Close();
This code creates a new instance and closes it hidden. However, this doesn't affect the main window as they are not related.
MainForm mainform = new MainForm();
mainform.Hide();
mainform.Close();
this.ParentForm.Hide(); can complete the operation of hiding the main window.
usercontrol:
code:
using System;
using System.Windows.Forms;
namespace WindowsFormsControlLibrary1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "admin" && this.textBox2.Text == "admin")
{
MessageBox.Show("Login Successfully");
UserForm dashboard = new UserForm();
this.ParentForm.Hide();
dashboard.ShowDialog();
this.ParentForm.Close();
}
else
{
MessageBox.Show("Login Error");
}
}
}
}
Mainform:
Output:
This is a WinForms app, I take it. You can't close the Main Form without terminating the application. The only thing running above that is the Program static instance. If I read this right, the effect you're looking for can be achieved with Panels. A panel is just a control that contains other controls. You can have a panel for login and another for dashboard and switch between them by the simple expedient of which one you decide is on top at any given moment.
Another possible approach is to make a tiny hidden Main Form whose sole function is to spawn other forms but it is never actually displayed at all.
I'm curious, though. Why don't you put the dashboard in the main form, and when the main form has loaded, have it spawn a Modal Login Dialog? Once login is complete, destroy the login dialog and the dashboard is now your main display by default, or if login fails, then you can keep respawning the login dialog until the login succeeds or you decide that the user is a Bad Actor and you abort the application entirely. It seems the login is the transient function, so code it as such.

C# Switching forms with passed data in the constructor

I'm searching for the proper way how to open and switch forms some uses Application.Run( new Form1());, some uses Form1.ShowDialog(); and Form.Show();. I really want to know how to properly pass a data from a form into another using constructors.
Additionally I want to know why when I use Form.Close(); to close to current form before to open next form .... both forms closes.
Here are my codes.
try
{
Form2 f2 = new Form2(connection, userLogin);
this.Hide();
f2.ShowDialog();
}
catch (NullReferenceException nre) {
MessageBox.Show("Sorry Login Another User account");
}
What I'm trying to do here is to pass the MySqlConnection in the variable connection and the valid user in the variable userLogin into the Form2. This method works but I'm not sure if this the right way to do it.
Here are the codes in Form2.
public partial class Form2 : Form
{
MySqlConnection connection;
User activeUser;
public Form2(MySqlConnection pConn, User loginUser)
{
InitializeComponent();
connection = pConn;
activeUser = loginUser;
this.Init();
this.CenterToScreen();
}
private void logoutB_Click(object sender, EventArgs e)
{
this.Hide();
new Form1().Visible = false;
new Form1().ShowDialog();
//Application.Run(new Form1());
}
}
Showing the Form2 doesn't also have a problem but when the logout button is pressed.
Form that is already visible cannot be displayed as a modal dialog box.
Set the form's visible property to false before calling showDialog.
It says Form already visible? So does it mean the form is still open even though I used this.Hide();? and when I use the code Application.Run(new Form1());
Starting a second message loop on a single thread is not a valid operation.
Use Form.ShowDialog instead.
In this section of code:
new Form1().Visible = false;
new Form1().ShowDialog();
You are simply creating two new forms; you are creating a form in the first line with Visibilityproperty set to false; and in the second line you are creating a new Form and calling ShowDialog on it. So two instances are not the same.
this.Hide();
Form1 a = new Form1();
a.Visible = false;
a.ShowDialog();
Since you use ShowDialog to show Form2 from Form1, then simply close the second form when the user clicks the logout button.
private void logoutB_Click(object sender, EventArgs e)
{
this.Close();
}
This will bring execution back to Form1 right after the ShowDialog call. So, make sure then you re-show the form after you invoke ShowDialog like this:
try
{
Form2 f2 = new Form2(connection, userLogin);
this.Hide(); //hide my self
f2.ShowDialog(); //show Form2.
//Execution will resume here after Form2 is closed
this.Show(); //re-show my self
}
catch (NullReferenceException nre)
{
MessageBox.Show("Sorry Login Another User account");
}

Windows Forms Application - Closing login form exits application [duplicate]

I have two forms in my project (Login and Main).
What I'm trying to accoomplish is, if the login is successful, I must show the Main form and close the Login form.
I have this method in Login form that closes the Login form when the login is successful. But the Main form doesn't show.
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
var main = new Main();
main.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
I tried hiding the Login form if the login process is successful. But it bothers me because I know while my program is running the login form is still there too, it should be closed right?
What should be the right approach for this?
Thanks...
The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.
The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:
static void Main()
{
LoginForm fLogin = new LoginForm();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
I would do this the other way round.
In the OnLoad event for your Main form show the Logon form as a dialog. If the dialog result of that is OK then allow Main to continue loading, if the result is authentication failure then abort the load and show the message box.
EDIT Code sample(s)
private void MainForm_Load(object sender, EventArgs e)
{
this.Hide();
LogonForm logon = new LogonForm();
if (logon.ShowDialog() != DialogResult.OK)
{
//Handle authentication failures as necessary, for example:
Application.Exit();
}
else
{
this.Show();
}
}
Another solution would be to show the LogonForm from the Main method in program.cs, something like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LogonForm logon = new LogonForm();
Application.Run(logon);
if (logon.LogonSuccessful)
{
Application.Run(new MainForm());
}
}
In this example your LogonForm would have to expose out a LogonSuccessful bool property that is set to true when the user has entered valid credentials
This is my solution. Create ApplicationContext to set mainform of application and change mainform when you want to open new form and close current form.
Program.cs
static class Program
{
static ApplicationContext MainContext = new ApplicationContext();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainContext.MainForm = new Authenticate();
Application.Run(MainContext);
}
public static void SetMainForm(Form MainForm)
{
MainContext.MainForm = MainForm;
}
public static void ShowMainForm()
{
MainContext.MainForm.Show();
}
}
When login process is complete.
private void BtLogin_Click(object sender, EventArgs e)
{
//Login Process Here.
Program.SetMainForm(new Portal());
Program.ShowMainForm();
this.Close();
}
I hope this will help you.
It's simple.
Here is the code.
private void button1_Click(object sender, EventArgs e)
{
//creating instance of main form
MainForm mainForm = new MainForm();
// creating event handler to catch the main form closed event
// this will fire when mainForm closed
mainForm.FormClosed += new FormClosedEventHandler(mainForm_FormClosed);
//showing the main form
mainForm.Show();
//hiding the current form
this.Hide();
}
// this is the method block executes when main form is closed
void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
// here you can do anything
// we will close the application
Application.Exit();
}
This is the most elegant solution.
private void buttonLogin_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
this.Hide();
mainForm.ShowDialog();
this.Close();
}
;-)
Here's a simple solution, your problem is that your whole application closes when your login form closes right? If so, then go to your projects properties and on the Application Tab change the shutdown mode to "When last form closes" that way you can use Me.close without closing the whole program
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login();
}
private static bool logOut;
private static void Login()
{
LoginForm login = new LoginForm();
MainForm main = new MainForm();
main.FormClosed += new FormClosedEventHandler(main_FormClosed);
if (login.ShowDialog(main) == DialogResult.OK)
{
Application.Run(main);
if (logOut)
Login();
}
else
Application.Exit();
}
static void main_FormClosed(object sender, FormClosedEventArgs e)
{
logOut= (sender as MainForm).logOut;
}
}
public partial class MainForm : Form
{
private void btnLogout_ItemClick(object sender, ItemClickEventArgs e)
{
//timer1.Stop();
this.logOut= true;
this.Close();
}
}
I think a much better method is to do this in the Program.cs file where you usually have Application.Run(form1), in this way you get a cleaner approach, Login form does not need to be coupled to Main form, you simply show the login and if it returns true you display the main form otherwise the error.
Try this:
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
this.Close();
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Main));
t.Start();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
[STAThread]
public void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
You must call the new form in a diferent thread apartment, if I not wrong, because of the call system of windows' API and COM interfaces.
One advice: this system is high insecure, because you can change the if condition (in MSIL) and it's "a children game" to pass out your security. You need a stronger system to secure your software like obfuscate or remote login or something like this.
Hope this helps.
you should do it the other way round:
Load the mainform first and in its onload event show your loginform with showdialog() which will prevent mainform from showing until you have a result from the loginform
EDIT:
As this is a login form and if you do not need any variables from your mainform (which is bad design in practice), you should really implement it in your program.cs as Davide and Cody suggested.
best way for show login for and close login form before login successfully put login form in FrmMain after InitializeComponent.
public FrmMain()
{
FrmSplash FrmSplash = new FrmSplash();
FrmSplash.ShowDialog();
InitializeComponent();
//Login Section
{
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
this.Hide();
var main = new Main();
main.Show();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
try this
private void cmdLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "admin" || txtPassword.Text == "1")
{
FrmMDI mdi = new FrmMDI();
mdi.Show();
this.Hide();
}
else {
MessageBox.Show("Incorrect Credentials", "Library Management System", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
and when you exit the Application you can use
Application.Exit();
Evan the post is too old i like to give you a trick to do this if you wants to show splash/login screen and when the progress bar of splash screen get certain value/or successful login happen and closed the splash/login then re show the main form, frm-main will be the startup form not frm-spalash
in frm-main
public partial class frmMain : Form
{
public frmMain()
{
frmSplash frm = new frmSplash();
frm.Show(); // new splash screen will shows
this.Opacity=0; // will hide your main form
InitializeComponent();
}
}
in the frm-Splash
private void timer1_Tick(object sender, EventArgs e)
{
int cnt = progressBar1.Value;
switch (cnt)
{
case 0:
//Do sum stuff
break;
case 100:
this.Close(); //close the frm-splash
frmMain.ActiveForm.Opacity = 100; // show the frm-main
break;
}
progressBar1.Value = progressBar1.Value+1;
}
if you use it for login form
private void btlogin_Click(object sender, EventArgs e)
{
bool login = false;
//try your login here
//connect your database or whatever
//and then when it success update login variable as true
if(login == true){
this.Close(); //close the frm-login
frmMain.ActiveForm.Opacity = 100; // show the frm-main
}else{
//inform user about failed login
}
}
note that i use a timer and a progress bar to manipulate the actions you don't need those two it just for sake of complete answer only, hope this helps
1.Go to MainMenu Design properties> Go to my events
2.Click MainMenu event Shown and show LoginForm
3.Leave MainMenu as the First run in Program.cs

How can I close a login form and show the main form without my application closing?

I have two forms in my project (Login and Main).
What I'm trying to accoomplish is, if the login is successful, I must show the Main form and close the Login form.
I have this method in Login form that closes the Login form when the login is successful. But the Main form doesn't show.
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
var main = new Main();
main.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
I tried hiding the Login form if the login process is successful. But it bothers me because I know while my program is running the login form is still there too, it should be closed right?
What should be the right approach for this?
Thanks...
The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.
The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:
static void Main()
{
LoginForm fLogin = new LoginForm();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
I would do this the other way round.
In the OnLoad event for your Main form show the Logon form as a dialog. If the dialog result of that is OK then allow Main to continue loading, if the result is authentication failure then abort the load and show the message box.
EDIT Code sample(s)
private void MainForm_Load(object sender, EventArgs e)
{
this.Hide();
LogonForm logon = new LogonForm();
if (logon.ShowDialog() != DialogResult.OK)
{
//Handle authentication failures as necessary, for example:
Application.Exit();
}
else
{
this.Show();
}
}
Another solution would be to show the LogonForm from the Main method in program.cs, something like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LogonForm logon = new LogonForm();
Application.Run(logon);
if (logon.LogonSuccessful)
{
Application.Run(new MainForm());
}
}
In this example your LogonForm would have to expose out a LogonSuccessful bool property that is set to true when the user has entered valid credentials
This is my solution. Create ApplicationContext to set mainform of application and change mainform when you want to open new form and close current form.
Program.cs
static class Program
{
static ApplicationContext MainContext = new ApplicationContext();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainContext.MainForm = new Authenticate();
Application.Run(MainContext);
}
public static void SetMainForm(Form MainForm)
{
MainContext.MainForm = MainForm;
}
public static void ShowMainForm()
{
MainContext.MainForm.Show();
}
}
When login process is complete.
private void BtLogin_Click(object sender, EventArgs e)
{
//Login Process Here.
Program.SetMainForm(new Portal());
Program.ShowMainForm();
this.Close();
}
I hope this will help you.
It's simple.
Here is the code.
private void button1_Click(object sender, EventArgs e)
{
//creating instance of main form
MainForm mainForm = new MainForm();
// creating event handler to catch the main form closed event
// this will fire when mainForm closed
mainForm.FormClosed += new FormClosedEventHandler(mainForm_FormClosed);
//showing the main form
mainForm.Show();
//hiding the current form
this.Hide();
}
// this is the method block executes when main form is closed
void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
// here you can do anything
// we will close the application
Application.Exit();
}
This is the most elegant solution.
private void buttonLogin_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
this.Hide();
mainForm.ShowDialog();
this.Close();
}
;-)
Here's a simple solution, your problem is that your whole application closes when your login form closes right? If so, then go to your projects properties and on the Application Tab change the shutdown mode to "When last form closes" that way you can use Me.close without closing the whole program
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login();
}
private static bool logOut;
private static void Login()
{
LoginForm login = new LoginForm();
MainForm main = new MainForm();
main.FormClosed += new FormClosedEventHandler(main_FormClosed);
if (login.ShowDialog(main) == DialogResult.OK)
{
Application.Run(main);
if (logOut)
Login();
}
else
Application.Exit();
}
static void main_FormClosed(object sender, FormClosedEventArgs e)
{
logOut= (sender as MainForm).logOut;
}
}
public partial class MainForm : Form
{
private void btnLogout_ItemClick(object sender, ItemClickEventArgs e)
{
//timer1.Stop();
this.logOut= true;
this.Close();
}
}
I think a much better method is to do this in the Program.cs file where you usually have Application.Run(form1), in this way you get a cleaner approach, Login form does not need to be coupled to Main form, you simply show the login and if it returns true you display the main form otherwise the error.
Try this:
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
this.Close();
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Main));
t.Start();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
[STAThread]
public void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
You must call the new form in a diferent thread apartment, if I not wrong, because of the call system of windows' API and COM interfaces.
One advice: this system is high insecure, because you can change the if condition (in MSIL) and it's "a children game" to pass out your security. You need a stronger system to secure your software like obfuscate or remote login or something like this.
Hope this helps.
you should do it the other way round:
Load the mainform first and in its onload event show your loginform with showdialog() which will prevent mainform from showing until you have a result from the loginform
EDIT:
As this is a login form and if you do not need any variables from your mainform (which is bad design in practice), you should really implement it in your program.cs as Davide and Cody suggested.
best way for show login for and close login form before login successfully put login form in FrmMain after InitializeComponent.
public FrmMain()
{
FrmSplash FrmSplash = new FrmSplash();
FrmSplash.ShowDialog();
InitializeComponent();
//Login Section
{
public void ShowMain()
{
if(auth()) // a method that returns true when the user exists.
{
this.Hide();
var main = new Main();
main.Show();
}
else
{
MessageBox.Show("Invalid login details.");
}
}
try this
private void cmdLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "admin" || txtPassword.Text == "1")
{
FrmMDI mdi = new FrmMDI();
mdi.Show();
this.Hide();
}
else {
MessageBox.Show("Incorrect Credentials", "Library Management System", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
and when you exit the Application you can use
Application.Exit();
Evan the post is too old i like to give you a trick to do this if you wants to show splash/login screen and when the progress bar of splash screen get certain value/or successful login happen and closed the splash/login then re show the main form, frm-main will be the startup form not frm-spalash
in frm-main
public partial class frmMain : Form
{
public frmMain()
{
frmSplash frm = new frmSplash();
frm.Show(); // new splash screen will shows
this.Opacity=0; // will hide your main form
InitializeComponent();
}
}
in the frm-Splash
private void timer1_Tick(object sender, EventArgs e)
{
int cnt = progressBar1.Value;
switch (cnt)
{
case 0:
//Do sum stuff
break;
case 100:
this.Close(); //close the frm-splash
frmMain.ActiveForm.Opacity = 100; // show the frm-main
break;
}
progressBar1.Value = progressBar1.Value+1;
}
if you use it for login form
private void btlogin_Click(object sender, EventArgs e)
{
bool login = false;
//try your login here
//connect your database or whatever
//and then when it success update login variable as true
if(login == true){
this.Close(); //close the frm-login
frmMain.ActiveForm.Opacity = 100; // show the frm-main
}else{
//inform user about failed login
}
}
note that i use a timer and a progress bar to manipulate the actions you don't need those two it just for sake of complete answer only, hope this helps
1.Go to MainMenu Design properties> Go to my events
2.Click MainMenu event Shown and show LoginForm
3.Leave MainMenu as the First run in Program.cs

Categories

Resources