请指导我如何格式化文本框以输入双值。 [英] Please guide me how to format the textbox to take input of double values.

查看:73
本文介绍了请指导我如何格式化文本框以输入双值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专业人士,



我必须输入允许的面具为##,###。##的项目费率。



由于我们要采用像612.36这样的值,我没有使用蒙面文本框。文本框控件已经注意接受只有一个小数点的数字和所有这些。



我的进一步要求:



当用户键入小数点(ASCII 46)时,我必须将光标移动到小数点旁边的点。有没有办法做到这一点?



或者,有没有其他方法可以提供用户友好的文本框来接受Double / Float值。请帮忙。



提前致谢。

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.

推荐答案

为什么使用单个文本框来保存两个值?

所做的就是让用户更难,因为他必须记住正确地分离这两个值,这可能很难处理。



相反,我会使用两个NumericUpDown控件 - 每个值一个。它们对于用户来说更容易玩!

如果我不能使用两个向下控制,那么我将使用两个文本框或屏蔽文本框。我不会做的一件事就是尝试在同一个文本框中输入两个输入!





控件是首先将其初始化为0.00,其中我将小数设置为2.

当用户键入365.40之类的值时,他/她必须删除那里的0和小数点。这就是我的意思想要避免。




在这种情况下,您最好使用文本框 - 但要处理KeyPress方法,并检查'​​。 ' - 如果你得到它,使用TextBox SelectionStart和SelectionLength属性

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
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.


这篇关于请指导我如何格式化文本框以输入双值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