You are on page 1of 1

Writing Code for the Form

Página 1 de 1

Writing Code for the Form
C# can insert code templates for event procedures for controls. You modify the code template to specify
what you want to happen when an event occurs, such as when users click the OK button.
1. Double-click the OK button on the form to open the Code Editor window containing an event procedure
for this control.
The name of the event procedure, bOK_Click, is a combination of the control's name as specified in the
control's Name property, an underscore (_), and the name of the event. The Click procedure is the
default procedure for a command button.
2. Type, or copy and paste, these declarations above Private void bOK_Click(object sender,
EventArgs e).
public double radius;
public double depth;
3. Type, or copy and paste, this code between Private void bOK_Click(object sender, EventArgs e)
event's curly brackets.
double radiusResult;
double depthResult;
//Convert strings to doubles and
//make sure that the text in the two
//text boxes on the form are positive,
//non-zero, numeric values
double.TryParse(txtRadius.Text, out radiusResult);
double.TryParse(txtDepth.Text, out depthResult);
if (radiusResult > 0 && depthResult > 0)
{
radius = radiusResult;
depth = depthResult;
//Close the form and continue
Close();
}
else
{
//Display a message box telling the user to type
//positive, non-zero, numeric values in both text boxes
MessageBox.Show("You must type positive, non-zero, numeric values in both Radius and
an Depth.");
}

PREVIOUS TOPIC

NEXT TOPIC

Finishing the Form

Adding Variables for Controls

mk:@MSITStore:C:\Program%20Files\SOLIDWORKS%20Corp\SOLIDWORKS\lang\s... 19/09/2016