您的位置:首页 > 编程语言 > C#

C#Winform实现登陆的两种方式

2014-04-02 00:36 477 查看

建立登陆注册窗口

相关基础知识

关于字符串

1.  IsNullOrEmpty             //用于判断字符串是否为空,如果为空,弹出提示

如:

public static String Test(string s)
{
if (String.IsNullOrEmpty(s)) //如果字符串为空
return "is null or empty";
else
return String.Format("(\"{0}\")is neither null nor empty", s);
}


2.       计算字符串个数用:**.Length,而不是**.Count;

如:

if (UserInputName.Length >= 15) {
MessageBox.Show("用户名过长,请重新输入用户名!");
}


方法1:从主界面开始(来自互联网)

1.设计登录注册三界面

共有三个,如下:



上图登录及注册为linklabel控件,其他为label控件;



上图为登陆界面,两个textbox文本输入框,注册为linklabel控件



2.解析

该例的一个重要特点是:先是从主界面开始的,有主界面跳到登陆界面或注册界面,这样在Main中只要开启主界面。登陆和注册界面要用到时new即可。

缺点:在还没有登录进去就能看到主界面,不是太安全。如果在登录进去之前设置属性都为不可见,在登录成功后然后再改为属性为可见,有太麻烦。

源代码

主界面(Form1):

注册界面(Form3)

本文使用的数据库是sql sever2005,先在引用里加入:

usingSystem.Data.SqlClient;
public partial class Form3 : Form
{
publicForm3()
{
InitializeComponent();
}
bool  flagRegister;//定义标志位,确认用户注册
stringstrConnect = "DataSource=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist SecurityInfo=True;User ID=sa;Password=******";  //连接数据库字符串
privatevoid button1_Click(objectsender, EventArgs e)
{
if((textBox1.Text.Length >= 4)&& (textBox1.Text.Length <=12) && (textBox2.Text.Length >= 6) &&(textBox3.Text.Length >= 6))
{
flagRegister = true; //用户名和密码达到要求,做个标记
}
else
{
if((textBox1.Text.Length < 4) || (textBox1.Text.Length > 12))
{
MessageBox.Show("用户名长度不在约定范围内,请重新输入!", "提示");
return;
}
if(textBox2.Text.Length < 6)
{
MessageBox.Show("密码长度不足6位,请重新输入!","提示");
return;
}
if(textBox3.Text.Length < 6)
{
MessageBox.Show("请重新输入邮箱!", "提示");
return;
}
}//判断用户名条件;

if(UserFlag == true)
{
MessageBox.Show("用户已经存在,请重新输入!");
return;
}

if(flagRegister == true) //用户名和密码达到要求,开始把用户写入数据库
{
SqlConnectionconConnection = new SqlConnection(strConnect);
conConnection.Open();
stringcmd = "insert into 用户(用户名,密码,email) values ('" + textBox1.Text + "'," + "'" +textBox2.Text + "'," + "'" +textBox3.Text + "') ";
SqlCommandcom = new SqlCommand(cmd, conConnection);
com.ExecuteNonQuery();
conConnection.Close();
MessageBox.Show("注册成功!点击确定,返回登录界面。", "提示");
this.Close();
Form1 f1 = new Form1();
f1.label2.Text = "欢迎你," + textBox1.Text;
f1.label1.Visible = false;
f1.label3.Visible = false;
f1.linkLabel1.Visible = false;
f1.linkLabel2.Visible = false;
f1.label2.Visible = true;
f1.Show();
}
}

public bool UserFlag;//定义标志位,来确认用户是否存在
private voidtextBox1_TextChanged(object sender, EventArgs e)
{
SqlConnectionconConnection = new SqlConnection(strConnect);
conConnection.Open();
stringcmd = "select 用户名 from 用户";
SqlCommandcom = new SqlCommand(cmd, conConnection);
SqlDataReaderreaderUser = com.ExecuteReader();
while(readerUser.Read())
{
if(textBox1.Text == readerUser["用户名"].ToString().Trim())
{
label5.Text = "用户已存在,请重新输入!";
UserFlag = true;
//textBox1.Text= "";
return;
}
elseif (textBox1.Text != readerUser["用户名"].ToString().Trim())
{
label5.Text = "恭喜你,该用户名可以使用。";
UserFlag = false;
}
}
}//判断用户名是否满足条件

privatevoid textBox3_TextChanged(object sender, EventArgs e)
{
intindex = textBox3.Text.IndexOf("@");
if(index < 1)
{
label7.Text = "邮箱格式不正确,请重新输入!";
}
else
{
label7.Text = "邮箱格式正确";
}
}//判断邮箱格式是否正确
}


