问题在数字文本框中,第三个分隔 [英] Problem On a numeric textbox with sepration on third

查看:96
本文介绍了问题在数字文本框中,第三个分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理一个TextBox,用户只能输入Integer字符和Backspace键.同样,这些字符将在第三个位置这样分隔(921,323,456).
问题是当我按退格键时,该数字位于数字中间,但该字符将被删除,但光标位置将移至第一个输入整数的第一个位置.例如,在上面的数字中,当我删除字符"4"时,光标位置将移至字符"9".

分隔数字的功能:

I manage a TextBox which user can input just Integer characters and backspace key. Also this characters would be separated on third position like this(921,323,456).
the problem is in middle of number when I press backspace the character would be delete but cursor position will go to first position of first input integer. for example in number above when I delete char ''4'' the cursor position will go to char ''9''.

function for separating numbers:

 public static void SpiltOnThird_TextChanged(object sender, EventArgs e)
{
        TextBox txb = (TextBox)sender;
        txb.Text=numberSpiltOnThird(txb.Text);
        txb.Select(txb.Text.Length, 0);
}
 public static string numberSpiltOnThird(string num)
{
    if (num != ("0") & !string.IsNullOrEmpty(num))
    {
        NumberFormatInfo nfi = new NumberFormatInfo();
        num = num.Replace(",", "");
        num = Convert.ToInt64(num).ToString();
        nfi.NumberDecimalDigits = 0;
        return decimal.Parse(num).ToString("n", nfi);
    }
    else
        return "";
}

推荐答案

为什么不使用文本框的遮罩,

然后在特定文本框中的按键事件上使用上面的代码

why don''t you use masking of textbox,

and then on keypress event on particular textbox use the above code

if (!char.IsNumber(e.KeyChar) && e.KeyChar.ToString() != "\b")
            {
                e.Handled = true;
            }



希望对您有所帮助:)



hope dis helps :)


文本框中的当前插入符位置(指示用户输入位置的"I"光束,"cursor"是不同的野兽)不是很明显-它是SelectionStart索引值.因此,请先阅读它,这样您便知道他在哪里,然后可以将其设置为将他放在您想要的地方.
The current caret position (the "I" beam that indicates the user input position, "cursor" is a different beastie) in a text box isn''t obvious - it''s the SelectionStart index value. So read it first, so you know where he was, and you can set it afterwards to put him where you want him.


这篇关于问题在数字文本框中,第三个分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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