大部分时候的作用就是当做显示/提示
一些需要使用链接时,使用标签替代
linkLabel 与Label 是一样的功能,提供显示或者提示的功能.
区别: LinkLable有点击时/点击后的视觉特效
实现一个简单的登录 思路
给登录按钮绑定一个Click事件
登录过程:
1.获取用户名,获取密码
2.比对用户名密码
3.对登录结果进行显示(labres.Text = 登录结果)
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace label
{
public partial class Form1 : Form
{
//声明变量
string defaultName;
string defaultPass;
public Form1()
{
InitializeComponent();
//初始化
defaultName = "Scarlett";
defaultPass = "dove2015";
}
private void label5_Click(object sender, EventArgs e)
{
MessageBox.Show(label5.Text);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linkLabel1.LinkVisited = true;
MessageBox.Show(linkLabel1.Text);
}
private void btn_login_Click(object sender, EventArgs e)
{
string strName = textName.Text;
string strPass = textPass.Text;
//string str = "";
string str = "登录失败";
if (strName == defaultName && strPass == defaultPass)
{
//labres.Text = "登录成功";
str = "登录成功";
}
/*
else
{
//labres.Text = "登录失败";
str = "登录失败";
}
*/
labres.Text = str;
}
private void btn_registered_Click(object sender, EventArgs e)
{
FrmReg fr = new FrmReg();
fr.ShowDialog();
string regName = fr.textName.Text;
string regPass = fr.textPass.Text;
string regPass2 = fr.textPass2.Text;
fr.Close();
labres.Text = string.Format("用户名{0},登录密码{1},二次密码{2}", regName, regPass, regPass2);
MessageBox.Show(labres.Text);
}
}
}
FrmReg.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace label
{
public partial class FrmReg : Form
{
public FrmReg()
{
InitializeComponent();
}
private void btn_registered_Click(object sender, EventArgs e)
{
NewMethod();
//this.Close();
this.Hide();
}
private void NewMethod()
{
//需要判断用户是否输入空字符信息
string regName = this.textName.Text;
string regPass = this.textPass.Text;
string regPass2 = this.textPass2.Text;
if (string.IsNullOrEmpty(regName) || string.IsNullOrEmpty(regPass) || string.IsNullOrEmpty(regPass2))
{
MessageBox.Show("请填写用户名与密码");
}
else if (regPass != regPass2)
{
MessageBox.Show("两次输入的密码不一致");
}
}
private void FrmReg_FormClosing(object sender, FormClosingEventArgs e)
{
NewMethod();
e.Cancel = true;
}
}
}