473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Feb 10 '08 #1
6 7327
kenobewan
4,871 Expert 4TB
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.
Feb 11 '08 #2
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.
Feb 11 '08 #3
Frinavale
9,735 Expert Mod 8TB
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
Feb 11 '08 #4
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.
Feb 11 '08 #5
wimpos
19
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

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             switch (e.KeyChar)
  4.             {
  5.                 case '0':
  6.                     break;
  7.                // all the other numbers
  8.                 case '*':
  9.                     break;
  10.               // all the other operators
  11.             }
  12.         }
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
Feb 12 '08 #6
ok cool it works, i put it that way:

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if (label1.Text.Length <= 15)
  4.             {
  5.                 switch (e.KeyChar)
  6.                 {
  7.                     case '1':
  8.                         label1.Text += e.KeyChar.ToString();
  9.                         break;
  10.                     //... etc
  11.                 }
  12.             }
  13.             else
  14.             {
  15.                 System.Media.SystemSounds.Beep.Play();
  16.             }
  17.         }
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.
Feb 12 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Joe Van Meer | last post by:
Hi, Is the datalist meant for one table or can I use it to display information from 2 different tables? Or should I be looking at a datagrid instead? I began my code and kinda ran into a snag...
4
by: Woody Splawn | last post by:
I would like to be able to know if the user has pressed a key in any text box on a winform or in other words, if he is editing a record. I would like to know this as soon as he presses any key. I...
2
by: RTT | last post by:
<form id="Form1" method="post" runat="server"> <asp:textbox id="TxtEmail" tabIndex="1" runat="server" Width="272px" ToolTip="Enter your emailaddress for authentication"></asp:textbox> <asp:textbox...
2
by: matthewr | last post by:
In Internet Explorer, for example, when you hit return in the address bar, the Go button is pressed. In my program, I have a toolstrip with a textbox and button. How do I ensure the button is...
19
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make...
4
nev
by: nev | last post by:
i have a bound textbox. i am in record 2 and textbox displays 'hello'. now i added code to automatically save a new word entered in textbox. for example, while in record 2 which textbox displays...
2
by: all eyes | last post by:
hi all, i am using here JTextField to get sum of all previously pressed numbers in one button called (+) and so on. there is only one button take care of addition and displaying the result in the...
12
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I want to plant an Easter Egg in our software. We have a TextBox that is multiline and used to display all sorts of messages on the screen for our operators based on database queries and such. ...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a webform with several entry boxes and the user enters numbers in each box. I keep a running total (just adds all of the entries together) but am posting back to the server to do this. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.