設計一個簡單的登錄視窗,要求輸入用戶名:小金,密碼:123456時候點登錄能正確轉到另一個視窗。 1、建立窗體應用。 2、這裡創建一個login和一個NewForm的窗體。 3、在login的窗體拖拉2個label和2個textbox和1個linklabel的控制項。一個標簽名字為用戶名,一個標簽為密 ...
設計一個簡單的登錄視窗,要求輸入用戶名:小金,密碼:123456時候點登錄能正確轉到另一個視窗。
1、建立窗體應用。
2、這裡創建一個login和一個NewForm的窗體。
3、在login的窗體拖拉2個label和2個textbox和1個linklabel的控制項。一個標簽名字為用戶名,一個標簽為密碼,超鏈接標簽名為登錄。
標簽的賦名:雙擊大窗體的邊框進入代碼界面,在這個代碼塊對各標簽賦名。
4、雙擊linklabel計入超鏈接標簽的代碼塊,這裡用戶判斷用戶名和密碼是否正確,是否跳轉另一個視窗等等一些事件。
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 { } } } }
5、在建立窗體即產生的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());//這裡改為需要操作的窗體名稱 } } }