【问题标题】:C# highlight current char in textbox while typingC#在键入时突出显示文本框中的当前字符
【发布时间】: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;
    }

如何编辑我的代码以获得正确答案?

【问题讨论】:

    标签: c# char highlight


    【解决方案1】:

    我写了它,但我认为这不是好方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      相关资源
      最近更新 更多