設計一個簡單的登陸窗口,要求輸入用戶名:小金,密碼:123456時候點登陸能正確轉到另外一個窗口。this
一、創建窗體應用。spa
二、這裏建立一個login和一個NewForm的窗體。設計
三、在login的窗體拖拉2個label和2個textbox和1個linklabel的控件。一個標籤名字爲用戶名,一個標籤爲密碼,超連接標籤名爲登陸。code
標籤的賦名:雙擊大窗體的邊框進入代碼界面,在這個代碼塊對各標籤賦名。orm
四、雙擊linklabel計入超連接標籤的代碼塊,這裏用戶判斷用戶名和密碼是否正確,是否跳轉另外一個窗口等等一些事件。blog
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 WindowsFormsApp1 { public partial class login : Form { public login() { InitializeComponent(); } private void Login_Load(object sender, EventArgs e) { label1.Text = "請輸入用戶名"; label2.Text = "請輸入密碼"; linkLabel1.Text = "登陸"; } private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //這裏點擊登陸時給出一個是否確認登陸的提示, //是就跳轉到另外一個窗口,否就關閉本窗口,取消關閉消息窗口 DialogResult dr = MessageBox.Show("是否肯定須要登陸", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { //只有輸入用戶名:小金,密碼:123456時候點登陸能正確轉到另外一個窗口。 if (textBox1.Text == "小金" && textBox2.Text == "123456") { MessageBox.Show("恭喜你,成功登陸"); NewForm newForm = new NewForm(); newForm.Show(); } else//輸入不正確提示 { MessageBox.Show("很是遺憾,成功失敗"); this.Close(); } } else if (dr == DialogResult.No) { this.Close(); } else { } } } }
五、在創建窗體即產生的Programe.cs的main方法裏面,將最後一行代碼的run窗體改成login。事件
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new login());//這裏改成須要操做的窗體名稱 } } }