Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lecture Notes on Chapter 2 - Introduction to Visual C#

The document provides lecture notes on Visual C# programming, focusing on creating graphical user interfaces (GUIs) using controls like Labels, Textboxes, and Buttons. It covers naming conventions, event handling, and the use of message boxes, as well as best practices for writing code, including comments and error handling. Additionally, it emphasizes the importance of correct sequence in programming statements and offers guidance on seeking help when needed.

Uploaded by

forthare193
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture Notes on Chapter 2 - Introduction to Visual C#

The document provides lecture notes on Visual C# programming, focusing on creating graphical user interfaces (GUIs) using controls like Labels, Textboxes, and Buttons. It covers naming conventions, event handling, and the use of message boxes, as well as best practices for writing code, including comments and error handling. Additionally, it emphasizes the importance of correct sequence in programming statements and offers guidance on seeking help when needed.

Uploaded by

forthare193
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Programming for Business

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

• Remember that a GUI is a graphical user interface.

• Remember that you change an object’s characteristics by setting its properties. In


the Visual Studio IDE, you can use the Properties window to see and change the
properties of a control.

Slide 4 – Controls at Design-Time

• The following controls are the ones that we’ll use first: Label, Textbox and Button.

o A Label is an output-only control (i.e. it is used to display text).

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.

Slide 5 – Controls at Design-Time

• We create a program’s GUI at design-time and the user interacts with our program
at run-time.

Slide 7 – Controls at Design-Time

• Always remember to change the default name of a control to something meaningful


and descriptive. Also, be sure to include the correct prefix in front of the name:

Control/Object Prefix to use


Form frm
Label lbl
Textbox txt
Button btn

By Thayne Breetzke Copyright © Thayne Breetzke (University of Fort Hare) 1


Introduction to Programming for Business

Slide 8 – Controls at Design-Time

• There are rules and naming conventions for naming forms and controls.

o For a name to be accepted by Visual Studio, it must follow Visual Studio’s


naming rules.

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.

Slide 9 – Events and the Button Control

• 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.

o The message is called an event (e.g. a mouse click event).

o We can write computer code to respond to an event (i.e. to do something


when an event happens).

Slide 10 – Events and the Button Control

• 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.

Slide 11 – Events and the Button Control

• This is an empty event handler that will respond when the user clicks the button
named btnButton.

private void btnButton_Click(object sender, EventArgs e)


{
:
}

o We don’t need to write this as it is automatically created for us when we


double click the btnButton button.

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.

By Thayne Breetzke Copyright © Thayne Breetzke (University of Fort Hare) 2


Introduction to Programming for Business

Slide 12 – The Message Box

• To display a message in a message box, you could write the following C#


statement:

MessageBox.Show(“Thanks for clicking the button!”);

o Note that every C# statement must end with a semi-colon (;).

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.

Slide 13 – The Label Control

• Remember that a Label is an output-only control (i.e. it is can only display output).

Slide 14 – The Label Control

• The following C# statement displays “Hello” in the label named lblOutput.

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.

• The following C# statement clears the text in the label:

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;

o Don’t forget that assignment statements work from right to left.

By Thayne Breetzke Copyright © Thayne Breetzke (University of Fort Hare) 3


Introduction to Programming for Business

Slide 15 – The Sequence Concept and Statements

• A program will have many statements. Obviously, they must be arranged in the
correct sequence otherwise the program will not work correctly.

Slide 16 – Adding Meaning with Comments

• Add comments to your program only when the code is not easy to understand.

• The following are different ways of writing comments:

// This is a one-line comment

/* This is a comment that can span multiple lines */

Slide 17 – Adding Meaning with Comments

• 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.

Slide 18 – Blank Lines and Indentation

• 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.

Slide 19 – Using Code to Close an Application’s Form

• 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.

By Thayne Breetzke Copyright © Thayne Breetzke (University of Fort Hare) 4


Introduction to Programming for Business

Slide 20 – Dealing with Syntax Errors

• Visual Studio underlines syntax errors in red. Hold the mouse over the error for
more information about the error.

Slide 21 – Program Errors

• 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.

• Check the Error List window for compilers 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.

Lectures and Demonstrations

• The following video on Blackboard shows you how to work with buttons, message
boxes and labels in Visual Studio:

Working with Buttons, Message Boxes and Labels

By Thayne Breetzke Copyright © Thayne Breetzke (University of Fort Hare) 5

You might also like