views:

118

answers:

3

I am developing a new custom control in VisualStudio and wonder whether you can limit the property selection at design time.

To illustrate the problem, there are two properties that rely on each other – orientation and textside. The control itself is rectangular and the orientation can be either vertical or horizontal. What I want is to limit the textside property so that if the orientation is vertical the textside can only be left or right and if orientation is horizontal the textside can only be top or bottom.

Clearly you can do this at runtime within a set property method by checking other properties but what I would like to do this within the property window as design time so there is no chance of someone choosing the wrong combination of properties and then nothing or the wrong information is displayed when the project is run.

Currently I have this:

public enum VerticalTextSide { Left, Right }
public enum HorizontalTextSide { Top, Bottom }
public enum TextSide { }
public enum Orientation { Vertical, Horizontal }

private VerticalTextSide vts;
private HorizontalTextSide hts;
private TextSide db;
private Orientation or;

public TextSide textSide
{
    get
    {
        if (or == Orientation.Vertical)
        {
            [need help!!]
        }
        else
        {
            [need help!!]
        }
    }
    set
    {
        [need help!!]
    }
 }

 public Orientation orientation
 {
     get
     {
         return or;
     }
     set
     {
         or = value;
     }
 }

Where I need help is to return and set the enum values depending on the orientation chosen. Maybe there is another way, perhaps?

Hopefully this is doable?

Thanks

A: 

Not sure how to limit it at design time -- I've seen compile-time and run-time checking.

However, you may want to consider simplifying your enumerations by combining Orientation and TextSide.

For instance, System.Windows.Forms.TabControl has Alignment property (TabAlignment enum) which specifies Top, Bottom, Left and Right. Implicit in this Horizontal/Vertical.

By doing this, you simplify the interface and remove the possibility for error and invalid combinations.

micahtan
A: 

The trouble is with that is that this will be taken futher and include something like text direction where you can have LeftToRight or RightToLeft either of which are acceptable for a horizontal control but not for a vertical control where you may want TopToBottom or BottomToTop!!

Is the only way to add properties to a control via a getter/setter type approach?

Is there really no way of dynamically changing properties which are dependent upon others? This seems to be a big shortfall.

A: 

I know this probably seems a bit clumsy but what about not using Left/Right or Up/Down but just Position1/Position2? For example in a horizontal setup Position1 would be Left (at least when thinking about LeftToRight) and in vertical setup Position2 would be Top (assuming TopToBottom).

You could then also forget about TopToBottom/BottomToTop or LeftToRight/RightToLeft but just use a boolean called something like IsReverseOrder.

chrischu