Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Professionals,

I have to take input of item rate for which the allowed mask is ##,###.##.

I am not using a masked textbox for known obvious reasons because we are to take values like 612.36. The textbox control has been taken care to accept numbers only with one decimal point and all that.

My further requirement :

When the user keys in a decimal point (ASCII 46) I have to move the cursor to the point next to the decimal point. Is there a way to do this ?

Or, Is there any other way to provide a user friendly textbox to take in Double/Float values. Please help.

Thanks in advance.
Posted
Updated 23-Jan-15 23:23pm
v2
Comments
Zoltán Zörgő 24-Jan-15 5:27am    
Why do you need the second decimal point?
Priya-Kiko 24-Jan-15 5:30am    
Thanks for the reply.

Not the second decimal point. I just want to move the cursor next to the decimal point for the user to key in the fraction part.
Zoltán Zörgő 24-Jan-15 5:33am    
Ok, but why moving the caret instead of inserting that decimal point? You can't have numbers lik 12_.15 after all... What is the use of it? You better validate the input.
Priya-Kiko 24-Jan-15 5:42am    
Validating the input is a post operation, which anyway has to done. But, can nothing be done during the input?

I thought I could capture the ASCII 46 in the Keypress and move the cursor after the decimal point and would format the integer portion of the text to take care of the abnormalities. Anyway, at the foreground user feels the input friendly.

Is my approach wrong. Please advice.

1 solution

Why use a single text box to hold two values?
All that does is make it harder for the user as he has to remember to separate the two values properly, and that can be complicated to handle.

Instead, I would use two NumericUpDown controls - one for each value. They are a lot easier for the user to play with!
If I can't use two up down controls, then I'd use two textboxes or masked text boxes instead. The one thing I wouldn't do is try to get two inputs in the same textbox!


"The control is first initialized to 0.00 where I have set the decimals to 2.
When user keys in a value like 365.40, he/she has to delete the 0's and the decimal point in there already. Which is what I want to avoid."


In which case you are going to be better off using a textbox - but handle the KeyPress method, and check for '.' - if you get it, use the TextBox SelectionStart and SelectionLength properties
C#
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == '.')
        {
        int index = myTextBox.Text.IndexOf('.');
        if (index >= 0)
            {
            // Allready has decimal
            index++;
            myTextBox.SelectionStart = index;
            myTextBox.SelectionLength = myTextBox.Text.Length - index;
            e.Handled = true;
            }
        }
    }

That way, when the user presses '.', the cursor will more immediately after the existing decimal point, and the digits to the right will be selected - so the next digit he types will delete the existing fraction part of the number.
 
Share this answer
 
v3
Comments
Priya-Kiko 24-Jan-15 4:56am    
Thanks for your immediate reply.

Iam not accepting two values. But a (Double) value. Rate of an item like 500.00, 350.50.
OriginalGriff 24-Jan-15 5:02am    
Ah! You confused me! :laugh:

In which case, I'd use a single NumericUpDown: you can set them to a maximum value and a precision so the user just enters what he needs and can't go outside your limits.
Priya-Kiko 24-Jan-15 5:22am    
Sorry if my question was confusing. :)

I tried using the NumericUpDown control.

The control is first initialized to 0.00 where I have set the decimals to 2.
When user keys in a value like 365.40, he/she has to delete the 0's and the decimal point in there already. Which is what I want to avoid.
OriginalGriff 24-Jan-15 5:48am    
Answer updated
Priya-Kiko 24-Jan-15 6:01am    
Thanks a lot. That is exactly what I had to do. Thank you so much.

This solution works very well with a masked text box with PromptChar = space. Exactly what I wanted. Thanks.

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