0
votes

I have several radio buttons that are dynamically populated on a form and I have set a click event on the dynamically created radio buttons. On click i get a returned value as follows through debugging (eg) "sender { Text = "this is answer one" + Checked = "True"} using code as follows:

    //Radio button click:
    void Form1_Click(object sender, EventArgs e)
    {
        RadioButton rb = sender as RadioButton;
        string radioButtonValue = rb.Text;

        if (radioButtonValue != String.Empty)
        {

        }
    }

The debug values are returned via "RadioButton rb = sender as RadioButton;" - the diffrent radio buttons text is set via a dataset that I call in a local dataset that loops through the dataset and and sets the radio button text accordingly (eg):

 for (int i = 0; i < _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows.Count; i++)
                                    {
                                        radioButtons[i] = new RadioButton();
                                        radioButtons[i].AutoCheck = true;
                                        radioButtons[i].Text = _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows[i]["tbl_QuestionnaireAnswer_Description"].ToString();
                                        radioButtons[i].Location = new System.Drawing.Point(60, 20 + i * 20);
                                        radioButtons[i].Click += new EventHandler(Form1_Click);
                                        panel.Controls.Add(radioButtons[i]);
                                    }

So: wat id like to know is on the radio button click (Form1_Click) event is it possible to return the primary key of the of the selected radio button that I choose and not just the sender { Text = "this is answer one" + Checked = "True"} as I would like to use the primarykey in that dataset to write back to my Database.

Thanks in advance. Kind regards geo

1

1 Answers

1
votes

Most winforms controls contain the Tag property that is used to contain custom user data in the control. You can read more at: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx

So, your solution should be simpler and more concise like this:

for (int i = 0; i < _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows.Count; i++)
{
    radioButtons[i] = new RadioButton();
    radioButtons[i].AutoCheck = true;
    radioButtons[i].Location = new System.Drawing.Point(60, 20 + i * 20);
    radioButtons[i].Tag = _dataSetRadioButtons.Tables["tbl_QuestionnaireAnswer"].Rows[i];
    radioButtons[i].Click += new EventHandler(Form1_Click);
    panel.Controls.Add(radioButtons[i]);
}

This includes a relevant datarow in the radiobutton. The next thing is to get any data from it you need:

//Radio button click:
void Form1_Click(object sender, EventArgs e)
{
    RadioButton radioButton = sender as RadioButton;
    if (radioButton == null)
       return;

    DataRow row = radioButton.Tag as DataRow;
    if (row == null)
       return;

    /* Post any processing here. e.g.
       MessageBox.Show(row["ID"].ToString());
    */
}

This way you have all the data and it's strongly typed, which is a good thing.