簡體   English   中英

檢查radiobuttons並在C#中顯示消息

[英]check radiobuttons and display message in C#

我有一個表單,其中“listBox1”和“button1”。 我有兩個功能。 第二個函數向listbox1添加復選框,第一個函數顯示消息框。 但我不知道如何編寫第一個函數。

在這里,我想檢查選中的復選框並寫入消息:

private void button1_click(object sender, EventArgs e)
{
    MessageBox.Show("radiobutton: " + rb[i].Text);
}

在這里,我創建了復選框://它正在工作

internal void loadSurveys()
{
    WebClient client2 = new WebClient();
    var json = client2.DownloadString("http://www.test.net/api/surveys/?api_key=123");
    JObject data = JObject.Parse(json);
    var example = JsonConvert.DeserializeObject<Example>(json);
    int y = 5;
    int i = 0;
    RadioButton[] rb = new RadioButton[example.surveys.Length];
    String chkBox_name = "";
    String chkBox_text = "";
    foreach (var survey in data["surveys"].Children())
    {
        rb[i] = new RadioButton();
        rb[i].Location = new Point(5, y);
        rb[i].Name = chkBox_name + survey["id"];
        rb[i].Text = chkBox_text + survey["title"];
        rb[i].AutoSize = true;
        this.listBox1.Controls.Add(rb[i]);
        y += 20;
        i++;
    }

}

第一步是使radiobutton數組在表單級別上變量:

RadioButton[] rb

在loadSurveys中分配

rb = new RadioButton[example.surveys.Length];

然后可以在按鈕單擊內訪問該數組

var rb = rb.FirstOrDefault(r=>r.Checked);
if(rb==null)
    MessageBox.Show("No radiobutton was selected");
else
    MessageBox.Show("radiobutton: " + rb[i].Text);

編輯只是注意到你將radiobuttons添加到列表框中。 listbox1變量是否是實際的列表框? 以上仍然有效,但如果目標是顯示radiobuttons的列表框,您可以自定義繪制列表框,否則使用普通面板而不是列表框。 無論哪種方式,您還可以對listbox1變量的控件(使用OfType)執行firstordefault,但是如果您使用列表框並填充其項目,則只需使用SelectedIndexChanged

編輯2由於我已經擁有它,想要展示一種方法,使您的列表框成為一個單選按鈕框。 您可以使用以下類將任何現有列表框設置為radiobutton框:

public class RadioButtonBoxPainter:IDisposable
{

    public readonly ListBox ListBox;
    public RadioButtonBoxPainter(ListBox ListBox)
    {
        this.ListBox = ListBox;
        ListBox.DrawMode = DrawMode.OwnerDrawFixed;
        ListBox.DrawItem += ListBox_DrawItem;
    }

    void ListBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1) return;
        Rectangle r = e.Bounds;
        r.Width=r.Height;
        bool selected= (e.State & DrawItemState.Selected) > 0;
        e.DrawBackground();
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        ControlPaint.DrawRadioButton(e.Graphics, r, selected ? ButtonState.Checked : ButtonState.Normal);
        r.X = r.Right + 2;
        r.Width = e.Bounds.Width - r.X;
        string txt;
        if (ListBox.Site != null && ListBox.Site.DesignMode && e.Index >= ListBox.Items.Count)
            txt = ListBox.Name;
        else
            txt = ListBox.GetItemText(ListBox.Items[e.Index]);
        using (var b = new SolidBrush(e.ForeColor))
            e.Graphics.DrawString(txt, e.Font, b, r);
        if (selected)
        {
            r = e.Bounds;
            r.Width--; r.Height--;
            e.Graphics.DrawRectangle(Pens.DarkBlue, r);
        }
    }

    public void Dispose()
    {
        ListBox.DrawItem -= ListBox_DrawItem;
    }
}

標准實現示例:

public class RadioButtonBox:ListBox
{
    public readonly RadioButtonBoxPainter Painter;

    public RadioButtonBox()
    {
        Painter = new RadioButtonBoxPainter(this);
    }

    [DefaultValue(DrawMode.OwnerDrawFixed)]
    public override DrawMode DrawMode
    {
        get{return base.DrawMode;}
        set{base.DrawMode = value;}
    }
}

RadioButtonBox是我實際使用很多的控件。 就個人而言,我發現在實施一堆單獨的無線電按鈕時要快得多。

如果您想使用它,並想要一個示例如何在當前代碼中實現它,請留下評論,我將添加一個。

您可以瀏覽listBox1.Controls並選擇已檢查的RadioButton

private void button1_click(object sender, EventArgs e)
{
    var rb = this.listBox1.Controls.OfType<RadioButton>().SingleOrDefault(n => n.Checked);
    if (rb != null)
        MessageBox.Show("radiobutton: " + rb.Text);
}

因為這是RadioButton所以不應該檢查多於一個

暫無
暫無

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

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