簡體   English   中英

C# 中帶有單選按鈕的 groupBox 的事件處理程序

[英]Event handler for groupBox with radioButtons in C#

我在 groupBox 中有一些 radioonButtons,我需要做一些我可以稱之為“其中一個 radiobuttons.checked 更改”的操作,或者從 radiobutton 中找出更改了什么索引。 我試圖在事件列表中找到它,但找不到合適的。

編輯:為了更清楚:我需要知道是否存在一些handel,用於為goupBox而不是單個radioButton編寫處理程序方法。 我知道如何使用 radiButton.checkedChanged,但這不是我所發現的。 我發現處理程序“在組框中發生了一些事情”或類似的(如果存在)。

它位於 Visual Studio 2012 的WFA(Windows 演示應用程序)中。

我認為您想要做的是將所有 RadioButtons 的 CheckedChanged 事件連接到同一個處理程序。

public Form1()
{
    radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
    radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);

    // ...
}

private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
    RadioButton radioButton = sender as RadioButton;

    if (radioButton1.Checked)
    {
        // Do stuff 
    }
    else if (radioButton2.Checked)
    {
        // Do other stuff
    }
}

據我所知,沒有內置任何內容。

將標簽屬性設置為某種指標(0 到 n)即可。

添加一個 CheckChangedHandler

將所有按鈕 CheckChanged 事件指向它。

然后像。

private void radioButtons_CheckedChanged (object sender, EventArgs e) 
{     
  RadioButton radioButton = sender as RadioButton;      
  int buttonid = (int)radioButton.Tag;
  switch (buttonid)
  {
    case 0 : // do something; break
  }
} 

如果您有其中的一些,我會查看 radiogroup 組件。

我遇到了同樣的問題:一個名為Button Type (gbxButtonType)的組框有 6 個單選按鈕,另一個名為Icon Type (gbxIconType) 的組框有 8 個單選按鈕。 當用戶從每個組框中選擇一個單選按鈕時,單擊DisplayButton后將出現一個 MessageBox 並應用該選擇。 我的問題是組框沒有 CheckedChanged 事件。 AKN的解決方案完美運行:

public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < gbxButtonType.Controls.Count; i++)
        {
            RadioButton rdb = (RadioButton)gbxButtonType.Controls[i];
            rdb.CheckedChanged += new System.EventHandler(gbxButtonType_CheckedChanged);
        }

        for (int i = 0; i < gbxIconType.Controls.Count; i++)
        {
            RadioButton rdb = (RadioButton)gbxIconType.Controls[i];
            rdb.CheckedChanged += new System.EventHandler(gbxIconType_CheckedChanged);
        }
    }

private void gbxIconType_CheckedChanged(object sender, EventArgs e)
    {
        if (sender == rdbAsterisk)
        {
            iconType = MessageBoxIcon.Asterisk;
        }
        else if (sender == rdbError)
        {
            iconType = MessageBoxIcon.Error;
        }
        ...
        else
        {
            iconType = MessageBoxIcon.Warning;
        }
   }

類似於 davenewza 的回答(可能應該是評論,但我沒有足夠的聲譽),但是對於整個單選按鈕組,該事件僅觸發一次。

public Form1()
{
    // Add a "CheckedChanged" event handler for each radio button.
    // Ensure that all radio buttons are in the same groupbox control.
    radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
    radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
}

private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
    // Do stuff only if the radio button is checked (or the action will run twice).
    if (((RadioButton)sender).Checked)
    {
        if (((RadioButton)sender) == radioButton1)
        {
            // Do stuff 
        }
        else if (((RadioButton)sender) == radioButton2)
        {
            // Do other stuff
        }
    }
}

System.Windows.Forms.RadioButton.CheckedChanged

是你需要的活動

因此,請執行以下操作:

    public Form1()
    {
        InitializeComponent();

        this.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        // your action
    }

Groupbox 將限制只選中一個單選按鈕

所以Setp1:您可以為所有單選按鈕分配一個“CheckedChanged”事件處理程序

private void initRadio()
{
        radio_button1.CheckedChanged += Radio_show_CheckedChanged;
        radio_button2.CheckedChanged +=Radio_show_CheckedChanged;
}

Setp2:像這樣實現這個事件處理程序(按單選按鈕的文本過濾)

private void Radio_show_CheckedChanged(object sender, EventArgs e)
{
    RadioButton radioButton = sender as RadioButton;
    if (radioButton.Checked == true) { //limited only checked button do function
        switch (radioButton.Text)
        {
            case "name1":
                // do your stuff ...
                break;
            case "name2":
                // do your stuff ...
                break;
        }
    }
}

我認為您想使用 groupbox 控件本身來處理 groupbox 內某些單選按鈕的選擇。

可能您希望這基本上是為了避免代碼重復。

(即)為設計器中的所有單選按鈕添加檢查更改事件,當有更多控制時可能會很乏味。 既然它已經存在於一個組下,為什么不使用組控制對象來操作其中的控件並設置事件。

這就是我理解您的問題的方式,因此解決方案如下所示。

為分組框中的所有單選按鈕控件設置一個通用處理程序

for (int i = 0; i < groupBox.Controls.Count; i++)
{
    RadioButton rb = (RadioButton)groupBox.Controls[i];
    rb.CheckedChanged += new System.EventHandler(evntHandler);
}

在處理程序中,您可以根據其他人的指示確定哪個按鈕已更改並執行必要的操作。

//這是喬克·弗蘭克·哈利迪的禮貌

     //^subscribe events to radio button check changed 
    private void seriesTxtBxEvent()
    {
        //Show txtBx
        this.radBtn_RoomSeries.CheckedChanged += new EventHandler(showSeriesTxtBx_Event);
        //Hide txtBx
        this.radBtn_RoomNumber.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
        this.radBtn_RoomName.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
        this.radBtn_RoomLevel.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
        this.radBtn_RoomDep.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
    }



    private void hideSeriesTxtBx_Event(object sender, EventArgs e)
    {
        tbx_SheetSeries.Visible = false;
    }


    private void showSeriesTxtBx_Event(object sender, EventArgs e)
    {
        tbx_SheetSeries.Visible = true;
    }
//Form Start

void MainFormLoad(object sender, EventArgs e)
{

    Control.ControlCollection locais =  groupBoxLocalização.Controls;

        foreach (CheckBox chkBox in locais)
        {
            chkBox.MouseUp += chkBoxLocais_MouseUp;
        }
}

// Event
void chkBoxLocais_MouseUp(object sender, MouseEventArgs e)
{

    //Tratar individualmente
    CheckBox chk = (CheckBox)sender;

    //ou para tratar todos objetos de uma vez

    Control.ControlCollection locais =  groupBoxLocalização.Controls;
    foreach (CheckBox chkBox in locais) {
        //chkBox....
    }

}

您也許可以使用 Timer 來實現,但這對於優化來說是不利的,簡單的解決方案是對於每個單選按鈕,您只需添加一個函數作為 ChekedChanged 事件。

  1. 通過雙擊任何單選按鈕創建 Checked 事件,復制 Visual Studio 為該事件創建的方法的名稱
  2. 轉到所有單選按鈕並將事件更改為從“屬性資源管理器”>“事件”部分復制的事件
  3. 在生成的方法中使用以下代碼。 這將觸發所有單選按鈕的事件,但只會“做你的事”一次

代碼:

private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;

    if (rb.Checked == false) return;

    // Do your thing
}
  1. 在設計器事件列表中的一個單選按鈕上創建事件 Checked_Changed。

  2. 從每個單選按鈕的 Checked_Changed 事件前面的下拉列表中向每個單選按鈕添加相同的事件

  3. 內部檢查更改事件使用

    private void CheckedChanged(object sender,EventArgs e) { var radio = groupBox.Controls.OfType<RadioButton> ().FirstOrDefault(r => r.Checked).Name; }

您可以了解現在哪個收音機處於活動狀態。

暫無
暫無

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

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