Fixed Height

Phylum

Centurion
Joined
Jun 20, 2003
Messages
105
Location
Canada
I have created a control designer to restrict the programmer using my control from altering the controls height. Here is the code:

Code:
internal class FixedHeightControlDesigner : System.Windows.Forms.Design.ControlDesigner
   {

      public override SelectionRules SelectionRules
      {

         get
         {
            return SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable | 
               SelectionRules.Visible;
         }

      }

   }

Then you just declare your control with the Designer attribute, as follows:

Code:
   [Designer(typeof(FixedHeightControlDesigner))]
   public class ControlClassName : System.Windows.Forms.UserControl

The problem I have is my usercontrol is made up of a button and a textbox, and like the windows textbox control I want to make the fixedheight conditional upon wether or not the multiline property is set to TRUE. The Control Designer dosen't contain a reference to the control and therefore can't read the property. Does anybody know how to do this?
 
This is just me thinking...

Why don't you simply handle the resize event and block (or not) the control height change?

Alex :p
 
AlexCode said:
This is just me thinking...

Why don't you simply handle the resize event and block (or not) the control height change?

Alex :p

It dosen't look as professional. With the way i did it the grab handle son the control are greyed out indicating that you can't resize it vertically. I may have to resort with what you said... :(
 
The fastest way I can see is to you do declare a static variable, that both control and designer can access, that will hold the selections rules.
Set this variable on the set of the Multiline Property, one selection rule for True, another for False.
Force the redraw of the control...

Tho I didn't tryed this, I believe it should work...

Alex :p
 
AlexCode said:
The fastest way I can see is to you do declare a static variable, that both control and designer can access, that will hold the selections rules.
Set this variable on the set of the Multiline Property, one selection rule for True, another for False.
This variable would also have to be declared publicly, allowing other programmers to access it and cause potential problems. Also, This might work of you have only a single instance of the control. If you have many, that would have to turn into a static array or collection that would have to be indexable by the reference of the controls.

The designer does have a reference to the control it is designing... the ControlDesigner.Control property.


Visual Basic:
internal class FixedHeightControlDesigner : System.Windows.Forms.Design.ControlDesigner
   {

      public override SelectionRules SelectionRules
      {

         get
         {
            MyControl control = (MyControl) this.Control;

            if (Control.Multiline)
                return SelectionRules.Moveable | SelectionRules.LeftSizeable |
                        SelectionRules.RightSizeable | SelectionRules.Visible;
            else
                return SelectionRules.Moveable | SelectionRules.LeftSizeable |
                        SelectionRules.TopSizeable | SelectionRules.BottomSizeable |
                        SelectionRules.RightSizeable | SelectionRules.Visible;
          }

      }

   }

I got this to work fine in VB, but I don't know much C#, so I don't know if the syntax is right. Also, this won't prevent the programmer from changing the control size at run time. You might want to implement some size checking in the resize event.

[Edit]Fixed a typo in the code[/Edit]
 
Last edited:
Back
Top