473,670 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

9 New Member
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 7336
kenobewan
4,871 Recognized Expert Specialist
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
AhmedGY
9 New Member
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 Recognized Expert Moderator Expert
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
AhmedGY
9 New Member
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 New Member
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
AhmedGY
9 New Member
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
2479
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 where my last two columns I am displaying are ID fields, one for clientid and the other for empid. What I would really like is to display their actual names instead of the ids.
4
6495
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 know that I can check for keypress on a given field, but I don't want to do this for every field on the form. Is there a way to determine what I want to know?
2
3563
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 id="TxtPassword" tabIndex="2" runat="server" Width="272px" ToolTip="Enter your domain password" TextMode="Password"></asp:textbox> <asp:linkbutton id="BtnLogin" tabIndex="3" runat="server">Login</asp:linkbutton> </form> This is the form as i...
2
13493
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 'clicked' when Enter is pressed in the text box. I can't set the form's AcceptButton property to the toolStripButton - it doesn't allow toolStripButtons to be set as the AcceptButton. As a last resort, I could create a keypress event handler for...
19
107946
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 sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
4
1628
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 'hello', i modify it into 'helicopter'. when i process the save, the word will become 'hello' again (but i'm still in record 2). and i know it saved because when i do... bindingsource.movenext bindingsource.moveprevious my textbox displays...
2
2408
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 same JTextFeild, like : 1-press any number, called it 1. 2-press the (+) button to add previous number. 3-press anthor number, called it 5. 4-again press the (+) button to add two numbers(1+5)and show the answer(6) . 5-again press anthor number...
12
4828
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. The Easter Egg I want to create would send a dump of the data for a particular part number to the screen when a certain secret combination of characters is pressed. If it works out well, I can actually impliment it on our production floor.
3
2093
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. Is there any way to do it all on the client side, without posting back to the server? I would like to update the running total each time the user inputs an amount in a textbox and then goes to the next textbox. Thanks, -- Paul G Software...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.