Problem with properties of a user control
Hello !
So, I have built a user control (named MyUsercontrol for exemple) with two components : a Label and a TextBox.
In a window form, I have added to a form, a component, MyUserControl. When I try to changes properties such as ForeColor or BackColor of MyUserControl, only the Label component changes. When I try to change the properties font, both components (Label and TextBox) change.
Why is it different ? In addition, TextBoxes have properties such as ForeColor and BackColor ...
Thanks for your help
[516 byte] By [
Syl20] at [2007-12-16]
The TextBox is different to the Label, in that by default it uses SystemColors.WindowText for ForeColor and SystemColors.Window for BackColor.
Simply override the ForeColor and BackColor properties and make sure you set the TextBox's Back and Fore color in them, for example (assuming you have a textbox called textbox1 on the user control):
public class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
textBox1.BackColor = BackColor;
textBox1.ForeColor = ForeColor;
}
public override Color ForeColor
{
set
{
textBox1.ForeColor = value;
base.ForeColor = value;
}
}
public override Color BackColor
{
set
{
textBox1.BackColor = value;
base.BackColor = value;
}
}
}
HTH
David