I want to select all the text of a System.Windows.Forms.TextBox() control in a GotFocus event, but all the examples I found make use of the .SelectionStart / .SelectionEnd properties of the control, and those aren't available in .NET 2.0 Framework.
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;
}
开发者_高级运维
Error:
'System.Windows.Forms.Control' does not contain a definition for 'SelectionStart' and no extension method 'SelectionStart' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?)
Any ideas?
TIA, Pablo
Replace the line
Control ctrl = (Control)sender;
with
TextBox ctrl = (TextBox)sender;
The compiler thinks you're working with a Control class, which doesn't have SelectionStart, but your object is really a derivative of TextBox, which does.
精彩评论