close

using System;
using System.Windows.Forms;

public class LoginForm : Form {
    private Label usernameLabel;
    private TextBox usernameTextBox;
    private Label passwordLabel;
    private TextBox passwordTextBox;
    private Button loginButton;

    public LoginForm() {
        // 設置窗口標題和大小
        this.Text = "登入";
        this.Width = 250;
        this.Height = 150;

        // 創建用戶名和密碼標籤
        usernameLabel = new Label();
        usernameLabel.Text = "用戶名:";
        usernameLabel.Left = 20;
        usernameLabel.Top = 20;
        usernameLabel.AutoSize = true;
        Controls.Add(usernameLabel);

        passwordLabel = new Label();
        passwordLabel.Text = "密碼:";
        passwordLabel.Left = 20;
        passwordLabel.Top = 50;
        passwordLabel.AutoSize = true;
        Controls.Add(passwordLabel);

        // 創建用戶名和密碼輸入框
        usernameTextBox = new TextBox();
        usernameTextBox.Left = 80;
        usernameTextBox.Top = 20;
        usernameTextBox.Width = 120;
        Controls.Add(usernameTextBox);

        passwordTextBox = new TextBox();
        passwordTextBox.Left = 80;
        passwordTextBox.Top = 50;
        passwordTextBox.Width = 120;
        passwordTextBox.PasswordChar = '*';
        Controls.Add(passwordTextBox);

        // 創建登入按鈕
        loginButton = new Button();
        loginButton.Text = "登入";
        loginButton.Left = 80;
        loginButton.Top = 80;
        loginButton.Click += new EventHandler(LoginButton_Click);
        Controls.Add(loginButton);
    }

    private void LoginButton_Click(object sender, EventArgs e) {
        string username = usernameTextBox.Text;
        string password = passwordTextBox.Text;

        // 在此處處理帳號和密碼的驗證
        // ...

        MessageBox.Show("用戶名:" + username + "\n密碼:" + password);
    }

    public static void Main() {
        Application.Run(new LoginForm());
    }
}
 

 

使用Windows Forms來創建一個表單,並在表單中添加用戶名和密碼輸入框、登入按鈕。當用戶點擊登入按鈕時,可以獲取用戶輸入的帳號和密碼,並進行相應的驗證,這部分邏輯需要根據具體應用場景進行實現。

您可以根據需要進一步修改表單的樣式和佈局,以及實現更複雜的驗證邏輯。

arrow
arrow
    全站熱搜

    kaiser560 發表在 痞客邦 留言(0) 人氣()