C# Winform初體驗


設計一個簡單的登錄窗口,要求輸入用戶名:小金,密碼: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("非常遺憾,成功失敗");
            //return;//也可以使用return,直接跳過main函數,立即結束本次方法的執行
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());//這里改為需要操作的窗體名稱
        }
    }
}

 

 
         

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM