亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建一個變量來驗證按鈕事件處理程序是否被單擊

創建一個變量來驗證按鈕事件處理程序是否被單擊

C#
躍然一笑 2023-07-23 14:01:01
我有一些按鈕事件處理程序,我需要驗證它們是否被單擊。我試圖在事件中放入一個布爾值,但由于有多個按鈕,我需要布爾值在每次退出事件時重置。我嘗試將布爾值放入事件中,但它僅適用于單個按鈕:public void verify(){    if (this.textBox.Text != null &&            this.comboBox.Text != null            && (button bool here)          )    {        tabControl.SelectedIndex = 2;    }    else    {        MessageBox.Show("Enter parameters");    }}如果沒有單擊所有按鈕,我希望 bool 為 false,并且在所有按鈕至少單擊一次后為 true。private void Button_Click(object sender, EventArgs e){    Form temp = new Form(Image);    temp.ShowDialog();    int x = temp.x;    int y = temp.y;    int w = temp.w;    int h = temp.h;}這是一個示例按鈕,不同頁面上可能還有 20 個類似的按鈕。單個頁面上平均有 4 到 5 個按鈕。驗證按鈕點擊的最佳方法是什么?
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

實現此目的的一種方法是將這些bool字段封裝到一個表示程序狀態的類中,以及一個計算屬性,true如果所有其他字段都是 則返回該計算屬性true。


例如:


class ProgramState

{

    public bool UserAcceptedAgreement { get; set; }

    public bool UserAcknowledgedLiability { get; set; }

    public bool UserSubmittedSignature { get; set; }


    public bool EverythingAccepted =>

        UserSubmittedSignature &&

        UserAcknowledgedLiability &&

        UserSubmittedSignature;

}

然后,您可以在您的類中創建此類的實例Form,并通過按鈕單擊事件設置屬性,并在方法中Verify檢查它們在if語句中是否全部為 true:


public partial class Form1 : Form

{

    private ProgramState programState = new ProgramState();


    public Form1()

    {

        InitializeComponent();

    }


    private void btnAcceptAgreement_Click(object sender, EventArgs e)

    {

        programState.UserAcceptedAgreement = true;

    }


    private void btnAcceptLiability_Click(object sender, EventArgs e)

    {

        programState.UserAcknowledgedLiability = true;

    }


    private void btnSubmitSignature_Click(object sender, EventArgs e)

    {

        programState.UserSubmittedSignature = true;

    }


    public void verify()

    {

        if (programState.EverythingAccepted)

        {

            tabControl.SelectedIndex = 2;

        }

        else

        {

            MessageBox.Show("Enter parameters");

        }

    }

}


查看完整回答
反對 回復 2023-07-23
  • 1 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號