Main() ->Login验证成功 - >Form1运行+Login窗体关闭但不知道怎么关login
如果在这里加代码:
if((name==reader["name"].ToString())&&(pwd==reader["password"].ToString()))
{
new Form1().ShowDialog();
this.Close();//这里
}
如果这样的话,不止是login关闭,整个程序也全部关闭了,想知道为什么,不需要您再给我写一遍代码,只想知道哪错了,为什么错了,怎么解决,谢谢!
代码如下:
//Form1
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace CC1
{
public class Form1 : Base
{
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent(); }
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 401);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
//主函数
using System;
using System.Windows.Forms;
namespace CC1
{
public class Start
{
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.Run(new Login());
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
//数据库操作函数
using System;
using System.Data;
using System.Data.OleDb;namespace CC1
{
public class Data
{
private OleDbConnection conn=new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;"+
"Data source=../../cc.mdb");
public Data()
{
}
public DataSet GetDs(string sql)
{
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
DataSet ds=new DataSet("ds");
da.Fill(ds);
return ds;
}
public OleDbDataReader GetReader(string sql)
{
conn.Open();
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader reader=cmd.ExecuteReader();
return reader;
}
public void DisConnect()
{
conn.Close();
}
}
}
如果在这里加代码:
if((name==reader["name"].ToString())&&(pwd==reader["password"].ToString()))
{
new Form1().ShowDialog();
this.Close();//这里
}
如果这样的话,不止是login关闭,整个程序也全部关闭了,想知道为什么,不需要您再给我写一遍代码,只想知道哪错了,为什么错了,怎么解决,谢谢!
代码如下:
//Form1
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace CC1
{
public class Form1 : Base
{
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent(); }
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(720, 401);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
//主函数
using System;
using System.Windows.Forms;
namespace CC1
{
public class Start
{
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.Run(new Login());
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
//数据库操作函数
using System;
using System.Data;
using System.Data.OleDb;namespace CC1
{
public class Data
{
private OleDbConnection conn=new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;"+
"Data source=../../cc.mdb");
public Data()
{
}
public DataSet GetDs(string sql)
{
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
DataSet ds=new DataSet("ds");
da.Fill(ds);
return ds;
}
public OleDbDataReader GetReader(string sql)
{
conn.Open();
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader reader=cmd.ExecuteReader();
return reader;
}
public void DisConnect()
{
conn.Close();
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;namespace CC1
{
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.TextBox loginName;
private System.Windows.Forms.TextBox loginPwd;
private System.Windows.Forms.Button loginbtnLogin;
private System.Windows.Forms.Button loginbtnExit;
private System.Windows.Forms.ToolTip toolTip1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.PictureBox pictureBox1; public Login()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Login));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.loginName = new System.Windows.Forms.TextBox();
this.loginPwd = new System.Windows.Forms.TextBox();
this.loginbtnLogin = new System.Windows.Forms.Button();
this.loginbtnExit = new System.Windows.Forms.Button();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("华文新魏", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(120, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 32);
this.label1.TabIndex = 0;
this.label1.Text = "用户登陆";
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 1;
this.label2.Text = "用户名:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(88, 128);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 2;
this.label3.Text = "密 码:";
//
// loginName
//
this.loginName.Location = new System.Drawing.Point(144, 80);
this.loginName.Name = "loginName";
this.loginName.Size = new System.Drawing.Size(128, 21);
this.loginName.TabIndex = 1;
this.loginName.Text = "";
this.toolTip1.SetToolTip(this.loginName, "在此输入您的用户名");
//
// loginPwd
//
this.loginPwd.Location = new System.Drawing.Point(144, 120);
this.loginPwd.Name = "loginPwd";
this.loginPwd.Size = new System.Drawing.Size(128, 21);
this.loginPwd.TabIndex = 2;
this.loginPwd.Text = "";
this.toolTip1.SetToolTip(this.loginPwd, "在此输入您的登陆密码");
//
// loginbtnLogin
//
this.loginbtnLogin.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.loginbtnLogin.Location = new System.Drawing.Point(88, 160);
this.loginbtnLogin.Name = "loginbtnLogin";
this.loginbtnLogin.Size = new System.Drawing.Size(72, 23);
this.loginbtnLogin.TabIndex = 3;
this.loginbtnLogin.Text = "登 陆";
this.toolTip1.SetToolTip(this.loginbtnLogin, "点击登陆");
this.loginbtnLogin.Click += new System.EventHandler(this.loginbtnLogin_Click);
//
// loginbtnExit
//
this.loginbtnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.loginbtnExit.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.loginbtnExit.Location = new System.Drawing.Point(224, 160);
this.loginbtnExit.Name = "loginbtnExit";
this.loginbtnExit.TabIndex = 4;
this.loginbtnExit.Text = "退 出";
this.toolTip1.SetToolTip(this.loginbtnExit, "点击退出");
this.loginbtnExit.Click += new System.EventHandler(this.loginbtnExit_Click);
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(72, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(48, 48);
this.pictureBox1.TabIndex = 5;
this.pictureBox1.TabStop = false;
this.toolTip1.SetToolTip(this.pictureBox1, "欢迎您使用本软件~");
//
// Login
//
this.AcceptButton = this.loginbtnLogin;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.loginbtnExit;
this.ClientSize = new System.Drawing.Size(382, 227);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.loginbtnExit);
this.Controls.Add(this.loginbtnLogin);
this.Controls.Add(this.loginPwd);
this.Controls.Add(this.loginName);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Login";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "登陆";
this.Load += new System.EventHandler(this.Login_Load);
this.ResumeLayout(false); }
#endregion private void Login_Load(object sender, System.EventArgs e)
{
}
private void loginbtnLogin_Click(object sender, System.EventArgs e)
{
string name=loginName.Text.Trim();
string pwd=loginPwd.Text.Trim();
if((name=="")||(pwd==""))
{
MessageBox.Show("用户名和密码不能为空哦!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
loginName.Focus();
return;
}
else
{
validate(name,pwd);
}
return;
}
private void validate(string name,string pwd)
{
string sql="SELECT name,password FROM admin";
Data data=new Data();
try
{
OleDbDataReader reader=data.GetReader(sql);
while(reader.Read())
{
if((name==reader["name"].ToString())&&(pwd==reader["password"].ToString()))
{
new Form1().ShowDialog();
}
else
{
MessageBox.Show("用户名或密码输入有错哦!","提示");
loginName.Focus();
}
}
reader.Close();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
data.DisConnect();
}
} private void loginbtnExit_Click(object sender, System.EventArgs e)
{
Exit();
}
static public void Exit()
{
if(MessageBox.Show("您确定要退出吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk)==DialogResult.OK)
Application.Exit();
}
}
}
用 hide 吧this.Hide();
MainForm frm=new MainForm();
frm.ShowDialog();
frm.Dispose();