Creating a simple UserControl from a TextBox

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
 
J

Jeff Johnson

Just a few code comments:
foreach (char c in tb.Text.ToString())


The Text property is a string, so there's no need for ToString().
{
if (char.IsDigit(c))
{
this.BackColor = Color.Red;
change = true;

Once you have determined that you need to set the back color, there's no
need to process any more chars, so you should add

break;
}
}

if (!change)
this.BackColor = Color.Empty;

You should use SystemColors.WindowColor instead of Color.Empty.
 
I

Ignacio Machin ( .NET/ C# MVP )

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

It depends of what you want to do, in your case you are specializing
the TextBox control, so deriving from it is the best solution in your
case. If you want that next to the textbox display a calendar (cause
you expect a date in the textbox) then it's best to use UserControl
 
T

Tony Johansson

Hello!

What is the reason for doing this.
base.OnTextChanged(e);
in your code?

//Tony
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

What is the reason for doing this.
 base.OnTextChanged(e);
in your code?

You should always call the base implementation of an event. If not you
need to re implement what the base does (this would be kind of
pointless).
 
T

Tony Johansson

Hello!

I have two questions according to the On event for example we can use
OnTextChanged() method for this example
1. When should I use and override this OnTextChanged() method instead of
using the event handler TextChanged.
I dragged a TextBox from the Toolbox and then add this OnTextChanged()
to the form and then I wrote something in the
TextBox but this OnTextChanged() method was not called. So can I use
and override this On.. only when inherit from
another Control for example TextBox
2. What is the advantage to use and override this OnTextChanged() method
compared to using the event handler
TextChanged.

//Tony
 
T

Tony Johansson

Hello!

This question is just for geting more knowledge about .NET and C#.
The form class override the OnTextChanged located in the Control class.
I just wonder what can I do to make the OnTextChanged be called in the form
class.

//Tony
 
T

Tony Johansson

Hello!

I found the answer myself.

//Tony


Tony Johansson said:
Hello!

This question is just for geting more knowledge about .NET and C#.
The form class override the OnTextChanged located in the Control class.
I just wonder what can I do to make the OnTextChanged be called in the
form class.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top