ENGR/CS 101 CS Session Lecture 4 - University of...

24
Lecture 4 ENGR/CS 101 Computer Science Session 1 ENGR/CS 101 CS Session Lecture 4 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Finish exercise from last time

Transcript of ENGR/CS 101 CS Session Lecture 4 - University of...

Page 1: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 1

ENGR/CS 101 CS SessionLecture 4

Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Finish exercise from last time

Page 2: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 2

Outline

Problem: Use a GUI to enter user input Microsoft Visual Studio GUI designer

Forms, textboxes, buttons, labels

C# programming language Properties Events and handlers

Page 3: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 3

Problem Specification

Today's program will be a GUI application that has the same functionality as the console program from last time. Allow the user to enter a shift key letter in

uppercase Allow the user to enter a plaintext letter in

uppercase to be enciphered Have a button for the user to click to have the

plaintext letter enciphered Display the ciphertext letter

Page 4: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 4

Interface Mockup

Textboxes to enter plaintext and shift key, and to display ciphertext

Button to encipher plaintext.

Labels to identify the textboxes

Page 5: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 5

Creating a GUI Project

Create a new project. Make sure the language template is Visual C#.

Select "Windows Form Application". Give the project a name like "cs101gui".

Click on OK. You will get the GUI designer with a blank form.

Page 6: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 6

Creating a GUI Project

Page 7: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 7

GUI Designer

Page 8: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 8

GUI Designer

Every GUI application has a base form that contains all other GUI elements. Change size by pulling on the handles.

GUI elements are available in the Toolbox. Click on form, then roll mouse over the Toolbox tab, click on Common Controls.

Select and place GUI elements E.g., Textbox, Button, Label

Page 9: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 9

GUI Designer

Toolbox Tab

GUI element

Page 10: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 10

GUI Designer

Page 11: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 11

Properties

Each GUI element has properties that control its appearance. E.g., Text, Font, Size

Values for the selected element are shown in Properties Window (View -> Properties Window), usually in the lower right corner

You can change the initial values in the Properties Window. Can also change them in program code (i.e., while program is running).

Page 12: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 12

Properties

Properties Window

Page 13: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 13

Properties

Each GUI element is an object with a Name property that is its variable name in the code. Always change the Name of manipulated elements so they have meaningful variables names. E.g. plaintextBox rather than textbox1.

Almost every GUI element has a Text property. E.g., the main form's Text property is its titlebar. We can change it to give the application a title.

Page 14: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 14

Properties

Properties are accessed in programs using a dot (.):

<element name>. <property name>

For example, textboxes have a Text property that is a string containing the contents of the box. To access this string in the plaintext box use: plaintextBox.Text

Page 15: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 15

In-class Exercise, Part 1

Create a simple form with three Textboxes for the plaintext, ciphertext, and shift key, and a Button.

After changing the properties as described on the next slide, label and arrange these elements however you like on the form.

Page 16: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 16

In-class Exercise, Part 1

Change the following properties: For the Textboxes, change their Name property to

"plaintextBox", "ciphertextBox", and "keyBox" (without the quotes), respectively. For plaintextBox and ciphertextBox, change Multiline property to True. After this, pull on the handles to make them as large as you like.

For the Button, change its Name to "encipher" and its Text to "Encipher".

Change Font properties to the font and size of your choice.

Page 17: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 17

Events and Handlers

Unlike console programs, which require the programmer to prompt and read in user data, GUI programs have form elements that are always waiting for data, but computation only happens when an event occurs.

Input devices cause events that the GUI then handles. For example: Mouse events include: Click, DoubleClick,

MouseDown, MouseMove, MouseUp, Rollover Keyboard events include: KeyPress

Page 18: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 18

Events and Handlers

Double-clicking on a form element in the GUI designer brings up the code view of the form. As with console programs, MSVS has created a skeleton program.

It also creates a handler function stub for the most common event for the element type and attaches it to the element. E.g., Click event for our Encipher button.

When a user clicks the Encipher button, this handler function is run to respond to the event. We'll have it encipher a plaintext letter.

Page 19: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 19

Events and Handlers

Handler code goes here!

Page 20: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 20

Events and Handlers

A handler function can access any of the GUI elements and their properties.

We will get the user data from the keyBox and plaintextBox using the Text property like so: keyBox.Text plaintextBox.Text

Just as with the user input from the console, we need to convert a string into a char using char.Parse( )

Page 21: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 21

Handler Design

The design of the handler code for the Encipher button basically is the same as the console program. However, the details of how the user input is received and the result displayed are different since we are using Textboxes. (New or modified steps in bold.)

1. Clear the ciphertext box2. Get the shift key from keyBox.Text3. Get the plaintext letter from plaintextBox.Text4. Compute the ciphertext letter5. Append the cipher letter to the ciphertext box.

Page 22: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 22

In-class Exercise, Part 2

For our program, we want to handle a mouse click on the Encipher button. Double-click on the button to get to the handler function stub, if you haven't done so already.

Add the handler function code shown on the next two slides. It basically is the same as the console program code, except the user interaction uses the textboxes rather than the console.

Page 23: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 23

Encipher Button Handler Code

// This code goes in the place indicated on Slide 19// The modified parts from the console program are bold

// Variable declarations ­ same as console programchar shiftKey,     // key letter      plainLetter,  // user input     cipherLetter; // resultint shiftNumber,   // # of shift places    index;         // of cipher letter

// Clear the result boxciphertextBox.Text = "";

// Get the key letter and a letter to encipher from usershiftKey = char.Parse(keyBox.Text);plainLetter = char.Parse(plaintextBox.Text);

Page 24: ENGR/CS 101 CS Session Lecture 4 - University of Evansvilleuenics.evansville.edu/~hwang/f11-courses/engrcs101/lecture04-slide… · ENGR/CS 101 CS Session Lecture 4 ... Finish exercise

Lecture 4 ENGR/CS 101 Computer Science Session 24

Encipher Button Handler Code

// Compute the corresponding ciphertext letter// Same as the console programshiftNumber = shiftKey ­ 'A';index = (plainLetter ­ 'A' + shiftNumber) % 26;cipherLetter = (char)((int)'A' + index);

// Display the results// Append the enciphered letter to ciphertext box// Need to convert it to a string firstciphertextBox.AppendText(cipherLetter.ToString());