Chapter 8 - Notes and Programs

Introduction to Windows Programming – Chapter 8
Windows Applications vs Console Applications:




(Please skim/read ppg. 401-472)
Windows uses events instead of inputs and outputs read from top to bottom
Extremely different UI when using Visual Studio
Easier to format and more visually appealing
New syntax (this, events, event handlers)
To create a Windows Application: New Project -> Windows Application
When you first open a new program it should look like this, but in the newer version.
In the toolbox there is going to be many things that you can incorporate in to your program. Some of these
objects require you to drag them onto your form such as a button. Once you drag something onto your form
you can edit it in the Properties window (Text, Colors, Font, Background Images, etc.). If you are ready to start
typing code you just double click on the object that you want to code for.
You’ll see two pages:
1.) Form Design, basically what your program will look like when you hit f5 and where you can directly
edit your window application without the use of program statements. On the left the toolbox now has a
ton of options such as buttons, lists, loading screens, which can be put right on your program by clicking
and dragging them onto the window itself with your cursor. When you place an object down, on the left
you will see a whole list of different properties that can be used to change the object (text, font, font
color, size, background color, position on the window, etc.). This is provided by Visual Studio itself.
2.) Form.cs, what your first page looks like in code. When you place an object down on the first page,
Visual Studio will automatically add the code to your second page. Every “object” is technically its own
class, and with every class it has its very own properties. All of the properties such as the color, font, and
size found in the first page are properties of your object’s class. The keyword this mentioned earlier is
used to signal Visual Studio’s IntelliSense for class properties, however you probably won’t have to use
it often as you can just edit properties much faster on the first page.
Therefore, Visual Studio saves a ton of time by automatically adding the lines of code by you simply inputting
values on the first page instead.
Here is an example of a block of code that would generate if you double clicked a button.
private void button1_Click(object sender, EventArgs e)
{
}
Message Boxes
Example of how to create a message box:
MessageBox.Show("Good", "Today I feel . . .");
“Good” would be the text in the message box and “Today I feel . . .” would be the caption at the top.
You can also pick what buttons you want on your message box.
Example:
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
Use the drop-down list to see the other options.
Multiple Forms
To create a second form just right click on your program name in the Solution
Explorer and click Add and then click Windows Form.
One way to get to your second from your first is to use a button.
Example:
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
Tool Tips
Tool tips could be useful for when you don’t want to write instructions or other text directly on the form
window.
Example:
private void textBox1_MouseHover(object sender, EventArgs e)
{
ToolTip tooltip1 = new ToolTip();
tooltip1.SetToolTip(this.textBox1, "TYPE YOUR NAME!");
}
Tool tips can also fade out after an amount of time too and you can
set that time yourself.
In this example the tool tip will stay up whenever the cursor is
hovering on the text box.
Events: Events are what make your windows applications actually do something. This is the main topic
covered in Chapter 9, so I won’t go too in depth. On the left side again on the properties menu, click on the
lightning bolt; this is the events menu. Double clicking on one places a method, or event handler in your second
page where you then type the code to perform whatever action you want to occur under the condition of your
event.
Setting up your project:
When you launch Windows Forms
Application it will bring up a form which should be labeled form 1 and a
program window. At this point you should know what you are planning to do
in the program so for this example we are going to say you want to set up a
button click.
1) Drag a button from the selection menu on the left into the form
2)
3) Click on the button and in the properties window on the left you will
see a lightning bolt as such
4) Find the click option in that menu and double click it(This is will
create a method for when the button is clicked)
5) Go to Form1.cs and locate the method labeled button1_Click(object
sender, EventArgs e)
6) In this method you can do something like shown above. This will create a message box that allows the
user to read what you have written and then click “Ok” to continue
Labels
1.) Drag a label from the selections menu on the left into the form
2.) Click on it so it appears in the properties menu to the right
3.) Scroll down to where it says “Text”. You can change this property to change
what the label says.
4.) You can also make the label change text through other means, such as on
button clicks
5.) If you place the code below under you button1_Click(object sender,
EventArgs e) method, it will change the label to say “This is my first label.”
Label1.Text = “This is my first label”;
TextBoxes
1.) Drag a text box from the selections menu on the left into the form
2.) This will allow users to enter input into the text box when they run the
program. (Note: All input in the text box will be in string format, so to do
any calculations with numbers a conversion is necessary)
3.) If you would like to get the input that the user inputted you would use
code below
textBox1.Text
4.) Likewise, if you’d like to manually edit the text that appears in the text box, say after a button is
clicked, you would place the code below under the button click method.
textBox1.Text = “Enter your text here”;
These are some of the few simple objects that can be used in windows forms. Each of these objects has many
different properties and events associated with them. This lesson was simply done to show you the basic use of
each object.
Program 1: Creating a button (5 pts) (slightly modified Program 3 on pg. 477)
-Create a button that on click displays a message box with a phrase of your choice. Use ForeColor and Back
Color to change the color of the button. Set the Form object properties of ForeColor, BackColor, Size,
Location, Text, and AcceptButton. Also use the Close method to end the program after reading the message.
Program 2: Switching between Forms (5pts)
-Create two different forms each with three buttons. Have a button that allows the user to switch between
two. A second button on each form with a Message box for each saying “Form 1” and “Form 2”
respectively. The final button on each label “End” that will close the application.
Program 3: Using Text Boxes & Buttons (5pts) (Program 4 on pg. 477)
-Create a Windows application that contains two textbox objects and two button objects. The text boxes
should be used to allow the user to input two numeric values. The buttons should be labeled Add and
Multiply. Create event-handler methods that retrieve the values, perform the calculations, and display the
result of the calculations on a Label object. The label object should initially be set to be invisible. Additional
Label objects will do needed for the TextBox object captions.
Program 4: Using Text Boxes & Labels (5pts) (Program 6 on pg. 477)
-Create a Windows application that contains TextBox objects for first name, last name, and title, plus one
Button object. The text boxes should be used to allow the users to input their names. When the Button
object is clicked, the usernames should be displayed in a Label object formatted with one space between the
title, first, and last names.
Program 5: Guessing Game (10pts) (Program 10 on pg. 478)
-
Create the higher/lower guessing game using a graphical user interface. Allow users to keep guessing until
they guess the number. Choose two colors for your game: one should be used to indicate the value the
users guessed is higher than the target; the other is used to indicate that the value the users guessed is lower
than the target. With each new guess, change the form color based on whether the guess is higher than the
target or lower. Keep a count of the number of guesses. When they hit the target, display a MessageBox
indicating the number of guesses it took. Several approaches can be used to seed the target: one is to
generate a random number by constructing an object of the Random class. For example, the following
stores a random whole number between 0 and 100 in target:
Random r = new Random( );
int target = r.Next(0,100);