登录界面(Form2)

本文使用的数据库是sql sever2005,先在引用里加入:

usingSystem.Data.SqlClient;
string User,Pwd; //用户名,密码
boolflagshow = false;//用来标注登录名是否存在于数据库
private voidlinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
}//显示注册界面

private void button1_Click(objectsender, EventArgs e) //登录
{
string strConnect = "DataSource=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist SecurityInfo=True;User ID=sa;Password=******";
SqlConnectionconConnection = new SqlConnection(strConnect);
conConnection.Open();
stringcmd = "select 用户名,密码,email from 用户";
SqlCommandcom = new SqlCommand(cmd, conConnection);
SqlDataReaderreader = com.ExecuteReader();
while(reader.Read())//从数据库读取用户信息
{
User = reader["用户名"].ToString();
Pwd = reader["密码"].ToString();
if(User.Trim () == textBox1.Text & Pwd.Trim () == textBox2.Text)
{
flagshow = true; //用户名存在于数据库,则为true
}
}
reader.Close();
conConnection.Close();

if(flagshow == true)
{
showMainForm();//用户存在,返回登录界面
}
else
{
MessageBox.Show("用户不存在或密码错误!", "提示");
return;
}
}


private void showMainForm()//登录成功,显示主界面
{
this.Close();
Form1 f1 = new Form1();
f1.label1.Visible = false;
f1.label3.Visible = false;
f1.linkLabel1.Visible = false;
f1.linkLabel2.Visible = false;
f1.label2.Visible = true;
f1.label2.Text = "欢迎你," + textBox1 .Text ;
f1.Show();
}


方法2:从登陆界面开始

标准登陆窗口

         此例暂时不用数据库做登录。Application.Run的窗口不能作为登录界面,因为Application.Run起来的窗口是主窗口,主窗口适合长期显示,主窗口一旦关闭程序就终止了。

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormLogin formlogin = new FormLogin();
formlogin.ShowDialog();//ShowDialog有返回值,返回值是DialogResult
Application.Run(new FormMain());
}


专业化的登陆界面

•    启动时窗口位于屏幕中央StartPosition=CenterScreen

•    设定AcceptButton,当点击回车键要响应的按钮,这样不用很土的点击按钮才能触发。设定CancelButton,当点击ESC键时要响应的按钮。

•    设定按钮的DialogResult,当点击按钮的时候窗口关闭,并且执行操作。设定窗口的DialogResult即可关闭窗口并且让ShowDialog方法返回设置的值。

•    对话框的风格,不能随意的拖放大小、最大化FormBorderStyle=FixedDialogMinimizeBox=FalseMaximizeBox=False

界面



  



源代码

【FormLogin】

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Admin" && textBox2.Text == "123")
{
//给DialogResult赋值,并且这个窗口是用ShowDialog显示的,则点击按钮时会自动关闭该窗口。
//一个按钮的DialogResult属性,可以在关闭对话框以后返回到底点击的是哪个按钮
DialogResult = DialogResult.OK;
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}


【FormMain】

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormLogin flogin = new FormLogin();
if (flogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new FormMain());
}
//if (flogin.ShowDialog() ==DialogResult.Cancel) {
//   Application.Exit();
//}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: