2011-04-12 53 views

回答

4

這應該處理大多數情況下我相信:

private void TextBox_TextChanged(object sender, EventArgs e) 
{ 
    TextBox tb = sender as TextBox; 
    if (tb != null) 
    { 
     int pos = tb.SelectionStart; 
     int length = tb.Text.Length; 
     tb.Text = tb.Text.Replace(",,", ","); 
     int diff = length- tb.Text.Length; 
     tb.SelectionStart = pos == 0 || diff == 0 ? pos : pos - diff; 
    } 
} 

這時候您鍵入的文本到TextBox,以及當你將文本粘貼到它的工作原理。

1
private void TextBox_KeyDown(object sender, KeyEventArgs e) 
{ 
    if(e.KeyChar == ',') 
    { 
     if(TextBox.Text.Contains(",,")) 
     { 
      e.Handled = true; 
     } 
    } 
} 
+0

你要設置'SuppressKeyPress =真',而不是'Handled',如備註部分說明如下:HTTP ://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx – 2011-04-12 07:25:40

相關問題