Вы находитесь на странице: 1из 3

COMPSCI 280 2015:

C#/.NET Lab Sheet 1


First program
Getting started
Open Visual Studio 2013 (under Development Environments in the lab image) and do
File/New/Project.
Select Visual C# as your language, browse to a handy location and select Console Application.
The result is a new Solution. Using the Solution Explorer window you can navigate to Program.cs
which is the C# source file containing the Program class definition and the definition of the Main
method for this class. We can write our program logic here.
Saying Hello
In the Main method, add the line:
Console.WriteLine(Hello world!);
Note if you just pasted that from this document the smart quotes will cause an error in VS.
Try running it (pressing the Start button or F5). Gee, it went by fast! Add another line this will
make the program pause until you hit Enter before completing.
Console.ReadLine();
Run it again. Congratulations on completing your first C# program!
Formatted Printing
Try adding in the following lines before the ReadLine and give it a run.
Console.WriteLine("The time is {0:t}", DateTime.Now);
Console.WriteLine("The date is {0:d}", DateTime.Now);
Note the difference in output for the time (t) versus date (d) formatting for the first parameter
(parameter index 0). You can also try format expressions like {0:yyyy-MM-dd}. Why do you reckon
we use capital Ms for the month part of the format expression?
Strings
Lets say Hello a little more boldly. Insert the following into the Main method and run.
string s = "Hello world!";
for (int i = 0; i < s.Length; i++)
Console.Write("-" + s.Substring(i, 1).ToUpper());
Console.WriteLine("-");
Note the indexed for loop structure and the use of string methods Length, Substring and ToUpper.

Substitute the above for loop with this one:


foreach (char c in s)
Console.Write("-" + (char)( c>96&&c<123? c-32 : c));
Hey, it does the same thing. Do you understand why? Hints: The ? : operator pair is like an
IF/ELSE; (char) castes the result back to character; and the ASCII code for lowercase a is 97
whereas ASCII for capital A is 65.

A Windows Forms application


Getting started
In Visual Studio 2013 do File/New/Project. This time select Windows Forms Application. You start
off looking at a blank grey square called Form1.
Select View/Toolbox from the top menu to open a list of controls to drag and drop onto the form.
From Common Controls select a Button and drag it onto the form. Resize the form, and resize and
reposition the button to your satisfaction.
Double-click on the button. This causes VS to event handler code into your project (a click event
method in the definition of the Form1 class. Write into that method this code:
MessageBox.Show("Hello world!");
Run the application. Press the button. Congratulations youve said Hello world! with a forms
application!
Properties of controls
Return to the form design, open the toolbox again and drag a Label control onto the form.
With the new label control still selected, note the Properties window. With this you can change
things about the control. For instance, the Name property is what the control is called in the
program code change it to MyLabel. The Text property sets the initial text of the label set it to
Initial. And theres a whole submenu of Font properties give it a font style that pleases you.
Changing properties at run-time
Double-click on the form itself. This causes VS to create the forms load event handler. Write the
following code into that method:
MyLabel.Text = String.Format("The time is {0:t}", DateTime.Now);
Run the program. You see that the load event runs when the program is run (and hence when the
form is opened) and has set the text on your label to show the current time. Also note how we used
the String classs Format method, which creates formatted results just like Console.WriteLine, but
now youre able to direct the result onto a control on your form.
You can also change label properties after the form has loaded. For instance, you can change the
buttons click event handler code to set MyLabel.Text = Hello World!. Run the program. Click the
button and see the change.

Appending text
Open the toolbox yet again and this time drag onto the form a Textbox control. Set its Multiline
property to True and ScrollBars property to Vertical. Once you set Multiline to True youll see that
you can resize it vertically as well as horizontally on the form.
Go back to the buttons click event handler and add the following code:
textBox1.AppendText(String.Format("The time is {0:HH:mm:ss}",
DateTime.Now)+Environment.NewLine);
Now run the program and push the button a couple times in succession to see the results. Note how
we used the Environment classs NewLine method to get a reliable carriage return/line feed
sequence.

Вам также может понравиться