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

new programmer question

I am teaching myself C# and was trying to build a small calculator. I
am trying to validate the process of ensuring that Integer keys are
pressed. I thought that I was able to capture the appropriate place for
intercepting the key press event and parse the result to determine which
key was pressed but I am removing the key prior to it showing up in the
text box. I have attached the code so you can see.

Ernie

private void txtCalculate_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
try
{
Int32.Parse(e.KeyChar.ToString());
}
catch
{
MessageBox.Show("Please enter Valid Numbers");
txtCalculate.Text=txtCalculate.Text.Substring(1,
txtCalculate.Text.Length-1);
}

}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
2 1008
Ernest Griffin <cs****@gcg.net> wrote:
I am teaching myself C# and was trying to build a small calculator. I
am trying to validate the process of ensuring that Integer keys are
pressed. I thought that I was able to capture the appropriate place for
intercepting the key press event and parse the result to determine which
key was pressed but I am removing the key prior to it showing up in the
text box. I have attached the code so you can see.

Ernie


<snipped code>

I don't know if there would be a better way of doing this, but this
works:

private void textBox1_KeyPress(...)
{
char KeyChar = e.KeyChar;
e.Handled = KeyChar < '0' || KeyChar > '9';
}

Now, setting e.Handled to true causes the character pressed to not be
displayed in the text box. Setting it to false causes the character
to be displayed.
Nov 15 '05 #2


Thank you -

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Market Mutant | last post by:
I just wonder job selections, job openings and salary level of PHP programer or Perl programmer comparing to Java programmers. Is Java programmer's salary has a minimal of 60K in US? Are there...
14
by: Daniel Chartier | last post by:
Hello. I work in the paper industry and we recently had someone (the original author) from within the company make a program for preventive maintenance. However, it had some bugs and we wanted...
15
by: Randall Smith | last post by:
I've been programming in Python for about 2 years. I think it offers the best combination of simplicity and power of any language I have explored. As I write more and larger and complex programs,...
12
by: Computer Whizz | last post by:
Hiya guys, I saw this (C++ Programmer's Reference by Herbert Schildt) book in the library today, and wondered what the experienced programmer thought about it before I decided to dive in and get...
29
by: jeffc | last post by:
How would you answer a question like this in an interview? I'm not interested in gathering the average of folks on this forum. I'm interested in a strategy for a) evaluating yourself objectively...
5
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
23
by: Steve Jorgensen | last post by:
Hi all, I'm working on a project through a consulting company, and I'm writing some database code for use in another programmer's project in Excel/VBA. The other programmer is working through...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.