views:

43

answers:

3

I have a control for which I need to enforce a certain Width or Height, depending on a property called Orientation, which, obviously can be Vertical or Horizontal.

So if the Orientation property is Vertical, then the Width must always be 2.
If the Orientation property is Horizontal, then the Height must always be 2.

I have tried many things including MaximumSize, but none of them seem to work well enough.
As an example... A single-line TextBox (With Multiline set to False.)?

+1  A: 

These are the steps:

  1. Implement a designer for the control by creating an empty class that inherits from ControlDesigner.
  2. In this designer class, override the SelectionRules property and return whatever rules you want to use for your control; for example, set left- and right-sizeable only if you want to forbid vertical resizing.
  3. Attach the designer to your control by decorating your control class with the DesignerAttribute, and referencing your designer class in the constructor of the attribute.
CesarGon
Thank you for the answer. I will test this right away!
Vercas
Thank you so much again, this also answers another question I was going to ask about enforcing the location! It works perfectly!
Vercas
You're welcome!
CesarGon
A: 

Is this a custom user control? If so, you can build this logic into the Orientation property.

If it is a custom control, then you could write the Orientation property such that the set method checks value (which would be either Vertical or Horizontal) and then quietly sets Width/Height accordingly. In that same set method you could also update two bool fields, like dontLetUserSetWidth and dontLetUserSetHeight, depending on value.

Does this look helpful? I'm sorry if I didn't read your question right!

Claire
No, I would have mentioned user control instead of control. I COULD build the logic there but then the user would be able to resize the control after setting the property.
Vercas
A: 

As an example... A single-line TextBox (With Multiline set to False.)

Considering this specific example, with multiline set to false, you will have to explicitly handle the Multiline Mode when the Vertical figure changes.

KMan
I do not get it... What do you mean?
Vercas