Вопрос

I have the following code:

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
            }
        }
    }
}

and I get the error:

'MyTextBoxes.myTextBox' does not contain a definition for 'CaretIndex' and no extension method 'CaretIndex'...

even though CaretIndex is a TextBox property: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.caretindex(v=vs.110).aspx

Это было полезно?

Решение 2

Changed this line:

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

to this:

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

Другие советы

You're looking at the documentation for WPF.

The WPF System.Windows.Controls.TextBox control has a CaretIndex property.

The WinForms System.Windows.Forms.TextBox control does not.

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

valter

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top