T
Tony Johansson
Hello!
Assume I want to create a very simple UserControl that has the following
functionality.
I have a TextBox which should be turned red as soon as you enter a figure.
I have implemented this in two different way.
The first way is to create a UserControl and inherit from TextBox in the
this way.
public partial class UserControl1 : TextBox
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_TextChanged(object sender, EventArgs e)
{
bool change = false;
TextBox tb = (TextBox)sender;
foreach (char c in tb.Text.ToString())
{
if (char.IsDigit(c))
{
this.BackColor = Color.Red;
change = true;
}
}
if (!change)
this.BackColor = Color.Empty;
}
}
My second way is to create a UserControl and keep the base class as
UserControl but
instead drag a TextBox into the area by using the user Control Designer
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool change = false;
TextBox tb = (TextBox)sender;
foreach (char c in tb.Text.ToString())
{
if (char.IsDigit(c))
{
tb.BackColor = Color.Red;
change = true;
}
}
if (!change)
tb.BackColor = Color.Empty;
}
}
Now to my question these two solutions to the same thing.
I want to ask you which solution is to recommend. I woud say that
alternative 1 when I derived from TextBox is the best solution.
//Tony
Assume I want to create a very simple UserControl that has the following
functionality.
I have a TextBox which should be turned red as soon as you enter a figure.
I have implemented this in two different way.
The first way is to create a UserControl and inherit from TextBox in the
this way.
public partial class UserControl1 : TextBox
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_TextChanged(object sender, EventArgs e)
{
bool change = false;
TextBox tb = (TextBox)sender;
foreach (char c in tb.Text.ToString())
{
if (char.IsDigit(c))
{
this.BackColor = Color.Red;
change = true;
}
}
if (!change)
this.BackColor = Color.Empty;
}
}
My second way is to create a UserControl and keep the base class as
UserControl but
instead drag a TextBox into the area by using the user Control Designer
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
bool change = false;
TextBox tb = (TextBox)sender;
foreach (char c in tb.Text.ToString())
{
if (char.IsDigit(c))
{
tb.BackColor = Color.Red;
change = true;
}
}
if (!change)
tb.BackColor = Color.Empty;
}
}
Now to my question these two solutions to the same thing.
I want to ask you which solution is to recommend. I woud say that
alternative 1 when I derived from TextBox is the best solution.
//Tony