{
MainForm form=new MainForm();
form.ShowDialog();
this.Close();
}//输入成功,登录到主窗口
能成功登录,但登陆后的登录界面还有,请问登录成功后怎么关闭登录界面!
2 楼的说得对,你如果在MainForm函数里面用new的话,那个窗口就关了。
我再发一个完整的给你:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace BookManagement
{
/// <summary>
/// Login 的摘要说明。
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOk;
//private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.TextBox textUserID;
private System.Windows.Forms.TextBox textUserPassword;
private System.Data.SqlClient.SqlConnection sqlConnection1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Login()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btnOk = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.textUserID = new System.Windows.Forms.TextBox();
this.textUserPassword = new System.Windows.Forms.TextBox();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("华文隶书", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(48, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 23);
this.label1.TabIndex = 0;
this.label1.Text = "登 陆";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "用户名:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 2;
this.label3.Text = "密码:";
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(24, 168);
this.btnOk.Name = "btnOk";
this.btnOk.TabIndex = 3;
this.btnOk.Text = "登录";
this.btnOk.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.btnOk_KeyPress);
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(144, 168);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "重置";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// textUserID
//
this.textUserID.Location = new System.Drawing.Point(120, 64);
this.textUserID.Name = "textUserID";
this.textUserID.TabIndex = 5;
this.textUserID.Text = "";
//
// textUserPassword
//
this.textUserPassword.Location = new System.Drawing.Point(120, 112);
this.textUserPassword.Name = "textUserPassword";
this.textUserPassword.TabIndex = 6;
this.textUserPassword.Text = "";
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=LUOFANG;packet size=4096;integrated security=SSPI;data source=LUOF" +
"ANG;persist security info=False;initial catalog=BookManagement";
//
// Login
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 221);
this.Controls.Add(this.textUserPassword);
this.Controls.Add(this.textUserID);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Login";
this.Text = "图书出租管理系统";
this.Load += new System.EventHandler(this.Login_Load);
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Login());
}
private void btnCancel_Click(object sender, System.EventArgs e)
{ //点“清除”按纽后,全部清空
this.textUserID.Text="";
this.textUserPassword.Text="";
}
private void btnOk_Click(object sender, System.EventArgs e)
{
if(UserCheck(this.textUserID.Text,this.textUserPassword.Text)=="system")
{ //调用UserCheck函数,如果用户名跟密码与“system“相应
this.Visible=false; //获取或设置一个值,该值指示是否显示控件;此处表示Login窗口不可见了
Form mainform=new MainForm(this.textUserID.Text,"system");
mainform.ShowDialog();//打开MainForm控件,成员为system
this.Close(); //Login窗口关系
}
else if(UserCheck(this.textUserID.Text,this.textUserPassword.Text)=="user")
{ //调用UserCheck函数,如果用户名跟密码与“user“相应
this.Visible=false;
Form mainform=new MainForm(this.textUserID.Text,"user");
mainform.ShowDialog(); ////打开MainForm控件,成员为user
this.Close();
}
else
{
if(MessageBox.Show("输入用户密码有误,是否重新登陆","输入有误",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{ //否则提示出错并清空
this.textUserID.Clear();
this.textUserPassword.Clear();
//this.textUserID.();
}
else
{
this.Close();
}
}
}
string UserCheck(string username,string userpassword)
{ //UserCheck函数,注意返回值为usersort!!!
string txtSql,usersort;
usersort="nobody";
txtSql = "SELECT * FROM [User]";
SqlCommand checkuser=new SqlCommand(txtSql,this.sqlConnection1);
try
{
this.sqlConnection1.Open();
SqlDataReader sqlreader=checkuser.ExecuteReader();
while(sqlreader.Read())
{ //sqlreader[0]对应用户名、sqlreader[1]对应用户密码、sqlreader[2]对应用户类型
if((sqlreader[0].ToString().Trim()==username)&&(sqlreader[1].ToString().Trim()==userpassword))
{
usersort=sqlreader[2].ToString().Trim();
break;
}
}
}
catch(Exception e)
{
//Console.WriteLine(e.ToString());
MessageBox.Show(e.ToString());
}
sqlConnection1.Close();
//return "nobody";
return usersort; //返回值为usersort,代码在{}之外
}
private void Login_Load(object sender, System.EventArgs e)
{
//this.sqlConnection1.Open();
//this.sqlDataAdapter1.Fill(this.dataSetUser1);
}
private void btnOk_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{ //设置敲了“Enter”以后,实现登陆
/* if(Key==#13)
{
btnOk_Click(Sender);
}
*/
}
}
}
为什么我的登陆后瞬间2个窗口都消失了????我的登陆界面是一个窗口,然后输入预先设定的一个密码,该密码用一个textbox的text属性表示了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Enter
{
public partial class Myworld : Form
{
private bool isEnter = false;
public Myworld()
{
InitializeComponent();
}
public void EnterPassword()
{
try
{
if (txtEnerPassword.Text == textPassword.Text)
{
isEnter = true;
toolStripStatusLabel.ForeColor = Color.Black;
toolStripStatusLabel.Text = "Wait for seconds";
}
else
{
toolStripStatusLabel.Text = "The password is wrong!";
toolStripStatusLabel.ForeColor = Color.Red;
txtEnerPassword.Text = "";
}
}
catch (Exception ex)
{
toolStripStatusLabel.Text = ex.Message;
}
}
public void handleProBarApp()
{
toolStripProgressBar.Value += 10;
if (toolStripProgressBar.Value == toolStripProgressBar.Maximum)
{
MainWindow newMainWin = new MainWindow();
newMainWin.Show();
timer.Enabled = false;
//this.Close(); //如果加上这句话,2个窗口刚创立就同时消失!
}
}
private void btnEnter_Click(object sender, EventArgs e)
{
EnterPassword();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void timer_Tick(object sender, EventArgs e)
{
if (isEnter)
handleProBarApp();
}
}
}