2014-02-23 62 views
0

我有以下代码:继承文本框丢失caretIndex属性

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

回答

0

改变了这一行:

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

这样:

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

你正在寻找的WPF的文档。

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

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

+0

哎呀 - 错过了这个问题,现在(希望这是在文档中更加突出。)如何确定光标是否位于文本的右端...(最好不使用ElementHost) –

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

瓦尔特

+0

如果选择了任何文本,这不起作用... –