C# WinApp, How to display number pressed on a textbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AhmedGY
    New Member
    • Jan 2008
    • 9

    C# WinApp, How to display number pressed on a textbox?

    Hi, I'm new to C#, and doing a simple calculator application, it's completed now and calculates well, i wanna provide the interface with hotkeys so when the user press a number lets say "1" on the keyboard gets displayed on the textbox, and activate operators when he press em, just like windows xp calculator.

    am using textBox1.Text += ((Button)sender ).Text; to display numbers when clicked by the mouse btw.

    How to do it?, i've searched and found i should use keypress event, however i couldn't get it applied.

    Thanks,

    AhmedGY
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Originally posted by AhmedGY
    Hi, I'm new to C#, and doing a simple calculator application, it's completed now and calculates well, i wanna provide the interface with hotkeys so when the user press a number lets say "1" on the keyboard gets displayed on the textbox, and activate operators when he press em, just like windows xp calculator.

    am using textBox1.Text += ((Button)sender ).Text; to display numbers when clicked by the mouse btw.

    How to do it?, i've searched and found i should use keypress event, however i couldn't get it applied.

    Thanks,

    AhmedGY
    How about an imagemap or buttons for the numbers? HTH.

    Comment

    • AhmedGY
      New Member
      • Jan 2008
      • 9

      #3
      Originally posted by kenobewan
      How about an imagemap or buttons for the numbers? HTH.
      Sorry but i don't understand, i just wanna know how to make the keyboard do the math just like pressing buttons with mouse clicks, see just like windows accessories calculator, you don't have to click on every button, u can simply use the keyboard.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by AhmedGY
        Sorry but i don't understand, i just wanna know how to make the keyboard do the math just like pressing buttons with mouse clicks, see just like windows accessories calculator, you don't have to click on every button, u can simply use the keyboard.
        This is done using the KeyPress Event....seeing as you've tried this, could you please post the code that you have that handles this event so that we can figure out what you're doing wrong.


        -Frinny

        Comment

        • AhmedGY
          New Member
          • Jan 2008
          • 9

          #5
          Originally posted by Frinavale
          This is done using the KeyPress Event....seeing as you've tried this, could you please post the code that you have that handles this event so that we can figure out what you're doing wrong.
          -Frinny
          I tried to understand the syntax but i couldn't, i don't know what to write inside the keypress event method.

          Comment

          • wimpos
            New Member
            • Jan 2008
            • 19

            #6
            Originally posted by AhmedGY
            I tried to understand the syntax but i couldn't, i don't know what to write inside the keypress event method.
            You will see that in the KeyDown/KeyPress/KeyUp event method
            there are 2 args: Object Sender and KeyEventArgs e

            It is the e that contains information about the key that is pressed.
            namely: KeyCode, KeyData, KeyValue
            KeyCode contains an enumeration of Keys.xxx
            KeyValue the Ascii Code

            i propose you use something like this: KeyPress Event

            Code:
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            		{
            			switch (e.KeyChar)
            			{
            				case '0':
            					break;
            			   // all the other numbers
            				case '*':
            					break;
            			  // all the other operators
            			}
            		}
            There is one thing you should be aware of. When you attach the keypress event to your Form (as I did in the example code) no button may have the focus. It is your form that should have the focus!

            This post is an answer to your question, but why do you want to use KeyPress events when you have a textbox in you calculator?

            Regards

            Comment

            • AhmedGY
              New Member
              • Jan 2008
              • 9

              #7
              ok cool it works, i put it that way:

              Code:
              private void Form1_KeyPress(object sender, KeyPressEventArgs e)
                      {
                          if (label1.Text.Length <= 15)
                          {
                              switch (e.KeyChar)
                              {
                                  case '1':
                                      label1.Text += e.KeyChar.ToString();
                                      break;
                                  //... etc
                              }
                          }
                          else
                          {
                              System.Media.SystemSounds.Beep.Play();
                          }
                      }
              and it works, but only once, i've to restrat the app to get it work again, i mean if i pressed any key it will display the corresponding number on the label, if i pressed the clear button which assign the label to "", and pressed any key again, it doesn't display anything, i thought i should have my form focused like u said, but i don't know how, i try to put Form1.Focus();, it just give me an error:

              Error 1 An object reference is required for the non-static field, method, or property 'System.Windows .Forms.Control. Focus()


              Although it doesn't give an error when doing label1.Focus().


              This post is an answer to your question, but why do you want to use KeyPress events when you have a textbox in you calculator?
              Well thought of replacing the textbox with a label is a good idea, cause the caret thing that blinks ruin the interface.

              Comment

              Working...