Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# show browsable child properties in designer

I'm using .NET C# with standard WinForms, not WPF.

I have this situation. I'm creating a user control for a month calendar, similar to the .NET one but with a little more functionality. I have a user control form, that fills with button objects representing dates. The buttons can be colored with different color depending on their state(selected, mouse over, weekend...)

The way I'd like it to work is extending the button class to accept states, which determine colors, rather than coloring them from the parent (user control) class. There are 10 colors at the moment and I'd really wouldn't like to mess up the user control code with coloring conditions.

Also I would like to select all the colors at design time, using browsable designer properties. The problem is that the designer shows only properties defined in the user control class, and not its children (buttons).

Is there any workaround for this problem? So to put it short I want to change colors using internal button properties, and to be able to select them at design time, using designer properties, and not hard coding them manually.

like image 781
Zoran Ivancevic Avatar asked Jan 23 '23 00:01

Zoran Ivancevic


1 Answers

Ok, I'll try to explain trough code:

For example, I have a user control and a button class. I want to expose Button properties, and make them visible among MyControl properties in designer.

class MyControl : UserControl
{
     private MyButton button;
     button.ChangeStyle("Selected");
}

class MyButton : Button
{
     private Color buttonColor;

     public void ChangeStyle(string styleName)
     {
          if (styleName == "Selected")
              this.BackColor = buttonColor;
     }

     [Browsable(true)]
     [Category("Button style")]
     public Color ButtonColor
     {
          get { return buttonColor; }
          set { buttonColor = value; }
     }
}

This is a simple example. Normally I have 5 different styles including background and foreground color for each of them. So instead of managing colors in MyControl class, I'd like to define them in MyButton class. But the problem this way is that the properties in the MyButton class aren't visible in designer, because it only focuses on MyControl properties.

Btw. ignore the missing constructors and other basic classes stuff in the code example

I can't use:

[Category("Wonder Control")]
public Color ButtonBackColor { get { return button.BackColor; } set { button.BackColor = value; }

because I have 30 buttons in MyControl (days in month), and I can't reference just a single object.

like image 172
Zoran Ivancevic Avatar answered Jan 27 '23 03:01

Zoran Ivancevic