【发布时间】:2017-06-25 19:03:38
【问题描述】:
当我单击或专注于文本框选择时,我编写了这段代码选择字符突出显示 但是当我输入(编辑)突出显示时(文本框有默认文本)
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;
}
如何编辑我的代码以获得正确答案?
【问题讨论】: