In order to work with the events of the inner
TextBox
, directly from your
UserControl
, you can expose the events that you need. Here is an example implementation of this idea:
public partial class TextBoxEx : UserControl
{
private TextBox textBox = new TextBox();
private Color borderColor = Color.Gray;
public TextBoxEx()
{
InitializeComponent();
this.Paint += new PaintEventHandler(TextBoxEx_Paint);
this.Resize += new EventHandler(TextBoxEx_Resize);
textBox.Multiline = true;
textBox.BorderStyle = BorderStyle.None;
this.Controls.Add(textBox);
InvalidateSize();
}
public override string Text
{
get { return textBox.Text; }
set { textBox.Text = value; }
}
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; Invalidate(); }
}
public event EventHandler TextBoxClick
{
add { textBox.Click += value; }
remove { textBox.Click -= value; }
}
private void TextBoxEx_Resize(object sender, EventArgs e)
{
InvalidateSize();
}
private void TextBoxEx_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, borderColor, ButtonBorderStyle.Solid);
}
private void InvalidateSize()
{
textBox.Size = new Size(this.Width - 2, this.Height - 2);
textBox.Location = new Point(1, 1);
}
}
Now you can use this event even through the VS designer. Here is an example event handler for the exposed event:
private void textBoxEx1_TextBoxClick(object sender, EventArgs e)
{
textBoxEx1.BorderColor = Color.Red;
}
An alternative is to expose the entire
TextBox
as a public property in user control and subscribe to it's events the way you desire.
Example:
public partial class TextBoxEx : UserControl
{
private TextBox textBox = new TextBox();
public TextBox InnerTextBox { get {return textBox ; } }
}
After doing this you can subscribe on any of its events like (pseudo-code):
textBoxEx.InnerTextBox.EventOfYourChoice += ...
:)