我有一个 Windows 应用程序,其中我的第一个 Windows 窗体是登录。
成功登录后,必须打开“主页”表单。
我在调试时看到“Home”表单,但是一旦代码进入 Home.Designer.cs 中的 Dispose 方法,我的应用程序就会停止。
我的登录页面代码如下所示:
private void loginbtn_Click(object sender, EventArgs e)
{
String username = "admin";
String password = "admin";
String @uname = Unametxtbox.Text;
String @pass = Passtextbox.Text;
if (@uname.Equals(username) && @pass.Equals(password))
{
MessageBox.Show("Login Successful");
Home home = new Home();
home.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid Credentials!");
}
}
我的 Home.cs 页面如下所示:
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
}
Home.Designer.cs 有以下代码:
partial class Home
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Home));
this.label1 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.closebtn = new System.Windows.Forms.PictureBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.Storedbtn = new System.Windows.Forms.Button();
this.Soldbtn = new System.Windows.Forms.Button();
this.Transbtn = new System.Windows.Forms.Button();
this.Supbtn = new System.Windows.Forms.Button();
this.Empbtn = new System.Windows.Forms.Button();
this.Custbtn = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.closebtn)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox closebtn;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button Storedbtn;
private System.Windows.Forms.Button Soldbtn;
private System.Windows.Forms.Button Transbtn;
private System.Windows.Forms.Button Supbtn;
private System.Windows.Forms.Button Empbtn;
private System.Windows.Forms.Button Custbtn;
}
如果我评论 this.Close();在loginbtn_Click中,我可以看到Windows窗体,但登录Windows窗体没有关闭。
我在这里缺少什么,提前致谢。
尝试使用隐藏而不是关闭。
Hide();
Home home = new Home();
home.ShowDialog();
Close();
了解更多信息您可以访问微软官方site https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.close?view=windowsdesktop-7.0.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)