Introduction to C # Programming trilogy

Source: Internet
Author: User
Tags definition class definition command line constructor tostring visual studio
Programming C # is Microsoft. NET architecture's main development language, it functions widely and powerful, web developers should not hesitate to
Hug it. In this paper, we pass an example of the conversion between Celsius temperature and Fahrenheit temperature to C #
GUI programming, designed to lead you into the powerful and magical world of programming in C #.
Preparation conditions
To understand the examples of this article, you first have a basic understanding of C # and object-oriented programming. About C # 's basic
Knowledge See Getting Started with C #
This article. To compile and run an example application, you need to download
The. NET Framework SDK, and one of its current versions is Beta 1.
As a program developer, we all know that creating a typical windows-based application should include the following
Some basic steps: Create an appropriate form, add controls to the form, and then increase the code that responds to user events

After C # and the. NET framework appear, the tools needed to complete these steps are empty in the system.winforms name
Found in the room.
 
The first step is to create a form
This is very simple, just create a class derived from the System.WinForms.Form class, and the appropriate
Can be initialized with the properties of the In our example, the class definition starts like this:
public class TempConverter:System.WinForms.Form {
.
.
.
}
Here's the main window (form) view we want:
We would like the form to have the following characteristics:
-Window size is 180 times 90 pixels
-Ability to change the window size without giving the user
-the caption displayed in the title bar is +c-> +f/+f-> +c
-Initial state the form appears in the center of the screen
-Do not want Help button (application is too simple, do not need help button)
-Do not provide users with the ability to maximize applications
(because everything is visible within the given window size, so there is no need to maximize)
Initializing a form to a given specification involves setting some properties of the Tempconverter object. Some genera
Sex has a way of changing values, while other attributes are modified directly by updating the appropriate instance variables. The following are the
Code. If you want to get about WinForms
class, the documentation provided by the. NET Framework SDK can be considered a very
A good reference.
This. SetSize (180,90);
This. BorderStyle = FormBorderStyle.FixedDialog;
This. Text = "+c-> +f/+f-> +c";
This. StartPosition = Formstartposition.centerscreen;
This. HelpButton = false;
This. MaximizeBox = false;
Now put the code together to compile and run to see what the form will look like after it runs. Here to use the class
Definition, create a constructor that contains the above code to initialize the appearance of the main window, and to create
A Main method to create an example of a class. Here's the code to do the job:
public class TempConverter:System.WinForms.Form {
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;
}
public static void Main () {
Application.Run (New Tempconverter ());
}
}
The only line with the Main () method is the new code.
Application.Run (New Tempconverter ());
The above line means to start the application with a new form.
Assuming the source file is called TempConverter.cs, execute the following command to compile the code:
Csc/r:system.dll/r:microsoft.win32.interop.dll/r:system.winforms.dl
L TempConverter.cs
This is no longer a detailed explanation for compiling commands, because when visual Studio. NET is available, you do not have to issue a command line
compiler command.

Step two to add controls to the form
The next step is to add controls to the form. We create an instance variable for each control, and change these new instances
To initialize, and finally put each control in the form. Here is the appearance of the form after adding the control to
and updated code:
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);
Bnftoc.tabindex = 3;
Bnftoc.text = "+f to +c";
Bnftoc.setsize (70,25);
Bnftoc.setlocation (90,35);
This. Controls.Add (Ttempcel);
This. Controls.Add (Ltempcel);
This. Controls.Add (Ttempfah);
This. Controls.Add (Ltempfah);
This. Controls.Add (Bnctof);
This. Controls.Add (BNFTOC);
}
The above code first creates two labels, two text boxes, and two buttons, then initializes each control and
it into the form. The specific meaning is as follows:
-SetSize () initializes the control's dimensions
-SetLocation () initializes the position of the control in the form
-Set the control's TabStop property to False to indicate that the control is never focused
-Set TabIndex to X to focus the control when you hit the TAB key x times
-The control's Text property represents the text information that is displayed on it
-this. Controls.Add () indicates that a control is placed on the form, and to quickly add each control, you can
? write: this. Controls = new
Control[] {ttempcel, Ltempcel, Ttempfar?}

Step three to increase response to user event code
There is a final step to be done, is to add a method to capture the button click event. Here's the point.
Click the code from Celsius to Fahrenheit button:
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;
}
Line four to line eighth (that is, everything in the Try area) retrieves the value in the Celsius (Celsius) text box. If
It is a double-byte number and stores it in Dtempcel, otherwise it clears two text boxes and exits. Then
Used to store in Dtempcel
We use the formula in line 9th to store the same temperature in the Fahrenheit. Put this new value in the
Fahrenheit (Fahrenheit) text box, and then place the cursor in each text box to set the pointer
To the beginning. (If you don't set the pointer to the beginning, we'll see the end of a long number, looking at the beginning
You must scroll the mouse).
The following is the code for the Fahrenheit button, which will accomplish the same task, except for the opposite process:
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 link the appropriate Click event Capture method with the Click event of the button. To complete this
Step, we place the following two lines in the constructor of the class:
Bnctof.click + = new EventHandler (This.bnctof_click);
Bnftoc.click + = new EventHandler (This.bnftoc_click);

Finally, take a look at 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);
= 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;
}
}
Conclusion
So far, you've seen a complete process of programming with C #. Although this example is very simple, but hemp
Although the sparrow is small, spite, understand the principle of which, you can play a full role in the powerful function of C #.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.