2016-05-23 52 views
1

我寫了一個只允許整數值到文本框的事件。現在需要將文本框值更改爲印度貨幣格式(在文本更改事件)

我的附加要求是,我需要改變文本框的整數值印度貨幣格式(我需要在OnTextchanged事件在WPF實現這一點)

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) 
      { 
      } 
     } 
    } 
+0

只需將'xml:lang =「en-IN」'添加到'UserControl'或'Window'開始標記。 – XAMlMAX

+0

所有這些都可以替換爲'' – XAMlMAX

+0

嗨它不工作,我正在編譯時間錯誤 in Text =「{綁定值StringFormat = \ {0:C \}}「 –

回答

0

您好我實現它通過使用下面的代碼

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) 
      { 
      } 
     } 
    }