簡體   English   中英

如何使用if-else語句檢查按鈕單擊歷史? C#

[英]How to check button click history using if-else statements ? C#

我總共有4種形式(Form1,form2,form3,form4)。
3個按鈕(表格1有2(Button1,Button2),表格2有1(Button3))

我的情況如下:

Form1兩個按鈕都通向form2但標簽輸入不同。 我想做的就是單擊我的按鈕3時,是否循環將確定按鈕導致的形式(form3,form4)。 如果單擊button1,則按鈕3將導致到form3,否則button3將導致到form4。

我不確定在問這個問題時我應該在代碼的哪部分以及哪一部分包括代碼,所以我認為我應該只放入按鈕代碼。

這是button1代碼:

public void DIModuleButton_Click(object sender, System.EventArgs e)
{
    //  MessageBox.Show("TEST");
    alloDI();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Show();
}

public void alloDI()
{
    ALL = "DI";
}

這是button2代碼:

public void DOModuleButton_Click(object sender, EventArgs e)
{
    alloDO();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Show();
}

public void alloDO()
{
    ALL = "DO";
}

一個簡單的解決方案是使用布爾標志,因為您只需區分兩個按鈕即可。

就像在MyProperty所做的那樣,將新的bool屬性放入第二種形式。

在Form2中:

public bool Button1_pressed { get; set; }

在“按鈕單擊”事件中,您將進行相應的設置:

public void DIModuleButton_Click(object sender, System.EventArgs e)
{
    //  MessageBox.Show("TEST");
    alloDI();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Button1_pressed = true;
    frm.Show();
}

在第二秒鍾,您將其設置為false

當按下第三個按鈕時,您可以檢查此變量。

如果您有2個以上的按鈕,我建議使用一個enum和一個切換用例來檢查不同的用例:

該屬性如下所示:

public ButtonSource MyButton_Clicked { get; set; }

// here your enum
public enum ButtonSource
{
    button1,
    button2,
    button3,
    button4
}

和開關盒:

switch (MyButton_Clicked)
{
    case ButtonSource.button1:
        break;
    case ButtonSource.button2:
        break;
    case ButtonSource.button3:
        break;
    case ButtonSource.button4:
        break;
    default:
        break;
}

暫無
暫無

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

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