check radiobuttons and display message in C#

Total Post:154

Points:1086
C# 
C#
 1621  View(s)
Ratings:
Rate this:

I have a form on which are "listBox1" and "button1". I have two function. The second function adds checkboxes to listbox1 and the first function displays message box. But I don´t know how to write the first function.

Here I want to check which checkbox is checked and write a message:

private void button1_click(object sender, EventArgs e)
{
    MessageBox.Show("radiobutton: " + rb[i].Text);
}
Here I create checkboxes: //it´s working
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++;
    }
}
  1. Post:412

    Points:2888
    Re: check radiobuttons and display message in C#

    Hi Manoj,


    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);

      Modified On Apr-04-2018 05:54:59 AM

Answer