Lecture 22

advertisement
Lecture 22




Reminder: No class on Friday. Have a fun
spring break!
Log into Windows/ACENET. Use a web
browser to download today's exercise program,
SimpleRPNCalculator.zip.
Browse to SimpleRPNCalculator.zip and extract
the project folder. Double-click into the project
folders to the solution file, then double-click it
to start up VS.
Questions?
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
1
Outline

In-class exercise: Simple RPN Calculator

Postfix notation

Events

Event handlers


Sharing event handlers

Casting sender objects

Renaming methods
GUI notes
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
2
Postfix Notation


Today's exercise is to write
the code for a very simple
RPN calculator. The project
already has a GUI defined.
Note the Names of the GUI
objects.
RPN stands for "Reverse Polish Notation" and
is a postfix notation for arithmetic
expressions. This notation was used by a
popular line of calculators made by HewlettPackard in the 1980's.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
3
Postfix Notation



In RPN, expressions are written with operators
after the operands rather than the usual
notation with operators between the operands,
which is called infix notation. There is also
prefix notation with the operators before the
operands (also called "Polish Notation").
For example, instead of writing "12.4 + 2.3", in
RPN we would write "12.4 2.3 +".
One advantage of RPN is that parentheses are
not needed in expressions.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
4
Postfix Notation


When RPN is used in a calculator, the first
operand is "entered" (using the Enter button),
after a second operand is input, an operation is
performed with the entered first operand and
the current input as the second operand.
The previous example would be entered into
the RPN calculator using:
12.4 Enter 2.3 +

The result is displayed and becomes both the
current entered operand and the current input
operand.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
5
Events


Console programs can execute only one code
statement at a time, so to get user input, the
programmer is required to prompt and read in user
data, then do a computation, and finally display a
result. At the end of the code, the program exits,
unless there is a loop to do otherwise.
In contrast, GUI programs have form elements that are
always waiting for data, allowing a user to enter data
at any time. Computation happens only when an
event occurs. Afterwards, the program goes back to
waiting for input. In other words, there is an
underlying event loop in all GUI programs.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
6
Events

Input devices cause events that the GUI then
handles. Some examples:



Mouse events include: Click, DoubleClick,
MouseDown, MouseMove, MouseUp, Rollover
Keyboard events include: KeyPress
In addition, there are software events
generated by the program. E.g., a timer event
can use to synchronize various independent
parts of a program.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
7
Event Handlers


Event handlers are methods that are called
when an event has occurred.
In the C# GUI designer, one way to attach a
handler to a GUI object is to double-click on it.
As we saw last class, this causes the GUI
designer to 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 Button.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
8
In-class Exercise, Part 1


Double-click on the "0" button. This should
take you to the code view with a stub for
method btn0_Click.
When a digit button is clicked, we just want to
append that digit to the Text property of the
input textbox. For the "0" button, this can be
done as follows:
txtInput.Text = txtInput.Text + "0";

After typing this into the handler, run the
program and test out the "0" button.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
9
Sharing Handler Code


We could do the same for each of the other
digit buttons. However, each handler would be
exactly the same except for the digit being
appended.
Instead, we can reuse btn0_Click for all the
digit buttons. The method heading for Click
handlers have a parameter called sender. This
parameter is the object whose event is being
handled. As such we can access the sender's
properties.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
10
Sharing Handler Code


In particular, since the sender is a Button, we
can access the Text property of the sender and
that will tell us what digit to append.
However, in order to do so, we have to tell the
compiler that the sender is a Button object.
This is done by casting the sender object into a
Button object and assigning the result to a
Button variable:
Button btn = (Button) sender;
Button variable declaration
Wednesday, March 2
Cast sender into a Button
CS 205 Programming for the Sciences - Lecture 22
11
Sharing Handler Code

So the complete btn0_Click method is
private void btn0_Click (object sender, EventArgs e)
{
Button btn = (Button) sender;
string digit = btn.Text;
txtInput.Text = txtInput.Text + digit;
}

Run the program to see that it works. Now that
we have a handler that works with any digit
Button, we can attach it to the other digit
Buttons.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
12
In-class Exercise, Part 2


Event list tab
Go to the Designer View,
click on the "1" Button. In
the Properties window,
click on the "Lightning
Bolt" tab. This gives the
Event list for the Button.
Find the Click event, click
on the arrow to the right,
and select btn0_Click as
shown on the right.
Wednesday, March 2
Attach btn0_Click as
handler for this button's
Click event
CS 205 Programming for the Sciences - Lecture 22
13
In-class Exercise, Part 2



Run the program again and check that the "1"
button now works.
Attach btn0_Click to the rest of the digit Buttons
("2" through "9") and test the program again.
Since this handler handles more than btn0's
Click event, perhaps we want to rename it.
This needs to be done everywhere this method
is used. Luckily, MS VS has an action that can
do this.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
14
Renaming a Method

Renaming a method is done as follows:

Right-click on the name to be changed.

Select Refactor, then Rename

Type in a new name and click OK


A list of all the places that will be changed is given.
Usually it is safe to click Apply for name changes.
This can be done with variable and class
names also.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
15
Renaming a Method
Right-click
on name to
change
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
16
Renaming a Method
Enter a new name
List of places that will change
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
17
In-class Exercise, Part 3

The rest of the buttons need individual
handlers, since they all do different things. We
will work throught the rest of them.

"Enter" button

Operator buttons ("+", "-", "*", "/")

"+/-" button

Dot button (".")

Clear and Clear Error buttons ("C" and "CE")

Backspace button ("<-")
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
18
"Enter" Button


Double-click on the "Enter" button.
This button saves the first operand as a number
to be used by an operator. This operand must
be accessed by the operator Button handlers.



Declare a double class variable operand1
initialized to 0
In btnEnter_Click( ), write code to parse
txtInput.Text as a double and assign it to operand1
This isn't quite enough, when we start entering
the second operand, the input textbox should
be cleared.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
19
"Enter" Button


We could just clear the textbox after getting the
operand, but usually whatever is entered is
displayed until something new happens.
We can get this behavior by doing the following:



Declare a bool class variable newInput initialized
to true.
In btnEnter_Click( ), set newInput to true.
In btnDigit_Click( ), check if newInput is true, and if
it is, clear the input textbox and set newInput to
false. This is done before appending the digit.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
20
Operator Buttons


Double-click on the "+" button.
The result is to be stored as the first operand of
the next operation. Method btnAdd_Click( )
should do the following:




Declare a local double variables operand2.
Parse txtInput.Text as a double and assign it to
operand2.
Sum operand1 and operand2 together, storing the
result back into operand1
Convert operand1 (the result) into a string (using
ToString( )) and assign it to txtInput.Text
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
21
Operator Buttons

As with the "Enter" button, when the next
operand is to be entered, the input textbox
needs to be clear, so we also need:


Set newInput to true.
Write the code for the other operator buttons.
The only difference for these handlers is the
operation being performed.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
22
"+/-" Button


Double-click on the "+/-" button
This button simply negates the current input.
However, since it is a string, we need to
compute the current value. This is done by:




Declare a local double variable value
Parse txtInput.Text as a double and assign it to
value.
Negate value (i.e., value = ­value;)
Convert value into a string (using ToString( )) and
assign it to txtInput.Text
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
23
Dot Button


Double-click on the Dot button
This button appends a dot to the end of the
input textbox like the digit buttons do, except
that only one dot may appear in the input. This
can be ensured by using the Contains method
for strings like so:
if (!txtInput.Contains('.')) // no dot
{
// do like the digit button handler
}
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
24
Clear and Clear Error Buttons




Double-click on the Clear button
The Clear button is to reset the calculator to its
starting state: operand1 is 0, newInput is true,
input textbox is blank.
Double-click on the Clear Error button
The Clear Error button is to reset just the input
textbox to be blank.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
25
Backspace Button


Double-click on the Backspace button
This button is to delete the last digit entered in
the input textbox. This can be done using the
Remove method on strings. This method has a
parameter that is the index of the character to
be removed. For the last character in the
textbox that would be txtInput.Text.Length - 1.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
26
Backspace Button

Remove is used like so
txtInput.Text.Remove (txtInput.Text.Length­1);


There is one problem with this. It causes an
error if the string is empty (Length is 0), so we
need to check for this before doing the
Remove.
At this point, we have a "working" calculator
that handles (only) two operands at a time.
After spring break we'll clean up this
application.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
27
GUI Notes

For the curious, here are some notes about
how the exercise GUI was created.


The buttons were made exactly the same size by
explicitly setting the Size property (length, width) of
each button. They were sized to fill a form that is
wide enough for the form title bar text to be seen in
its entirety.
The textbox TextAlign property was set to Right
(default is Left). The font size of the textbox is
slightly larger than that of the buttons.
Wednesday, March 2
CS 205 Programming for the Sciences - Lecture 22
28
Download