0

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

Derek Johnson
  • 927
  • 1
  • 11
  • 15

3 Answers3

2

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.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Oops - missed that (wish it was more prominent in the docs.) Question is now how to determine if the cursor is at the right end of the text...(preferably without resorting to ElementHost.) – Derek Johnson Feb 23 '14 at 02:14
0

Changed this line:

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

to this:

if ((this.Text.Length > 0) && ((this.SelectionStart + this.SelectionLength) == this.Text.Length))
Derek Johnson
  • 927
  • 1
  • 11
  • 15
0
if(this.SelectionStart == this.Text.Length)
{
    e.Handled = true;    // throw away keypress
}

valter