2017-06-25 111 views
1

我寫了這個代碼當我點擊或集中在文本框中選擇的炭突出 但是當我輸入(編輯)的亮點不能顯示(文本框具有默認文本)C#突出顯示文本當前焦炭打字時

 textBox1.GotFocus += textbox1_OnFocus; 

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) 
     { 
      e.Handled = true; 
     } 

     // only allow one decimal point 
     if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) 
     { 
      e.Handled = true; 
     } 

     textBox1.SelectionStart = textBox1.SelectionStart; 
     textBox1.SelectionLength = 1; 

    } 

    private void textbox1_OnFocus(object sender, EventArgs e) 
    { 
     textBox1.Focus(); 
     textBox1.SelectionStart = 0 ; 
     textBox1.SelectionLength = 1; 

    } 

    private void textBox1_MouseClick(object sender, MouseEventArgs e) 
    { 
     listBox1.Items.Add(textBox1.SelectionStart); 
     textBox1.SelectionStart = textBox1.SelectionStart; 
     textBox1.SelectionLength = 1; 
    } 

如何編輯我的代碼以獲得我的正確答案?

回答

0

我寫這它的工作,但我覺得這不是好辦法

 private void textBox1_KeyUp(object sender, KeyEventArgs e) 
    { 
     textBox1.SelectionStart = textBox1.SelectionStart; 
     textBox1.SelectionLength = 1; 
    }