I’m new to Winforms. I’m using VS2022 and .NET 6 to create a Winforms app.
I created two forms (loginForm
and MainForm
). I’m trying to use the loginForm
to protect data in MainForm
. When user logs in, the loginForm
should be hidden and show MainForm
, so user can perform some actions.
When user finishes his actions, he should click logout, which shows the loginForm
again, and hides the MainForm
. Please help
5
You could try the example below.
I tested the following steps and it works.
click loginin -> show MainForm -> Click Hide MainForm -> show LoginForm->click login out -> then click loginin again -> show previously-hidden MainForm
internal static class Program
{
private static Form1 mainForm;
private static LoginForm loginForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainForm = new Form1();
loginForm = new LoginForm();
loginForm.Login += LoginForm_Login;
loginForm.Logout += LoginForm_Logout;
loginForm.Show();
mainForm.Invisible += MainForm_FormInvisible;
Application.Run();
}
private static void MainForm_FormInvisible(object sender, EventArgs e)
{
mainForm.Hide();
loginForm.Show();
}
private static void LoginForm_Login(object sender, EventArgs e)
{
loginForm.Hide();
if (mainForm.IsDisposed)
{
mainForm = new Form1();
}
mainForm.Show();
}
private static void LoginForm_Logout(object sender, EventArgs e)
{
mainForm.Hide();
loginForm.Show();
}
}
LoginForm(loginbutton and logout button):
public partial class LoginForm : Form
{
public event EventHandler Login;
public event EventHandler Logout;
public LoginForm()
{
InitializeComponent();
}
private void OnLogin()
{
Login?.Invoke(this, EventArgs.Empty);
}
private void OnLogout()
{
Logout?.Invoke(this, EventArgs.Empty);
}
private void btnLogin_Click(object sender, EventArgs e)
{
OnLogin();
}
private void btnLogout_Click(object sender, EventArgs e)
{
OnLogout();
}
//exit button
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
MainForm((textbox and Hide button)):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public event EventHandler Invisible;
private void button1_Click(object sender, EventArgs e)
{
OnHide();
}
private void OnHide()
{
Invisible?.Invoke(this, EventArgs.Empty);
}
}
3
You can try this:
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
while (true)
{
var dlg = new LoginForm();
if (dlg.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else if (dlg.ShowDialog() == DialogResult.Abort)
{
break;
}
}
}
and in LoginForm, provide two results
for success login: this.DialogResult=DiaglogResult.OK;
for exiting application: this.DialogResult=DiaglogResult.Abort;
1
Your code seems almost correct for switching between two Windows Forms in C#. However, there’s a mistake in the Form2
class where you’re creating an instance of Form1
instead of Form2
when switching back to Form1
.
Here’s the corrected version of your code:
namespace SwitchBetweenWinfomToWinfomApp
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
namespace SwitchBetweenWinfomToWinfomApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSwitchToForm2_Click(object sender, EventArgs e)
{
// Create an instance of Form2
Form2 form2 = new Form2();
// Show Form2
form2.Show();
// Hide the current form (Form1)
this.Hide();
}
}
}
namespace SwitchBetweenWinfomToWinfomApp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnSwitchToForm1_Click(object sender, EventArgs e)
{
// Create an instance of Form1
Form1 form1 = new Form1();
// Show Form1
form1.Show();
// Hide the current form (Form2)
this.Hide();
}
}
}
2