继承的文本框缺少插入符号索引属性



我有以下代码:

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

您正在查看 WPF 的文档。

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

WinForms System.Windows.Forms.TextBox控件不会。

更改了此行:

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

对此:

if ((this.Text.Length > 0) && ((this.SelectionStart + this.SelectionLength) == this.Text.Length))
if(this.SelectionStart == this.Text.Length)
{
    e.Handled = true;    // throw away keypress
}

瓦尔特

最新更新