繁体   English   中英

需要将文本框值更改为印度货币格式(在文本更改事件上)

[英]Need to Change the Textbox Value to Indian Currency Format (On Text changed event)

我写了一个事件,只允许整数值到文本框。

现在我的附加要求是我需要将Textbox Integer值更改为Indian Currency Format(我需要在WPF中的OnTextchanged事件中实现此功能)

   private void validateTextInteger(object sender, EventArgs e)
    {
        NumberFormatInfo nfi = new CultureInfo("en-IN", false).NumberFormat;
        nfi = (NumberFormatInfo)nfi.Clone();
        nfi.CurrencySymbol = "";
        Exception X = new Exception();
        TextBox T = (TextBox)sender;
        try
        {
            if (T.Text.Trim() != "-")
            {
                int x = int.Parse(T.Text);
                T.Text = string.Format(nfi, "{0:C0}", x);
                return;
            }
        }
        catch (Exception)
        {
            try
            {
                int CursorIndex = T.SelectionStart - 1;
                T.Text = T.Text.Remove(CursorIndex, 1);
                //Align Cursor to same index
                T.SelectionStart = CursorIndex;
                T.SelectionLength = 0;
            }
            catch (Exception)
            { 
            }
        }
    }

嗨,我通过使用以下代码实现了它

    private void validateTextInteger(object sender, EventArgs e)
    {


        NumberFormatInfo nfi = new CultureInfo("en-IN", false).NumberFormat;
        nfi = (NumberFormatInfo)nfi.Clone();
        nfi.CurrencySymbol = "";
        Exception X = new Exception();
        TextBox T = (TextBox)sender;
        try
        {
            if (T.Text.Trim() != "-")
            {
                var val = T.Text.Trim();
                int x=0;
                val = val.Replace(",", "");
                if (!(val.Contains(',')))
                {
                     x = int.Parse(val);
                }
                string  s  = string.Format(nfi, "{0:C0}", x);
                T.Text = s.Trim();
                T.SelectionStart = T.Text.Length ;
                T.SelectionLength = 0;
                return;
            }
        }
        catch (Exception)
        {
            try
            {
                int CursorIndex = T.SelectionStart - 1;
                T.Text = T.Text.Remove(CursorIndex, 1);
                //Align Cursor to same index
                T.SelectionStart = T.Text.Length;
                T.SelectionLength = 0;
            }
            catch (Exception)
            { 
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM