Welcome to Gaia! :: View User's Journal | Gaia Journals

 
 

View User's Journal

neil4salinas Journal
neil4salinas Personal Journal
How to Add Radio Buttons to a C# Windows Form
User Image - Blocked by "Display Image" Settings. Click to show.

private Panel langPanel = new Panel();

private RadioButton radBritish = new RadioButton();

private RadioButton radFrench = new RadioButton();

private RadioButton radSpanish = new RadioButton();

The panel is required in order to group the radio buttons together. Radio buttons with the same parent (in this case the panel) always act as a single unit so that as one button is clicked all others in the group are unchecked automatically. Radio buttons with different parents (for example in more that one panel) will not interact with each other.

The programmer can then define their new class's main and constructor functions:

public static void Main()

Application.Run(new MainForm());

public MainForm()

setUpRadioButtons ();

The class's constructor function calls another function (setUpRadioButtons) and it's this one that sets up the radio buttons and the other components that are to be used on the form.

Setting Up Radio Buttons with C#

At this point the components have been created but are not yet connected http://pymt.eu/motorola-xpr-4550-mobile-two-way-radio/ to the new form. The programmer must:

State where the component is to be placed on the formAdd the component to the formThe programmer uses the "Location" property to define where the component should lie. In this example the panel has been placed in the top left of the form:

public void setUpRadioButtons ()

langPanel.Location = new Point(0,0);

The location has two coordinates in "x" and "y". Increasing "x" will move a component to the right, andincreasing "y" will move it down the form. Another useful property is "AutoSize". Setting "AutoSize" to true will mean that the height and width of a component will automatically vary according do its contents:

langPanel.AutoSize = true;

Radio buttons share many of the same properties as panels (such as "Location" and "AutoSize" wink :

radBritish.Location = new Point(0,0);

radBritish.AutoSize = true;

However, there are some additional properties for the programmer to use. The "Text" is used to place an appropriate message next to the radio button, and setting "Checked" to true for a radio button will make it the default one:

radBritish.Text = "English";

radBritish.Checked = true;

Each of the radio buttons is set in the same way (but only one should have "Checked" set to true):

radFrench.Location = new Point(radBritish.Width,0);

radFrench.AutoSize = true;

radFrench.Text = "French";

radSpanish.Location = new Point

(radBritish.Width + radFrench.Width,0);

radSpanish.AutoSize = true;

radSpanish.Text = "Spanish";

lblDay.Location = new Point(0,radBritish.Height);

lblDay.AutoSize = true;

And finally the programmer must add the components to the form:

langPanel.Controls.Add(radBritish);

langPanel.Controls.Add(radFrench);

langPanel.Controls.Add(radSpanish);

this.Controls.Add(langPanel);

If the form is compiled at this point then the application user will see three simple but effective radio buttons.

Summary

C# programmers can use radio buttons to provide user options. Radio buttons (like all form components) must have a parent however each separate group of radio buttons must have their own parent (for example different panels).

Radio buttons grouped together with a single parent will act as a single unit, meaning that if a if one of the buttons is selected then all others will be unselected. This enables a C# programmer to provide their users with a set of options.





 
 
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum