Lecture 24 Log into Windows/ACENET. Start MS VS and open SimpleRPNCalculator project from last class. Questions? Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 1 Outline Review: Events and event handlers Complete last class exercises KeyPress handler Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 2 Review: 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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 3 Review: 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. 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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 4 In-class Exercise, Last Class The rest of the buttons need individual handlers, since they all do different things. We will work through the rest of them. "Enter" button Operator buttons ("+", "-", "*", "/") "+/-" button Dot button (".") Clear and Clear Error buttons ("C" and "CE") Backspace button ("<-") Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 5 "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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 6 "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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 7 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 Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 8 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 between these handlers is the operation being performed. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 9 "+/-" 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 Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 10 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 } Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 11 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 set to blank. Double-click on the Clear Error button The Clear Error button is to reset just the input textbox to be blank. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 12 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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 13 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. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 14 KeyPress Handler Calculator applications usually let the user enter input using the keyboard. This is the usual use for a textbox. However, for our calculator, we only want the digits and dot to be allowed in the input textbox. For other inputs like '*' or '+', we want the application to do something different. To do this we can write a KeyPress handler. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 15 KeyPress Handler To attach a KeyPress Handler to the input textbox, go to the design view and click on the input textbox. In the Properties window, click on the lightning bolt icon tab, if not already there. Scroll to the KeyPress entry, then double-click on it. This will attach a method named txtInput_KeyPress as the handler for the KeyPress event of the input textbox, and add a stub for for this method in the code view. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 16 KeyPress Handler click on input textbox lightning bolt icon tab double-click on KeyPress Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 17 KeyPress Handler The stub looks like: private void txtInput_KeyPress(object sender, KeyPressEventArgs e) { } As before, sender is the GUI element that is responding to the event. e is an object that contains various data. We are interested in the KeyChar property that says what key (e.g., '+') was pressed. This information is obtained using e.KeyChar Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 18 KeyPress Handler We can use a switch statement to test the value of e.KeyChar and do the appropriate action. For example, if it is the character '0' ... '9', we want to do the same thing as the digit button. We can combine the cases for these characters together as shown on the next slide. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 19 KeyPress Handler Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 20 Extracting a Method When a digit key is pressed, we want the application to do the same thing as when a digit button is clicked. Likewise for other valid keys. Instead of cutting and pasting the code from btnDigit_Click into the KeyPress handler, we can extract the code in btnDigit_Click that checks for new input and appends the digit into a separate method that also can be called by txtInput_KeyPress. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 21 Extracting a Method To do this extraction, do the following: Highlight the code to be made into a new method Go to the Refactor menu and select Extract Method The Extract Method dialog will appear asking for a new method name. Enter one, e.g. ProcessDigit, and click OK. The new method definition is added to the code and the selected code is replaced with a call to the new method. These steps are shown on the next slides. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 22 Extracting a Method 1. Select code to extract 2. Refactor -> Extract Method 3. Give new method a name, click OK Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 23 Extracting a Method call to extracted method extracted method Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 24 KeyPress Handler Now the KeyPress handler can call ProcessDigit to do the appropriate action when a digit key is pressed: case '9': ProcessDigit (e.KeyChar.ToString()); break; Note that the ProcessDigit parameter is a string, so we convert e.KeyChar to a string by calling the ToString( ) method. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 25 KeyPress Handler This doesn't quite work, yet. When we run the program, there are two problems: The input digit gets added to both the beginning and the end of the input textbox. The input cursor is left after the first character in the input textbox. The first problem is due to a default handler trying to handle another aspect of a keypress. This is eliminated by setting e.Handled to true: e.Handled = true; // no more processing needed Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 26 KeyPress Handler The second problem is fixed by setting the insertion point to after the last character in the input textbox. This is done by setting the SelectionStart property to the TextLength property: txtInput.SelectionStart = txtInput.TextLength; Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 27 In-Class Exercise Extract methods for processing dot, the operators, Enter, and backspace. Add cases to txtInput_KeyPress to handle '.', the operator characters ('+', '-', '*', '/'), Enter ('\r'), and backspace ('\b') that call the appropriate method. Monday, March 14 CS 205 Programming for the Sciences - Lecture 24 28