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

KeyUp event with TextBox

I currently have this code in my kepup event of a text box. It works
except for the fact that if the number
25,000 or any number is put in then the user arrows to the left of the
third zero and tries to replace the number 0 with a 4, this is the
result
25,004

Here is my code. Any help would be appreciated.

public static void KeyStroke_FormatNumericText(string number,TextBox
txtBx) {

try {
long iReturn
=Int64.Parse(number,System.Globalization.NumberSty les.AllowThousands);
txtBx.Text = iReturn.ToString("#,0");
txtBx.SelectionStart = txtBx.Text.Length;
}
catch(Exception ex) {
Debug.WriteLine(ex.ToString());
}
}

Nov 16 '05 #1
3 10644
Hi JPSutor,

Could you explain what your goal with the TextBox is as my attempts to
test your code resulted in a TextBox that only shows a single number at a
time. Could you show us your KeyUp event as well?

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
KeyStroke_FormatNumericText(txtSquareFootage.Text, txtSquareFootage);

Nov 16 '05 #3
Alright, I passed the Key value as a parameter to the method instead of
the whole text.
If I understand your problem correctly, it does not handle moving the
arrow keys and trying to replace specific digits correctly.

This is caused by the fact that even the arrow keys are considered input
and the marker is pushed back to the end. Try calling the method for
certain keys only, something like

private void tb_KeyUp(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.Back:
case Keys.Delete:
KeyStroke_FormatNumericText(tb.Text,tb);
break;
}
}

A better way might be to set a flag in the KeyPress event if
Char.IsDigit(e.KeyChar) and call the method in the keyup event only if the
flag is set or Keys.Delete or Keys.Back is entered. This would be better
as the above code will let through [SHIFT]+Keys.D0 etc while also ignoring
Numpad keys.

Or you could store the TextBox string somewhere and compare it in the
keyup event, if it is changed, call the method.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

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

Similar topics

0
by: Amaro Cantador | last post by:
Hi, I need help in the following: I have a base class, called frmBase, in wich I have a routine that gives to all form objects a handle to track the KeyUp Event of all inherited class object,...
1
by: Ori | last post by:
Hi Guys, Here is my problem, but maybe someone can help me with this. Background: 1. Using C#. 2. I'm having a form which the KeyPreview is on (and must stay like this) and I'm...
3
by: trint | last post by:
Ok, I have tried to do this with the System.Web.UI and can't find anything for the webform. It seems much easier for a Winform. Any help in trapping Webform keydown event and keyup event is...
0
by: Gene Hubert | last post by:
I'm trying to catch the KeyUp event in textbox of a DataGrid. I'm picking up the keydown and keypress events ok, but not keyup. Can anyone see what is wrong with this code. I been fighting with...
2
by: Adam J. Schaff | last post by:
I have recently noticed an unwanted behavior that I do not know how to get rid of. To Recreate Problem: Windows Forms App with 2 forms. Form 1 has nothing on it and this code underneath: ...
4
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As...
3
by: Gidi | last post by:
Hi, I'm trying to look for an aswer to that question for long long time and i can't find it. I've a DataGrid which i want to bound one of it's cell to a KeyUp event, and it doesn't work. All...
1
by: Johnny E. Jensen | last post by:
Hello I have a textbox where i check for a "return" or "enter" keystroke for that i use the txtBox KeyUp and checks the e.KeyCode value. But when i do stroke the return or enter kay, a sound...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
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: 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
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: 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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.