Lecture Notes on Chapter 2 - Introduction to Visual C#
Lecture Notes on Chapter 2 - Introduction to Visual C#
Lecture Notes
Chapter 2 – Introduction to Visual C#
These notes provide explanatory notes for the slides for the above topic. They provide extra
content, explanation and guidance that would have been provided in the lecture(s) for this course.
Together, these slides and notes can serve as a “transcript” of the lecture(s) for this topic.
Slide 3 – Forms
• The following controls are the ones that we’ll use first: Label, Textbox and Button.
o A Textbox is an input-output control (i.e. it displays text but also allows the
user to input text).
o A Button is a command button that the user can click to get a program to do
something.
• We create a program’s GUI at design-time and the user interacts with our program
at run-time.
• There are rules and naming conventions for naming forms and controls.
o Naming conventions are different. They are not naming rules imposed by
Visual Studio, but are generally accepted way (or good practice) for naming
things. So, not following the naming conventions may not make a name
invalid in Visual Studio, but it may make the name a bad one.
• How does your computer know when you click the left mouse button? It is because
when you click the button a message is sent to the system to announce that the
button was clicked. The system can choose to respond to or ignore that message.
• Unless you’ve written some code to respond to a click of a button, nothing will
happen when you click the button. An event handler is a method that contains the
code to respond to an event.
• This is an empty event handler that will respond when the user clicks the button
named btnButton.
o We write any code we want between the two curly brackets. This is the code
that will execute when the user clicks the button.
o The text “Thanks for clicking the button!“ becomes part of the
source code for the program (i.e. it is literally written inside the code),
therefore it is called a string literal. Surround every string literal with double
quotes.
• Students love to use message boxes! Only use for important situations that need
the user’s attention. Most of the time, output should be displayed in labels.
• Remember that a Label is an output-only control (i.e. it is can only display output).
lblOutput.Text = “Hello”;
o This statement sets the Text property of the label (i.e. it is assigning the text
to the Text property). Remember that assignment statements work from
right to left.
o Notice that the name of the control that you are dealing with is written first,
followed by the property name. Both are separated by a full stop.
lblOutput.Text = “”;
o Actually, the statement just assigns a blank string to the Text property so
that the label displays nothing.
• The following C# statement gets the text that is in the label named lblLabel2 and
puts the text in the label named lblLabel1 (so both labels show the same text):
lblLabel1.Text = lblLabel2.Text;
• A program will have many statements. Obviously, they must be arranged in the
correct sequence otherwise the program will not work correctly.
• Add comments to your program only when the code is not easy to understand.
• Comments can be useful to help identify logic errors. You can enclose one or more
statements in a comment and the computer will ignore those statements.
• Notice how much easier it is to follow the code when you indent the code correctly
and use blank lines. Be sure to do so when you write your code.
• A user can close your program by clicking the Close button on the top-right corner
of the form. But what if you want your program to close the form? Simply write this
statement:
this.Close();
o this refers to the current form. Close is a method that the form understands
(a behaviour of the form). The statement therefore calls the Close method
of the form to close it.
• You could create an Exit button on a form and write this statement inside the
button’s Click event handler. When the user clicks the button, the program will
close the form.
• Visual Studio underlines syntax errors in red. Hold the mouse over the error for
more information about the error.
• Remember that the compiler will not compile your source code if there are syntax
errors in your code. Therefore, you need to correct all syntax errors.
• Remember that if a program compiles successfully, it does not mean that it is free
of errors. It is only free of syntax errors. It may still have logic errors, called bugs.
These cause the program to not do what it is supposed to (e.g. the algorithm on
which the code is based is incorrect and was not tested properly).
Slide 24 – Help
• When you are stuck, don’t be afraid of searching for help on the Web, Google, or by
using the Help features of Visual Studio.
• The following video on Blackboard shows you how to work with buttons, message
boxes and labels in Visual Studio: