2016-07-22 44 views

回答

0

按鍵事件都會在每一個按鍵觸發,所以你可以把你的格式代碼在事件處理程序,以確保您的格式的代碼上進行任何更改運行文本框

-1
public void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
{ 
     if ((int)(e.KeyChar) != 8) 
     { 
      if ((((int)(e.KeyChar) < 48 | (int)(e.KeyChar) > 57) & (int)(e.KeyChar) != 46) | ((int)(e.KeyChar) == 46 & textBox1.Text.Contains("."))) 
      { 
       e.Handled = true; 
      } 
      else 
      { 
       if (!textBox1.Text.Contains(".")) 
       { 
        int iLength = textBox1.Text.Replace(",", "").Length; 
        if (iLength % 3 == 0 & iLength > 0) 
        { 
         if ((int)(e.KeyChar) != 46) 
         { 
          textBox1.AppendText(","); 
         } 
        } 
       } 
      } 
     } 
     else 
     { 
      if (textBox1.Text.LastIndexOf(",") == textBox1.Text.Length - 2 & textBox1.Text.LastIndexOf(",") != -1) 
      { 
       textBox1.Text = textBox1.Text.Remove(textBox1.Text.LastIndexOf(","), 1); 
       textBox1.SelectionStart = textBox1.Text.Length; 
      } 
     } 
    } 
+0

有點太令人費解,但不止於此:逗號不是隨處使用, 3位數字分組不是通用的,它充滿幻數,光標位置丟失... –

+0

逗號和3位數字系統在計算器中使用,正如@DasturchiUZ所問,因此我實現了相同。 –

+0

僅供參考計算器使用計算機的區域設置... –

0

在鍵盤類型的事件:

  • 解析輸入的文字轉換成整數並存儲的值,例如:int theinteger
  • 重寫回textbox.Textstring.Format("{0:n0}", theinteger);