.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

The Windows Controls Program

Posted By:Shashi Ray       Posted Date: February 28, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

The Windows Controls Program


The text in the label changes whenever you change the


·  Font size from the combo box

·  Font color from the radio buttons

·  Font bold from the check box.


For the check box, we create a new font which is either bold or not depending on the state of the check box:


private void ckBold_CheckedChanged(object sender, EventArgs e)


if (ckBold.Checked ) {

lbText.Font =new Font ("Arial",

fontSize,FontStyle.Bold );


else {

lbText.Font = new Font ("Arial", fontSize);




When we create the form, we add the list of font sizes to the combo box:


private void init()


fontSize = 12;

cbFont.Items.Add ("8");

cbFont.Items.Add ("10");

cbFont.Items.Add ("12");

cbFont.Items.Add ("14");

cbFont.Items.Add ("18");

lbText.Text ="Greetings";

tips.SetToolTip (btPush, "Press to add text to list box");

tips.SetToolTip (lsCommands, "Click to copy to text box");



When someone clicks on a font size in the combo box, we convert that text to a number and create a font of that size. Note that we just call the check box changing code so we don't have to duplicate anything.


private void cbFont_SelectedIndexChanged(object sender, EventArgs e)


fontSize= Convert.ToInt16 (cbFont.SelectedItem );

ckBold_CheckedChanged(null, null);



For each radio button, we click on it and insert color-changing code:


private void opGreen_CheckedChanged(object sender, EventArgs e) {

lbText.ForeColor =Color.Green;



private void opRed_CheckedChanged(object sender, EventArgs e)


lbText.ForeColor =Color.Red;



private void opBlack_CheckedChanged(object sender, EventArgs e) {

lbText.ForeColor =Color.Black;



When you click on the ListBox, it copies that text into the text box, by getting the selected item as an object and converting it to a string.


private void lsCommands_SelectedIndexChanged(object sender, EventArgs e)


txBox.Text = lsCommands.SelectedItem.ToString ();



Finally, when you click on the File | Exit menu item, it closes the form, and hence the program:


private void menuItem2_Click(object sender, EventArgs e)


this.Close ();




Shashi Ray

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend