Step 3: Add code to respond to user events
The last step is to add a method to capture button click events. Here is the code for clicking the button from Celsius to Fahrenheit:
Private void bnCtoF_Click (Object sender, EventArgs e ){
Double dTempCel = 0;
Double dTempFah = 0;
Try {dTempCel = tTempCel. Text. ToDouble ();}
Catch (Exception ){
TTempCel. Clear ();
TTempFah. Clear ();
Return;
}
DTempFah = 1.8 * dTempCel + 32;
TTempFah. Text = dTempFah. ToString ();
TTempFah. Focus ();
TTempFah. SelectionStart = 0;
TTempFah. SelectionLength = 0;
TTempCel. Focus ();
TTempCel. SelectionStart = 0;
TTempCel. SelectionLength = 0;
}
Rows 4 to 8 (that is, everything in the try area) retrieve the values in the Celsius (Celsius) text box. If it is a dual-byte number, it will be stored in dTempCel, otherwise it will clear the two text boxes and exit. Then, it is stored in dTempCel
Value in, we store the same temperature in Fahrenheit using the formula in row 9th. Display the new value in the Fahrenheit (Fahrenheit) text box, and place the cursor in each text box to set the pointer to the beginning. (If you do not set the pointer to the beginning, we will see the end of a long number. You must scroll the mouse to see the beginning ).
The following is the code of the Fahrenheit button, which completes the same task, but is the opposite:
Private void bnFtoC_Click (Object sender, EventArgs e ){
Double dTempCel = 0;
Double dTempFah = 0;
Try {dTempFah = tTempFah. Text. ToDouble ();}
Catch (Exception ){
TTempCel. Clear ();
TTempFah. Clear ();
Return;
}
DTempCel = (dTempFah-32)/1.8;
TTempCel. Text = dTempCel. ToString ();
TTempCel. Focus ();
TTempCel. SelectionStart = 0;
TTempCel. SelectionLength = 0;
TTempFah. Focus ();
TTempFah. SelectionStart = 0;
TTempFah. SelectionLength = 0;
}
Next, we need to associate the appropriate Click Event capture method with the Click event of the button. To complete this step, we put the following two rows in the constructor of the class:
BnCtoF. Click + = new EventHandler (this. bnCtoF_Click );
BnFtoC. Click + = new EventHandler (this. bnFtoC_Click );
Finally, see the complete code:
Using System;
Using System. WinForms;
Public class TempConverter: System. WinForms. Form {
Label lTempFah = new Label ();
Label lTempCel = new Label ();
TextBox tTempFah = new TextBox ();
TextBox tTempCel = new TextBox ();
Button bnCtoF = new Button ();
Button bnFtoC = new Button ();
Public TempConverter (){
This. SetSize (180,90 );
This. BorderStyle = FormBorderStyle. FixedDialog;
This. Text = "+ C-> + F/+ F-> + C ";
This. StartPosition = FormStartPosition. CenterScreen;
This. HelpButton = false;
This. MaximizeBox = false;
TTempCel. TabIndex = 0;
TTempCel. SetSize (50, 25 );
TTempCel. SetLocation (13, 5 );
LTempCel. TabStop = false;
LTempCel. Text = "C ";
LTempCel. SetSize (25, 25 );
LTempCel. SetLocation (65,5 );
TTempFah. TabIndex = 1;
TTempFah. SetSize (50, 25 );
TTempFah. SetLocation (90,5 );
LTempFah. TabStop = false;
LTempFah. Text = "F ";
LTempFah. SetSize (25, 25 );
LTempFah. SetLocation (142,5 );
BnCtoF. TabIndex = 2;
BnCtoF. Text = "C to F ";
BnCtoF. SetSize (70, 25 );
BnCtoF. SetLocation (13, 35 );
BnCtoF. Click + = new EventHandler (this. bnCtoF_Click );
BnFtoC. TabIndex = 3;
BnFtoC. Text = "F to C ";
BnFtoC. SetSize (70, 25 );
BnFtoC. SetLocation (90,35 );
BnFtoC. Click + = new EventHandler (this. bnFtoC_Click );
This. Controls. Add (tTempCel );
This. Controls. Add (lTempCel );
This. Controls. Add (tTempFah );
This. Controls. Add (lTempFah );
This. Controls. Add (bnCtoF );
This. Controls. Add (bnFtoC );
File: // =New Control [] {tTempCel, lTempCel, tTempFah, lTempFah, bnCtoF, bnFtoC };
}
Public static void Main (){
Application. Run (new TempConverter ());
}
Private void bnCtoF_Click (Object sender, EventArgs e ){
Double dTempCel = 0;
Double dTempFah = 0;
Try {dTempCel = tTempCel. Text. ToDouble ();}
Catch (Exception ){
TTempCel. Clear ();
TTempFah. Clear ();
Return;
}
DTempFah = 1.8 * dTempCel + 32;
TTempFah. Text = dTempFah. ToString ();
TTempFah. Focus ();
TTempFah. SelectionStart = 0;
TTempFah. SelectionLength = 0;
TTempCel. Focus ();
TTempCel. SelectionStart = 0;
TTempCel. SelectionLength = 0;
}
Private void bnFtoC_Click (Object sender, EventArgs e ){
Double dTempCel = 0;
Double dTempFah = 0;
Try {dTempFah = tTempFah. Text. ToDouble ();}
Catch (Exception ){
TTempCel. Clear ();
TTempFah. Clear ();
Return;
}
DTempCel = (dTempFah-32)/1.8;
TTempCel. Text = dTempCel. ToString ();
TTempCel. Focus ();
TTempCel. SelectionStart = 0;
TTempCel. SelectionLength = 0;
TTempFah. Focus ();
TTempFah. SelectionStart = 0;
TTempFah. SelectionLength = 0;
}
}
Knot
So far, you have seen a complete process of programming with C. This example is very simple, but the sparrow is small and dirty. After understanding the principles, you can show your skills and give full play to the powerful functions of C.