Workbook 1 2016v2
Workbook 1 2016v2
using C#
WorkBook 1
Using C# to create forms
Tasks 1 – 9:
Using a Text Box for Input
Creating a MessageBox
Buttons and Icons
Variables
Concatenation of strings
Naming conventions
You should ensure that you have completed the first exercise: “Introduction to C#
Forms”.
This explains the basics of the C# environment within Visual Studio, how you
should save your projects, how to run the program, and how to view the code.
TASK
Notice the Solution Explorer on the right side of your screen. (If you can't see
it, click this command from the View menu).
Notice the Properties, the References and the Program.cs file within this
section. Double click the Program.cs file to open it, and you'll see some
familiar code:
The code between the curly brackets of Main will get executed when the
programme first starts. The last line in the WindowsApplication1 code above is
the one that Runs Form1 when the Application starts.
Program.cs in the Solution Explorer on the right is where the code for Main
lives.
To see the window where you'll write most of your code, right click Form1.cs in
the Solution Explorer (or use View menu):
The menu has options for View Code and View Designer. The Designer is the
Form you can see at the moment. Click View Code from the menu to see the
following window appear (you can also press the F7 key on your keyboard):
The code has a lot of ‘using’ statements. They just mean "using some code that's
already been written".
The code also says ‘partial class Form1’. It's partial because some code is hidden
from you. To see the rest of it (which we don't need to alter), click the plus symbol
next to Form1.cs in the Solution Explorer:
But you don't need to do anything in this window, so you can right click the
Form1.Designer.cs tab at the top, and click Close from the menu.
Click back on the Form1.cs tab at the top to see your form again. If the tab is not
there, right click Form1.cs in the Solution Explorer on the right. From the menu,
select View Designer. Here's what you should be looking at:
From the Debug menu at the top, click Start (Or you can
just press the F5 key on your keyboard.):
NOTE:
6. Use the Properties Window to change the Location of your button to a position
on the form of 100, 50. (100 means 100 units from the left edge of the form; 50
means 50 units down from the top of the form.)
7. A Form also has lots of Properties. Click away from the button and on to the
Form itself. The Properties for the form will appear in the Properties Window.
Change the Text Property of the Form to A First Message.
8. The Form, like the button, also has a Size Property. Change the Size of the Form
to 300, 200.
When you changed the Text property of the Form, you changed the text that runs
across the blue bar at the top, the text in white. You can type anything you like
here, but it should be something that describes what the form is all about or what
it does. Often, you'll see the name of the software here, like Microsoft Word, or
Microsoft Visual C# 2010 Express Edition.
Task 2
What we want to do now is to display a message box whenever the button is
clicked. So we need the coding window.
1. To see the code for the button, double click the button you added to the
Form. When you do, the coding window will open, and your cursor will be
flashing inside of the button code.
Notice that there is a pair of curly brackets for the button code:
If you want to write code for a button, it needs to go between the two
curly brackets. We'll add a single line of code in the next part below.
Task 3
If you look at the message box we created in the previous section, you'll notice
there's no Title in the blue area to the left of the red X - it's blank:
1. Position your cursor after the final double quote of "My First Message",
circled in red in the image below:
, "Message"
Again, you need the double quotes. Make sure you include the comma just
before it. Your line of code should look like this:
2. When your line of code looks like the one above, run your programme again.
Task 4
Rather than having just an OK button, you can add buttons like Yes, No, and
Cancel to your C# message boxes. We'll add a Yes and a No button.
1. Return to your coding window. After the second double quote of the Title
you've just added, type another comma. Hit the spacebar on your keyboard
once, and you'll see the list
appear. (If it doesn't appear,
just type a capital letter "M").
Task 5
Adding Icons to a C# Message Box
Another thing you can add to brighten up your Message Box is an Icon.
Variables in C#
Programmes work by manipulating data stored in memory. These storage areas
come under the general heading of Variables. In this section, you'll see how to set
up and use variables.
You'll see how to set up both text and number variables.
We'll start with something called a String variable.
String Variables in C#
String variables are always text. We'll write a programme that takes text from a
text box, stores the text in a variable, and then displays the text in a message box.
1. If you've got your project open from the previous section, click File from the
menu bar at the top of Visual C#.
5. For the Name, type String Variables; use your personal location.
7. Add a button to the form, just like you did in the previous section.
8. Click on the button to select it (it will have the white squares around it),
and then look for the Properties Window in the bottom right of Visual Studio.
Name: btnStrings
Location: 90, 175
Size: 120, 30
Text: Get Text Box Data
A Label is just that: a means of letting your users know what something is, or what
it is for.
10. To add a Label to the form, move your mouse over to the Toolbox on the
left. Click the Label item under Common Controls:
11. Change the following properties of your label, just like you did for the
button:
Location: 10, 50
Text: Name
15. Double click your button to open up the coding window. Your cursor will be
flashing inside of the curly brackets for the button code:
string firstName;
17. After the semi-colon, press the enter key on your keyboard to start a new
line.
Naming conventions
The only characters that you can use in your variable names are letters, numbers,
and the underscore character ( _ ). Also, you must start the variable name with a
letter, or underscore. You'll get an error message if you start your variable names
with a number. So these are OK:
firstName
first_Name
firstName2
Notice that all the variable names above start with a lowercase letter. Because
we're using two words joined together, the second word starts with an uppercase
letter. It's recommended that you use this format for your variables (called
camelCase notation.) So firstName, and not Firstname.
After setting up your variable (telling C# to set aside some memory for you), and
giving it a name, the next thing to do is to store something in the variable.
18. Add the following line to your code (don't forget the semi-colon on the end):
firstName = textBox1.Text;
Now that we have stored the text from the text box, we can do something with it.
In our case, this will be to display it in a message box.
MessageBox.Show(firstName);
3. Type something in your text box, and then click the button. You should see
the text you typed.
Task 8
Assigning text to a String Variable
As well as assigning text from a text box to your variable, you can assign text like
this:
firstName = "James";
On the right hand side of the equals sign, we now have some direct text
surrounded by double quotes. This then gets stored into the variable on the left
hand side of the equals sign.
1. To try this out, add the following two lines just below your MessageBox line:
firstName = "James";
MessageBox.Show(firstName);
string firstName;
firstName = textBox1.Text;
Messagebox.Show(firstName);
firstName = "James";
Messagebox.Show(firstName);
}
Task 9
Concatenation in C#
Concatenation is joining things together. You can join direct text with variables, or
join two or more variables to make a longer string.
string messageText;
3. We want to store some text inside of this new variable, so add the following
line of code just below string messageText:
When the message box displays, we want it say some thing like "You name is John".
The variable we've called messageText holds the first part of the string, "Your
name is ". And we're getting the person’s name from the text box:
The person's name is being stored in the variable called firstName. To join the two
together (concatenate) C# uses the plus symbol ( + ).
messageText + firstName
MessageBox.Show(messageText + firstName);
4. Amend your MessageBox line so it's the same as the one above. Here's the
coding window:
Here, we're not storing the text in a variable called messageText. Instead, it's just
direct text surrounded by double quotes. Notice, though, that we still use the plus
symbol to join the two together.