1

I have a class that is used to create a "settings" GUI for any other class.

Quick example for clarity: enter image description here

I use attributes to decorate the individual items. For example, you can specify ToolTipText:

[Attributes.Display(ToolTipText = "IP Address of target computer")]
public string IPAddress;

Now, the actual question: I'd like to add functionality to the display to create a group box around several items. My problem is that there isn't a "GroupBox" field or member already there for me to decorate.

I've had a few ideas on how to approach this:

  1. Force the class to contain a private GroupBox that can be decorated. fields would be decorated to assign themselves to the GroupBox. I like that there is real something to decorate. I don't like that it really doesn't belong there. Another problem: If I wanted to create yet another GroupBox as a child of this one it would be hard to order items within the first GroupBox (currently ordering is controlled by the order of the class code itself)

    A. Same as (1), but modification of the GroupBox could be done in the constructor of the settings class.

    B. Same as (1), but the settings class could subscribe to a "Draw" event and customize the ordering of controls there.

  2. Ignore that there's no "physical" object to decorate and add an attribute called GroupBoxName that fields could have. Any field containing the same GroupBoxName would be grouped together. GroupBoxName could contain a "path" to create "sub GroupBoxes" (e.g. "ParentBox/ChildBox"). The problem with this approach is how do I modify the box itself? What if I want the box to have BackColor = Color.Green? I could subscribe to an event with the settings class and specify there.

So, basically I'm looking for ideas on how to approach this. I would like the work from the settings class to be as intuitive as possible while still allowing for some flexibility. I'd also like to design this such that it doesn't have the "code smell" feel.

1

The code smell I'm picking up here is that view details like ToolTipText are being smushed together with model details like IPAddress valued. Shouldn't your IPAddress label be closer to the tool tip than the IPAddress value? Wouldn't it be nice if, lets say you needed to make a french version, if every string that then needs translating were all at the same architectural level?

I'm very conservative when it comes to decorators. They give you enough power to effectively change the language your working with into your own weird custom language. That can work but it has consequences.

2
  • The idea with this is to take an existing class, add some decorations and have a GUI generated through reflection. The decorations are not even required, but allow for some customization of the resulting form. – MikeH Jun 15 '17 at 21:00
  • The idea with not doing this is to follow a single responsibility. To let a class not know more than it needs to know to do it's job. – candied_orange Jun 15 '17 at 21:52
0

You could define a separate structure for grouped items.

For example. instead of

class User {
    string Name;
    string Phone;
    string Street;
    string City;
    string State;
}

You could do this:

class Address {
    string Street;
    string City;
    string State;
}


class User {
    string Name;
    string Phone;
    Address Address;
}

And then decorate at both levels:

class Address {
    [Attributes.Display(ToolTipText="Mailing street address")]
    string Street;

    [Attributes.Display(ToolTipText="Mailing city")]
    string City;

    [Attributes.Display(ToolTipText="State code")]
    string State;
}

class User {
    [Attributes.Display(ToolTipText="The legal name of the user")]
    string Name { get; set; }

    [Attributes.Display(ToolTipText="Primary phone number")]
    string Phone { get; set};

    [Attributes.Display(Style="Grouped", BackColor="Green")]
    Address Address { get; private set; }
}
2
  • Thanks for your suggestion. The primary use of this is to provide a drop in GUI without having to refactor. My form does support multiple classes and works pretty nicely. My current problem is a settings class stored in a single database table that cannot be refactored into multiple classes. – MikeH Jun 15 '17 at 20:57
  • @MikeH, application design should not depend on database structure. You can create classes which supports your UI structure and then save them to one table in database. Use mapping for converting multiple classes to one – Fabio Oct 14 '17 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.