继承的TextBox缺少CaretIndex属性

时间:2014-02-23 01:32:11

标签: c# winforms textbox inherited

我有以下代码:

public class myTextBox : TextBox
{
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
        if (Char.IsDigit(e.KeyChar))  // Digits are OK
        {
            // execpt if cursor is at right end
            if (this.CaretIndex == this.Text.Length)
            {
                e.Handled = true;    // throw away keypress
            }
        }
    }
}

我收到错误:

'MyTextBoxes.myTextBox'不包含'CaretIndex'的定义,也没有扩展方法'CaretIndex'......

即使CaretIndex是TextBox属性:http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.caretindex(v=vs.110).aspx

3 个答案:

答案 0 :(得分:2)

您正在查看WPF的文档。

WPF System.Windows.Controls.TextBox控件具有CaretIndex属性。

WinForms System.Windows.Forms.TextBox控件没有。

答案 1 :(得分:0)

更改了这一行:

if (this.CaretIndex == this.Text.Length)

到此:

if ((this.Text.Length > 0) && ((this.SelectionStart + this.SelectionLength) == this.Text.Length))

答案 2 :(得分:0)

if(this.SelectionStart == this.Text.Length)
{
    e.Handled = true;    // throw away keypress
}

瓦尔特

相关问题