DotNet Tutorials

Server Intellect

 Programming Controls in a Windows Form in C#

This tutorial will show how we can use the many .NET Controls in a Windows Form and how we can use them to gather information from the user and provide more interactivity. C# version.

In this tutorial we are going to look at ways we can implement .NET Controls into a Windows Form and how we can code them to make them functional to our needs. We are going to see how to implement and utilize textboxes, buttons, radio buttons, a list box, and a combo box.

The resulting Form will look something like this:

First, we will start off by adding a button to initiate a Message Box that poses a question to the user. We simply drag a button onto the form, and create the event handler for its click:

private void button1_Click(object sender, EventArgs e)
{
DialogResult yourAnswer = MessageBox.Show("Do you understand how this works?", "Caption",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (yourAnswer == DialogResult.Yes)
MessageBox.Show("You clicked Yes, well done.");
else
MessageBox.Show("You clicked No!");
}

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

In this block of code, we are making a Message Box appear as soon as the user clicks the button. The Message Box will have Yes and No buttons and ask if the user understands how it works. Then we capture the user's answer, and display another Message Box dependant upon that answer.

Next, we will create a TextBox and a button. When the button is pressed, a Message Box will be displayed, with the data the user typed in. If no data was input, the user will be instructed to do so.

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
MessageBox.Show("Your name is: " + textBox1.Text + ".");
else
MessageBox.Show("Please type your name.");
}

Next, we will add the Combo Box and add code to the SelectedIndexChanged event handler, and again, display a Message Box to the user, announcing the option they choose:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("You are: " + comboBox1.SelectedItem.ToString());
}

We can also add a List Box Control, a Text Box and a Button to let the user add their own items into the List Box. We do this simply with the following code:

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
listBox1.Items.Add(textBox2.Text);
else
MessageBox.Show("Please enter text to add.");
}

We can also add another buttons allowing the user to clear the contents of the List Box:

private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}

Finally, we can add a bunch of Radio Buttons to allow the user to set the background color of the Form, from a pre-determined set of colors:

private void radioButtonGrey_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.WhiteSmoke;
}

private void radioButtonGreen_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.Green;
}

private void radioButtonWhite_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.White;
}

private void radioButtonBlue_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
}

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

The entire code-behind will look something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SimpleMessageBox_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Text = "dotNetTutorials.com";
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult yourAnswer = MessageBox.Show("Do you understand how this works?", "Caption",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (yourAnswer == DialogResult.Yes)
MessageBox.Show("You clicked Yes, well done.");
else
MessageBox.Show("You clicked No!");
}

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
MessageBox.Show("Your name is: " + textBox1.Text + ".");
else
MessageBox.Show("Please type your name.");
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("You are: " + comboBox1.SelectedItem.ToString());
}

private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
listBox1.Items.Add(textBox2.Text);
else
MessageBox.Show("Please enter text to add.");
}

private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}

private void radioButtonGrey_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.WhiteSmoke;
}

private void radioButtonGreen_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.Green;
}

private void radioButtonWhite_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.White;
}

private void radioButtonBlue_CheckedChanged(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect