Your First C# Windows Application
This example is to get you started creating Windows Forms applications in C# using
SharpDevelop
.
If you don't already have
SharpDevelop , download and install it.
Create a new Combine and select a C# Windows Application Project.

Below the editor view, select the Design tab. Select the Form in the Form Designer
and change the Text property to Calculator.

Add 3 TextBox and 4 Button Controls to the Form.
For the 3 TextBox controls, clear the Text property, and change the (Name)
properties to Op1TextBox, Op2TextBox, and ResultTextBox. Change the text on the
buttons to "+, -, *, & /". Change the (Name)s to "AddButton, SubtractButton,
MultiplyButton, and DivideButton. Select all 4 buttons then adjust the Font
property in the Properties Window.

Double click each button to create its event handler, then modify the code as shown
here.
void AddButtonClick(object sender, System.EventArgs e)
{
double dAns;
dAns = double.Parse( Op1TextBox.Text ) + double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
void SubtractButtonClick(object sender, System.EventArgs e)
{
double dAns;
dAns = double.Parse( Op1TextBox.Text ) - double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
void MultiplyButtonClick(object sender, System.EventArgs e)
{
double dAns;
dAns = double.Parse( Op1TextBox.Text ) * double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
void DivideButtonClick(object sender, System.EventArgs e)
{
double dAns;
dAns = double.Parse( Op1TextBox.Text ) / double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
Run the application and type values into the two operand text boxes. Click the four
function button to test the application. This program has no error checking so if
you give it invalid input it will crash.
Add some simple error handling.
void AddButtonClick(object sender, System.EventArgs e)
{
double dAns;
try
{
dAns = double.Parse( Op1TextBox.Text ) + double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
void SubtractButtonClick(object sender, System.EventArgs e)
{
double dAns;
try
{
dAns = double.Parse( Op1TextBox.Text ) - double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
void MultiplyButtonClick(object sender, System.EventArgs e)
{
double dAns;
try
{
dAns = double.Parse( Op1TextBox.Text ) * double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
void DivideButtonClick(object sender, System.EventArgs e)
{
double dAns;
try
{
dAns = double.Parse( Op1TextBox.Text ) / double.Parse( Op2TextBox.Text );
ResultTextBox.Text = dAns.ToString();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
}
wburris at telusplanet dot net |