2010-05-15 81 views
0

我想在GotFocus事件中選擇System.Windows.Forms.TextBox()控件的所有文本,但是我找到的所有示例利用控件的.SelectionStart/.SelectionEnd屬性,並且這些屬性在.NET 2.0 Framework中不可用。如何以編程方式選擇TextBox控件的全部文本(Compact .NET 2.0)

using System; 

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 

namespace xCustomControls 
{ 
    public partial class xTextBox : System.Windows.Forms.TextBox 
    { 
     public xTextBox() 
     { 
      InitializeComponent(); 
      this.GotFocus += new System.EventHandler(this.GotFocusHandler); 
     } 

     private void GotFocusHandler(object sender, EventArgs e) 
     { 
      Control ctrl = (Control)sender; 
      ctrl.BackColor = Color.Cyan; 
      ctrl.SelectionStart = 0; 
     } 

錯誤:

「System.Windows.Forms.Control的」不包含關於「SelectionStart」和沒有擴展方法「SelectionStart」接受類型的第一個參數定義「System.Windows.Forms的.Control'可以找到(你是否缺少使用指令或程序集引用?)

任何想法?

TIA, 巴勃羅

回答

2

替換行

Control ctrl = (Control)sender; 

TextBox ctrl = (TextBox)sender; 

編譯器認爲你和一個控制類,它不具有SelectionStart工作,但你的對象真的是TextBox的衍生物,它的確如此。

相關問題