473,387 Members | 1,693 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,387 software developers and data experts.

Simple Keypad

Once again, I'm still very new but learning a ton through online tutorials using c sharp in visual studio 2008. I'm trying to build a calculator but I don't need help with the math as you might think. I'm looking forward to the challenge of figuring that out myself.

What I need help with will probably seem easy to you guys. I have a text box being used as a display and a keypad with all number 0-9 on them. I want to make the keys interactive with the display. If I hit "2" then "0" then "6" I want the display to show "206".

I'm sure it's totally easy but for some reason it's escaping me. How do I do this? Is there some sample code that someone can show me to learn from? :)

Thanks!
Craig
Feb 5 '10 #1
16 8199
tlhintoq
3,525 Expert 2GB
A textbox has a property of .Text
It contains (as a string) the text that is in the textbox.
You can get and set this text.

Expand|Select|Wrap|Line Numbers
  1. String RightNow = myTextBox.Text;
  2. myTextBox.Text = "5";
strings are additive. You can
s
Expand|Select|Wrap|Line Numbers
  1. tring One = "1";
  2. string Two = "2"
  3. string Both = One + Two; // Both now equals "12"
So for your calculator display you could do something as simple as getting the value currently displayed, and tack onto the end of it the key that was just pressed.

Expand|Select|Wrap|Line Numbers
  1. myDisplayTextbox.Text = myDisplayTextbox.Text + "5";
Code to the right side of an = sign is evaluated then stored in the object on the left side. So the compiler will first get the value of the text, concatenate "5" to the end of it, then store it in... the display textbox.

There are lots of good explanations and code samples on the MSDN website.
http://msdn.microsoft.com/en-us/libr...ox(VS.71).aspx
The easiest way to find them (to me) is to google "MSDN" plus what you are looking for... "MSDN Textbox" .... "MSDN button.click" .... and so on
Feb 5 '10 #2
You rock! I don't know how I missed that. I tried EVERYTHING except:

myDisplayTextbox.Text = myDisplayTextbox.Text + "5";

-Craig

PS - This site is amazing!
Feb 5 '10 #3
PS - If there is a quick and easy way of programming the keys to work on multiple text boxes then I'd love to see it. A Point of Sale system would be a good example where you would use ONE keypad to enter numbers in multiple text boxes. I have a feeling it's some setting that can tell what text box is currently active with the program running and then enters text there.

I haven't done any research on it yet so no worries if not. :)

Thanks again,
Craig
Feb 5 '10 #4
Curtis Rutland
3,256 Expert 2GB
It has nothing to do with which one is "active." ("Has focus" would be a more accurate term, btw).

You put it in the second or third or fourth or nth the same way you put it in the first:
myDisplayTextbox.Text = myDisplayTextbox.Text + "5";
myDisplayTextbox2.Text = myDisplayTextbox.Text + "5";
Feb 5 '10 #5
hmmm....do i need to lump all the text boxes in a variable and add some sort of "has focus" property to them? I've never done something like that so i don't even know if that's possible. Just thinking out loud.

IE:

displayBoxes.Text = displayBoxes.something-something-that-has-focus.Text + "5"; //????
Feb 5 '10 #6
Curtis Rutland
3,256 Expert 2GB
I'm not sure I understand your question, and I think I may have confused you more.

If your question is how to you append the same string to two different text boxes, then you can do it like this:
Expand|Select|Wrap|Line Numbers
  1. string textToAppend = "5";
  2. textBox1.Text = textBox1.Text + textToAppend;
  3. textBox2.Text = textBox2.Text + textToAppend;
I think I need some clarification from you...when you say "keys" and "keypad" do you mean physical keys on the keyboard? Or Buttons on the form? There's ways to handle both, but I need to know which you mean.
Feb 5 '10 #7
tlhintoq
3,525 Expert 2GB
I think I would go a different route.
Try to use a property to hold the current displayed value.
Clicking one of the 10key buttons makes a change... the set method then updates all the boxes.

Expand|Select|Wrap|Line Numbers
  1. namespace POScalc
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         string DisplayText
  11.         {
  12.             get
  13.             {
  14.                 return textBox1.Text;
  15.             }
  16.             set
  17.             {
  18.                 textBox2.Text = textBox1.Text = value;
  19.             }
  20.         }
  21.  
  22.         private void btn1_Click(object sender, EventArgs e)
  23.         {
  24.             DisplayText += "1";
  25.         }
  26.  
  27.         private void btn2_Click(object sender, EventArgs e)
  28.         {
  29.             DisplayText += "2";
  30.         }
  31.     }
  32. }
Now you have a single central variable (DisplayText) that automagically changes all the textboxes it is meant to have control over.

If you recalculate sales tax for example, then changing the one string changes all of its boxes, and so on.
Feb 5 '10 #8
The keypad i'm refering to are buttons that I created on the form. :)
Feb 5 '10 #9
Curtis Rutland
3,256 Expert 2GB
Yeah, tlhintoq, that's the direction I would have worked towards, but I wasn't sure where we were starting at.

Actually, I'd simplify it a bit further. All of the number button's OnClick event's would be bound to a single event handler, then I would use (sender as Button).Text to get which button was pressed.
Feb 5 '10 #10
I think that I'm getting ahead of where my skill level is. I can't help it, I love c sharp!
Feb 5 '10 #11
Curtis Rutland
3,256 Expert 2GB
That's part of the problem here. I would suggest you find a good book or online tutorial dealing with Windows Forms applications, and all of these questions will disappear. Work through it and learn each concept on the way, and don't try to get too far ahead of yourself.
Feb 5 '10 #12
tlhintoq
3,525 Expert 2GB
Craig you have inspired me.
I'm going to use this idea (a basic cash register) to build an "insights" article. How to build a little program from the ground up, making use of properties, form to form talking, custom events etc. I'll let you know when it is posted
Feb 5 '10 #13
I would absolutely love to see something like that!!! I make a lot of tutorials for beginners using the Adobe CS4 Suite and have got a lot of good feedback. I've been teaching a few of the classes that I've been a student in and the professor asked me to make up a bunch of these tutorials. You can see an example at www.craigfisherlive.com/tutorials.html. The site is still being built so there isn't much up there and probably a few broken links.

Are you going to do it through VS2008? Reason I ask is, a lot of the tutorials put out there (even the beginning ones) just dump a ton of code onto the screen and say do this. As a beginner, we don't really understand all the extra code that we are seeing or how to "plug" everything in. It's escpecially difficult to figure out since VS2008 does a lot of the code work for you, IE: the designer. That being said, I still try to figure it out.

Basically what I'm saying is...if you had a step-by-step tutorial geared towards my level of c sharp, it would inspire a lot of beginning programers like myself and give them "ideas" of how they could construct/modify the program to their specific needs.

-Craig
Feb 5 '10 #14
tlhintoq
3,525 Expert 2GB
Here is the start on it
http://bytes.com/forums/writing-room...er#post3544337

Yes it is going to be based on VS08 and filled with screenshots.
Feb 5 '10 #15
tlhintoq
3,525 Expert 2GB
Ok. Part 1 of this article is posted. I realized it is growing far beyond my original plan - which is good - so it will have to be a series.
Part 1
Feb 7 '10 #16
gopishk
15
Hi,
I have a text box and it can contain only numbers. Programatically can we open only the numeric keypad instead of both text/numeric keypad.
By this the user cannot enter text at anytime. Though validation for symbols can be made later.
Can you provide a code snippet for opening only a numeric keypad to enter numbers only for the text box.


Thanks,
Gopish
Apr 5 '10 #17

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

Similar topics

1
by: RoDzZzZ | last post by:
anyone known how i can get this virtual keypad? access: https://www.secureweb.com.br not is possible copy the keypad virtual full? anyone have one to send for me?
0
by: Jussi | last post by:
I have a Windows Application project which is registered for the COM Interop and Output type is Class Library. My class is as follows: namespace WSure { //COM events interface public...
0
by: Sushant Bhatia | last post by:
Hi. I am trying to develop an application that would be able to get keypad key press events even when the focus is not on the application. For instance, if the application is minimized to the...
2
by: Melson | last post by:
Hi Does anyone know how to configure the numeric keypad on the keyboard. For example, by pressing button 7, it displays a character. I must also be able to display number 7 whenever I pressed...
3
by: Melson | last post by:
Hi May I know is there a way to change the Numeric keypad into mobile phone keypad? Regards Melson
10
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something...
0
by: Claire | last post by:
Ive a modal form that offers message editing for an internal mail system. Its part of a touch screen application. I need to display a keypad form when necessary (some users have no keyboard). My...
0
saranjegan
by: saranjegan | last post by:
dudes,i need to access a PC game from a mobile keypad instead of playing via our PC keyboard,the mobile caller may call a particular number its handled by PBX via PBX i can get the input of data...
4
by: Dennieku | last post by:
Hi, I have to develop an on-screen keyboard and on-screen numeric keypad for a touchscreen UI. The hardest thing with this is that it has to be multi-lingual. Has anybody have ideas how to...
0
by: Marcelo Muzilli | last post by:
I have an application developed in C# (compact framework) and I would like to activate or de-activate the keypad in runtime. Do you know if Windows Mobile has an API for this? If there is, how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.