Custom Textbox Class No Worky

  • Thread starter Thread starter Steven C
  • Start date Start date
S

Steven C

Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!

Steven



using System;
using System.Windows.Forms;
using System.Drawing;

namespace Chrismon.Form.Objects
{

public class ChrismonTextBox : TextBox
{
public ChrismonTextBox()
{
TextBox ChrismonTextBox = new TextBox();
ChrismonTextBox.BackColor = Color.Blue;
ChrismonTextBox.Width = 200;
ChrismonTextBox.Height = 40;


}

}
}
 
You're actually creating a local object in your constructor which is
probably not what you intended.

Try this..

public ChrismonTextBox() : base () // This is not absolutely necessary,
though
{
this.BackColor = Color.Blue;
this.Width = 200;
this.Height = 40;
}

-vJ
 
You are actually instantiating a new copy, and setting that object's
properties.

Try this instead -- Note the "this.xxx" call, instead of creating a new
object and setting properties on this. Note the call to the base class's
constructor by adding ": base()"

using System;
using System.Windows.Forms;
using System.Drawing

namespace Chrismon.Form.Objects
{
public class ChrismonTextBox : TextBox
{
public ChrismonTextBox() : base()
{
this.BackColor = Color.Blue;
}
}
}
 
Hi Steve,

Hello:

This should be a simply question for most of you guys. Obviously, I'm
new to C#. I'm trying to add a base form objects base class to my
project, so I can pre-configure textboxes, combos, etc, with a blue
backcolor, to be used on all my forms. I compiled this, but when I
use it on my form, the custom control properties are not being set.

What am I missing here? Many thanks!
<snip>

In your current constructor code you are creating, modifying and dumping
a TextBox object that has no connection to the ChrismonTextBox object being
constructed. The constructor should look like this:

public ChrismonTextBox()
{
this.BackColor = Color.Blue;
this.Width = 200;
this.Height = 40;
}

It would also probably be a good idea to override the properties and
supply a new DefaultValueAttribute for each. Not doing this will mean that
the property values in the designer's property grid will appear bold (as if
the user changed them), and if the user "resets" the property value via the
designer, the value will be the default from the base TextBox class.

Regards,
Dan
 
Awesome! That did the trick. All three of your comments were much
appreciated.

Thanks again;
Steven
 
I'm trying to add a base form objects base class
to my project, so I can pre-configure textboxes,
combos, etc, with a blue backcolor,

If all you want to do is apply certain settings to controls, I'd suggest an
alternate approach that doesn't require any inherited classes. Inheritance
is best used when you really need to extend functionality.

private void Form1_Load(object sender, System.EventArgs e)
{
MakeControlsBlue(this);
}

private void MakeControlsBlue(Control ctl)
{
// Loop over all the child controls
foreach (Control ctlChild in ctl.Controls)
{
// We want textboxes and checkboxes to be blue
if (ctlChild is TextBox || ctlChild is CheckBox)
{
ctlChild.BackColor = Color.SkyBlue;
}

// Deal with any controls of the child control
if (ctlChild.HasChildren)
{
MakeControlsChildrenBlue(ctlChild);
}
}
}

P.
 
Back
Top