| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5594 人关注过本帖
标题:[求助]登录成功后怎么关闭登录窗口??
只看楼主 加入收藏
ugene
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-4-6
收藏
 问题点数:0 回复次数:4 
[求助]登录成功后怎么关闭登录窗口??
if((dr[0].ToString().Trim()==userid)&&(dr[1].ToString().Trim()==userpassword))
{
MainForm form=new MainForm();
form.ShowDialog();
this.Close();
}//输入成功,登录到主窗口
能成功登录,但登陆后的登录界面还有,请问登录成功后怎么关闭登录界面!
搜索更多相关主题的帖子: 窗口 登录 
2007-06-05 11:39
白菜粉条
Rank: 1
等 级:新手上路
帖 子:76
专家分:0
注 册:2007-5-25
收藏
得分:0 
你应该在 MainForm 的构造函数里面打开登陆窗口!

我不是不想不郁闷,台湾问题没有解决啊!
2007-06-05 11:57
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 

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);
}
*/
}

}
}


IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-06-05 12:45
jasonxie
Rank: 1
等 级:新手上路
威 望:2
帖 子:225
专家分:0
注 册:2007-3-19
收藏
得分:0 

为什么我的登陆后瞬间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();
}

}
}


你是我心内的一首歌,不要只是个过客;在我生命留下一首歌,无论结局会如何.
2007-06-05 15:22
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
this.Hide();
MainForm mainForm = new MainForm();
mainForm.ShowDialog();

msdn == 葵花宝典!!!
QQ:122768959
2007-07-18 17:42
快速回复:[求助]登录成功后怎么关闭登录窗口??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017145 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved