Answers
Jul 23, 2010 - 04:04 AM
As always with C# programming the first step in creating an application is to state which libraries arerequited and then to create a new class which extends the basic C# Windows form class:
using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
The next thing to do is to define the components required in the form. In this case a panel and three radio will be required:
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 to the new form. The programmer must:
•State where the component is to be placed on the form
•Add the component to the form
The programm 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”):
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 you will see three simple but effective radio buttons.
Aug 09, 2010 - 02:24 AM
Did my previous post help you at all ?
Add New Comment