how to get the state of dynamically created radio button

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5788 Views - Last Post: 06 March 2012 - 09:29 AM Rate Topic: -----

#1 ken2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 05-December 10

how to get the state of dynamically created radio button

Posted 06 March 2012 - 06:28 AM

int count = 0;
                RadioButton[] personRadio = new RadioButton[rowsPerson];
                foreach (DataRow dataRow in dataTable.Rows)
                {

                    string strFirtsname = dataRow["firstname"].ToString();
                    string strLastname = dataRow["lastname"].ToString();

                    personRadio[count] = new RadioButton();
                    personRadio[count].Name = personRadio[count].ToString();
                    personRadio[count].Text = strFirtsname + " " + strLastname;
                    personRadio[count].TabIndex = count;
                    personRadio[count].Location = new Point(20, count * 20);
                    personRadio[count].AutoCheck = true;
                    personPanel.Controls.Add(personRadio[count]);
                    personPanel.Visible = true;
                    count++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sqlConnection.Close();
            }
        }

Radio buttons are dynamically created based on the number of items that are in the database table(Person) which is working fine, my question is how do I get the selected value once i click any of this on the panel since they are dynamically created? Please help me out. Thanks. God bless in Jesus name.

Is This A Good Question/Topic? 0
  • +

Replies To: how to get the state of dynamically created radio button

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 06:33 AM

Most radio button used the .Checked keyword, not sure for C# let me check....

example

//return
boolResult = radio1.checked;

//conditional
if radio1.checked
{

}

//assignment
radio1.checked = True;


This post has been edited by Toadill: 06 March 2012 - 06:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 06:40 AM

This seems to be the correct way, I would not see it being any different from a constant control.
Was This Post Helpful? 0
  • +
  • -

#4 ken2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 05-December 10

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 06:49 AM

.....but it dos not respond when i try to troubleshoot it with a messagebox. What else do you think I need to do?
Was This Post Helpful? 0
  • +
  • -

#5 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 06:58 AM

What do you mean it does not respond
What error message are you getting copy and paste it.
also you could try setting break points in your code to find out where it is throwing errors.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:01 AM

You should handle the CheckedChanged event for the radiobutton.

http://msdn.microsof...kedchanged.aspx

Then you could show the MessageBox for the checked radiobutton's Text.

private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
     RadioButton rb = sender as RadioButton;
     if (rb.Checked)
     {
          MessageBox.Show(rb.Text);
     }
}



You should also look into using a generic list instead of an array.
Was This Post Helpful? 0
  • +
  • -

#7 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:05 AM

I am a little confused by this

//This could be causing as error since you can not have spaces, could maybe use Trim
personRadio[count].Name = personRadio[count].ToString()




Great idea eclipsed4utoo :rockon:

This post has been edited by Toadill: 06 March 2012 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 ken2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 05-December 10

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:12 AM

Basically that is what I did, and i went on and click one of the dynamically created radio button and nothing happened. I'd expected the messagebox to display the selected radiobutton text. Thanks what other suggestion do you think I should use?

Thanks you talked about the Trim can you discuss more on this?
Was This Post Helpful? 0
  • +
  • -

#9 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:15 AM

Set a break point on line 10 and 11 of your initial post. Tell me what is being stored under name. I am just wondering if your radio buttons are being assigned valid names.

I am not sure what this would return.
personRadio[count].ToString()
Was This Post Helpful? 0
  • +
  • -

#10 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:21 AM

If you could just tell me what is being stored in the controls name property, this could be causing problems if the data is the same more then once, or if it is just junk it may include invalid characters I would suggest just naming the control the persons name.
Was This Post Helpful? 0
  • +
  • -

#11 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 07:28 AM

Spoiler


If you wanted the control name to just be a number

Spoiler

This post has been edited by Toadill: 06 March 2012 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#12 ken2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 05-December 10

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 08:02 AM

the messagebox shows this when i try to get the name by doing this: MessageBox.show(personRadio[count].Name) that "System.Windows.Forms.RadioButton, Checked:false". does this mean it can not read the values attac to it?
Was This Post Helpful? 0
  • +
  • -

#13 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 08:12 AM

Are you saying that once you set values they are not printing correctly if so this is because you do not have a event handler like eclipsed4utoo said earlier

Quote

You should handle the CheckedChanged event for the radiobutton.

http://msdn.microsof...kedchanged.aspx

Then you could show the MessageBox for the checked radiobutton's Text.

1	private void RadioButton_CheckedChanged(object sender, EventArgs e)
2	{
3	     RadioButton rb = sender as RadioButton;
4	     if (rb.Checked)
5	     {
6	          MessageBox.Show(rb.Text);
7	     }
8	}


Okay try this once take a real control and drag it to a form in a new project then click events in the properties area. The events and properties can all be set through code as suggested.

Btw just double click an event to get the code needed. Such as RadioButton_CheckedChanged event

This post has been edited by Toadill: 06 March 2012 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 08:44 AM

You need to subscribe to the event...

personRadio[count].CheckedChanged += new EventHandler(RadioButton_CheckedChanged);



Then use my code from earlier.
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: how to get the state of dynamically created radio button

Posted 06 March 2012 - 08:52 AM

View PostToadill, on 06 March 2012 - 10:15 AM, said:

Set a break point on line 10 and 11 of your initial post. Tell me what is being stored under name. I am just wondering if your radio buttons are being assigned valid names.

I am not sure what this would return.
personRadio[count].ToString()


It would return System.Windows.Forms.RadioButton,Checked:{Checked Value}.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2