Problem Set 7
Up Problem Set 0 Problem Set 1 Problem Set 2 Problem Set 3 Problem Set 4 Problem Set 5 Problem Set 6 Problem Set 7 Problem Set 8 Problem Set 9 Problem Set 10

 

Graphical User Interfaces

Due 11:59pm on Wednesday, November 8

 

  1. Do Program Design Exercise 20 in Chapter 9 of the text.  When the button at the top is pressed, the text on the right is copied and then displayed in the center.  This program doesn't do much, but it gets you familiar with several of the different control (i.e. widget) types.
     
    Solution.
     
  2. Write a C# window application that implements a very simple calculator, based on the following image.  The width and height of the window form should be 225 and 320, respectively.  The text for the top-level form is "Calculator".  There is a label component (size 175, 35) at the top (with white background color, text aligned to the right, size 16 regular font). There are 15 buttons (each size 48x23, size 14 bold font): ten of these are for digits; three are for the addition, subtraction, and multiplication buttons; the "C" button clears the text in the label to zero; and the "=" button prints out the result.  There are many details left for you to decide.  The general rule is that the calculator should behave like a standard calculator, except that it only does integer arithmetic.
     

                                                             [calculator]
 

You should develop your code in three stages:

  1. First, just get all of the buttons and the text window to appear properly on the form.
  2. Next, get the numeric keys to work properly -- that is, as they are pressed, the proper number should appear in the text window, gradually increasing in length as more numbers are pressed.  Also, get the "Clear" button to work.
  3. Extra credit:  Finally, get the "compute" buttons (+, -, *, and =) to work properly.  This will require first clearing the text area when +, -, or * is pressed (but somehow remembering the number), then accepting a second number, and finally displaying the result when the = button is pressed.

Hints: Have all 10 digit-buttons share a single event handler; likewise, the three arithmetic-operator buttons should share an event handler as well.  Also, it might be useful to have a method like the following to convert a sender object into an integer:

  private int SenderToInt(Object sender)
  {
     if (sender == button0) return (0);
     else if (sender == button1) return (1);
     else if (sender == button2) return (2);
     else if (sender == button3) return (3);
     else ...
  }
 
Solution.