Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I have a very serious problem. I want my digits to be in groups in my textbox as soon as I type them. I used number format like below

if (textBox1.Text != "")
{
  NumberFormatInfo nFI = new CultureInfo("en-US",false).NumberFormat; 

  double int_value = Convert.ToDouble(textBox1.Text);
  textBox1.Text=int_value.ToString("N5", nFI);          
}

but there is a problem when I write this code in the text box (key down event).
After some digit, some digit is inserted from left of text and some others from right but if I write the code in a butten click event after writing the whole text in text box it works.
In fact I can just have grouping after writing whole word in my textbox.

Help me please :(( :(( :(( X|
Posted
Updated 28-Dec-10 9:47am
v2
Comments
JF2015 28-Dec-10 15:48pm    
Edited to add code formatting and improve readybility. Please use <pre> tags when posting code snippets.

You don't want to use the KeyDown event - you should use the TextChanged event.

 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Dec-10 1:11am    
I think you're right - my 5, but also please see Manfred's answer and my comment.
e.mn 29-Dec-10 3:59am    
thx but i cant use textchange event because as you see i changed text of text box in my algoritm so if i write it in textboxchange event it causes a recursive without an end and over flow for stack so i must use an other event what event do you segest key down and key presss raise before text change
From a small win form app I just wrote to understand your problem, I came to the conclusion that when you set the Text property of the textbox, the current caret position is lost. You can read the caret position beforehand and after you write your reformatted string to the textbox set the caret position back to where it belongs.

Cheers,

Manfred
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Dec-10 1:10am    
Manfred, you're right; my 5. I also think you should have confirmed John's advice to use TextChanged event.

I must add that keeping such invariant as validity of input on each text input event is a difficult problem, in many cases not solvable in principle, in other cases impractical to address.
Further, in some cases successful solution of this problem makes UI only worse. Example: you want to enforce signed value to be valid all the time. The user wants to change the figure by inserting minis sign in the middle of the digits and then delete unwanted part of them; and what, minus will be ignored in this case but not ignored in right place?
But it breaks the natural expectation of behavior of text editor! No, text editor must remain text editor.

Different story is simple filtering, another approach -- field editor.
e.mn 29-Dec-10 3:58am    
it seems your right but
how could i set caret position to right place, any suggestion??!!!
Manfred Rudolf Bihy 29-Dec-10 11:37am    
@SAKryuko: 5'ed John, ... done! :)
Manfred Rudolf Bihy 29-Dec-10 11:44am    
@e.mn: Use the SelectionStart and SelectionEnd properties of TextBox. When both are set to the same value the cursor will appear at the selected location. You'll have to implement some logic though to get the positioning right. I've recently solved a problem in javascript that may give you some ideas to the when's and where's of the proper logic, see here:

http://www.codeproject.com/Answers/140532/How-to-restrict-space-between-characters-in-textbo.aspx#answer1
Have you tried using the KeyPress event?

You also need to add some validation, Convert.ToDouble(textBox1.Text); will throw an exception if the a non-digit is in the textbox.
 
Share this answer
 
solution:

thx for all answers i got the solution i just use int culture and break the text to two int and double part an then i concat them
the cualture that works is
C#
textBox1.AppendText(val.ToString("#,#", CultureInfo.InvariantCulture));

.......
goodluck
 
Share this answer
 
v3
Hi every one
This works good:

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            bool f = true;
            if (textBox1.Text != "" && f)
            {
                f = false;
                decimal d = Convert.ToDecimal(textBox1.Text.Replace(",", ""));
                textBox1.Text = d.ToString("###,###,###");
                string s = textBox1.Text + " ";
                textBox1.Select(textBox1.Text.Length, 1);
            }
        }
 
Share this answer
 
Comments
Member 14733107 3-Feb-20 3:29am    
This is not working with the numbers entered with decimal
Use leave event instead of key events.
 
Share this answer
 
Comments
Henry Minute 28-Dec-10 16:17pm    
That won't help the OP, who wants the text grouped as he types.
e.mn 29-Dec-10 4:01am    
yeah i want dynamic grouping ,so i cant use leave event it causes a static groupinc i want didit in groups as soon as i type them
thx any way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900