簡體   English   中英

禁用某些按鈕,具體取決於哪個用戶已登錄

[英]Disable certain buttons depending on which user has logged in

以登錄形式,當我以DOCTOR表中存在的Jack身份登錄時,它將轉到page_two。 我想禁用護士按鈕1和護士按鈕2,因為傑克不是護士而是醫生。 然后相反,如果我以NURSE表中存在的Mary身份登錄,它將轉到page_two。 我想禁用醫生按鈕1和醫生按鈕2,因為瑪麗不是醫生,而是護士。

Page_two的按鈕名稱為btnDoctor1,btnDoctor2,btnNurse1和btnNurse2

//登錄表單代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace GRP_02_03_SACP
{
    public partial class page_one : Form
    {
        public page_one()
        {
            InitializeComponent();

        }


        private void page_one_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //retrieve connection information info from App.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
            //STEP 1: Create connection
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            //STEP 2: Create command
            string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
            // Add a WHERE Clause to SQL statement
            strCommandtext += "   WHERE dUsername=@dname AND dPassword=@dpwd;";
            strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
            SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
            cmd.Parameters.AddWithValue("@dname", textUsername.Text);
            cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
            cmd.Parameters.AddWithValue("@nname", textUsername.Text);
            cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);


            try
            {
                // STEP 3: open connection and retrieve data by calling ExecuteReader
                myConnect.Open();
                // STEP 4: Access Data
                SqlDataReader reader = cmd.ExecuteReader();


                while (reader.Read()) //For Doctor
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        page_two form = new page_two();
                        form.Show();
                        return;
                    }                                     
                } 
                reader.NextResult();
                while (reader.Read()) //For Nurse
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        page_two form = new page_two();
                        form.Show();
                        return;
                    }
                }

                //STEP 5: close connection
                reader.Close();
                MessageBox.Show("Invalid username or password");
            }
            catch (SqlException ex)
            {

            }
            finally
            {
                //STEP 5: close connection
                myConnect.Close();
            }
        }      
    }
}

我建議您創建一些Person類來保存人員數據:

public class Person
{
    public string Name { get; set; }
    public JobPosition Position { get; set; }
    // etc
}

Position是您個人所能獲得的工作職位的枚舉:

public enum JobPosition
{
    Doctor,
    Nurse
}

下一步將通過將數據庫查詢移至某個存儲庫類來將數據訪問邏輯與表示代碼分離:

public class PersonRepository
{
    public Person GetPerson(string userName, string password)
    {
        // execute query and create Person instance
    }
}

我還要創建一個單獨的登錄表單,該表單應在您的主表單啟動之前顯示。 它應該使用PersonRepository來獲取Person實例,該實例應該傳遞給MainForm構造函數:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() != DialogResult.OK)
    return;

Application.Run(new MainForm(loginForm.Person));

在主窗體上,使用已登錄人員的位置來啟用或禁用控件:

public partial class MainForm : Form
{
    private Person _person;

    public MainForm(Person person)
    {
        InitializeComponent();
        _person = person;
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        fooButton.Enabled = (_person.Position == JobPosition.Doctor);
        // etc
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM