473,804 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Forma tNumericText(st ring number,TextBox
txtBx) {

try {
long iReturn
=Int64.Parse(nu mber,System.Glo balization.Numb erStyles.AllowT housands);
txtBx.Text = iReturn.ToStrin g("#,0");
txtBx.Selection Start = txtBx.Text.Leng th;
}
catch(Exception ex) {
Debug.WriteLine (ex.ToString()) ;
}
}

Nov 16 '05 #1
3 10668
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_Forma tNumericText(tx tSquareFootage. Text,txtSquareF ootage);

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.KeyCod e)
{
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_Forma tNumericText(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
1129
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, to centralize them. When in the derivated class, called frmXPTO, the users press the Enter key ou Esc Key this routine will be called (so far so good) catches wich key was
1
4507
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 implementing some functions on the Key up event of some controls and the form.
3
3284
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 appreciated. Thanks, Trint
0
1423
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 this for way too long. Thanks a ton, Gene H. ==================
2
2357
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: Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Return Then
4
7133
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 System.Windows.Forms.KeyEventArgs) _ Handles ListBox1.KeyUp, ListBox1.KeyDown If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue = Keys.End Or e.KeyValue = Keys.Home Then
3
1950
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 other events work, and KeyUp on a regular TextBox works just fine. It worked for me in the past, and just stopped suddenly (i guess after Windows Update). Maybe it's related to .NET Version, i don't know, but i really have to find a solution.
1
3085
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 (ping) sounded. How can i prevent that ping? Kind regards Johnny Jensen
2
19301
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 have created for this purpose is derived from class UserControl.
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
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
10354
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
10097
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
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
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.