none
Trying to get the value of RadioButton in other method. RRS feed

  • Question

  • Hello,

    I'm creating a Windows Form Application, in this application im creating RadioButtons dynamically(from xml).

    I'm trying to get the values of these RadioButtons in another method.

    Code of creating buttons:

                        int heightRadioButtons = 0;
                        WidthPanelsRow1 += 155;
                        Panel panel = new Panel();
                        panel.Size = new Size(140, 200);
                        panel.Location = new Point(WidthPanelsRow1, heightPanelsRow1);
                        panel.BackColor = Color.LightGray;

                        Label lbl = new Label();
                        lbl.Text = node["Titel"].InnerText;
                        lbl.Location = new Point(0, 0);
                        lbl.Font = font1;
                        panel.Controls.Add(lbl);

                        int counterLastRadioButton = 0;
                        XmlNodeList waardeNodes = node.SelectNodes("Waardes");
                        foreach (XmlNode wNode in waardeNodes)
                        {
                            counterLastRadioButton += 1;
                            heightRadioButtons += 20;
                            RadioButton rb = new RadioButton();
                            rb.Text = wNode.InnerText;
                            rb.Location = new Point(5, heightRadioButtons);
                            rb.Name = "rb" + count.ToString();
                            if (waardeNodes.Count - 1 < counterLastRadioButton)
                            {
                                rb.Checked = true;
                            }
                            panel.Controls.Add(rb);
                        }
                        this.Controls.Add(panel);

    Code for retrieving selected radiobutton(so far):

                        for (int i = 1; i < radioButtonCounter; i++)
                        {
                            RadioButton rb = this.Controls.Find("rb"+i.ToString(), true).LastOrDefault() as RadioButton;
                            MessageBox.Show(rb.Text);
                        }

    Now obviously im asking for the last of default value of the radiobuttons(not checking if checked), but i dont know how to get the selected value..

    Hope anybody can help, 

    Thanks

    Monday, October 16, 2017 8:58 AM

Answers

  • If they are in panels

    var panels = Controls.OfType<Panel>().ToList();
    foreach (Panel p in panels)
    {
        var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
        if (selectedRadioButton != null)
        {
            MessageBox.Show($"{p.Name}.{selectedRadioButton.Text}");
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Niels Schutte Monday, October 16, 2017 11:46 AM
    Monday, October 16, 2017 11:44 AM
    Moderator

All replies

  • If the Radio buttons are on a control such as a Panel.

    var selectedRadioButton = panel1.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
    if (selectedRadioButton != null)
    {
        MessageBox.Show(selectedRadioButton.Name);
    }

    Find by searching children

    var index = 2;
    var rb = Controls.Find($"radioButton{index}", true).FirstOrDefault();
    Console.WriteLine(((RadioButton)rb).Checked);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 16, 2017 10:24 AM
    Moderator
  • The panel are also created dynamically, how do i reach them for another method? I've named the panels : panel + i(1/2/3/etc). Sorry if I dont understand you right away, im still learning. Also in the code above only checks the first value, what if there are like 5 options in 1 radiobutton menu option(panel).

    Basically what I'm trying to achieve is: Getting the text of the selected radiobutton within that group(9 groups).

    Thanks for your answer so far.

    Niels


    Monday, October 16, 2017 11:12 AM
  • If they are in panels

    var panels = Controls.OfType<Panel>().ToList();
    foreach (Panel p in panels)
    {
        var selectedRadioButton = p.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
        if (selectedRadioButton != null)
        {
            MessageBox.Show($"{p.Name}.{selectedRadioButton.Text}");
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Niels Schutte Monday, October 16, 2017 11:46 AM
    Monday, October 16, 2017 11:44 AM
    Moderator
  • Thank you so much for this, been struggling for hours!
    Monday, October 16, 2017 11:46 AM