Control Properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maurelius
    New Member
    • Jun 2009
    • 5

    Control Properties

    Hello all,
    Can you dynamically change the property options for a control in the property grid of VisualStudio?

    What I want is to be able to change some of the property options which are dependent upon others. For example:
    If my control orientation was horizontal then I would want the text direction options to be "left" or "right" but if the orientation wsa vertical then I would want the text direction options to be "up" and "down". It does go deeper than that as there likely to be at least three controls dependent upon each other...

    Currently I have some getter/setter methods which expose my custom properties in the property grid but I cannot see how you can change the options dependent upon another getter/setter method.

    Clearly I need this to work at design time, which the current method does after the source is built.

    Can anoyone help?

    Thanks
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Override the OnPaint and put some of the logic in there.
    This has to run even at design time in order for the form to be painted so you can work on it.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Just register appropriate event handlers to your controls.

      Comment

      • maurelius
        New Member
        • Jun 2009
        • 5

        #4
        Thanks for the reply but it is the logic that is escaping me at the moment :(

        This is what I have so far; it should be quite obvious to someone with brains what I am doing wrong!!

        Code:
        public partial class CustomControl1 : Control
            {
                public enum HorizontalDirection
                {
                    Left,
                    Right,
                }
        
                public enum VerticalDirection
                {
                    Up,
                    Down,
                }
        
                public enum ControlOrientation
                {
                    Horizontal,
                    Vertical
                }
                
                private Enum _direction;
                private ControlOrientation _orientation;
        
                public Enum TextDirection
                {
                    get
                    {
                        if (_orientation == ControlOrientation.Horizontal)
                        {
                            return _direction; //[need to return HorizontalDirection];
                        }
                        else
                        {
                            return _direction; //[need to return VerticalDirection];
                        }
                    }
                    set
                    {
                        _direction = value; 
                    }
                }
        
                public ControlOrientation Orientation
                {
                    get
                    {
                        return _orientation;
                    }
                    set
                    {
                        _orientation = value;
                    }
                }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You don't really need to extend (inherit from) the Control class to do this.
          Just add event handlers to instances of your controls.
          Here is a basic example of attaching an event handler to a button's onclick action.
          The trick for you will be attaching the delegates to the appropriate event for each control.

          Comment

          • maurelius
            New Member
            • Jun 2009
            • 5

            #6
            Originally posted by r035198x
            You don't really need to extend (inherit from) the Control class to do this.
            Just add event handlers to instances of your controls.
            Here is a basic example of attaching an event handler to a button's onclick action.
            The trick for you will be attaching the delegates to the appropriate event for each control.
            Agreed, attaching to the correct event is tricky...not sure where to start!!

            Even if I did manage to figure it out, I still need to somehow return the correct enum from the method the event handler calls to give the correct options, wouldn't I??

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              The good news is you don't have to guess. All the controls have a lot of documentation at MSDN (and indeed probably available on your IDE) that explains which event listeners you can attach handlers on. The handlers (delegate methods) would then change the look of the controls as you wish. The method handler methods themselves would take parameters about the control that generated the event so you can decide how to change your controls by analyzing the source of the events from the parameters.

              Comment

              • maurelius
                New Member
                • Jun 2009
                • 5

                #8
                Originally posted by r035198x
                All the controls have a lot of documentation at MSDN (and indeed probably available on your IDE) that explains which event listeners you can attach handlers on.
                This is a custom control created by me and MSDN will not have any information on it!!

                Cannot find any sound examples so i'll approach this another way...

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Even if it's custom control, isn't it that the changes you want to listen for are available in the Control class(es) that you inherited from?
                  Whatever approach you decide to take I feel that it should ultimately involve attaching appropriate delegates to handle property changes on your controls.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Thanks for the reply but it is the logic that is escaping me at the moment :(
                    The logic for which part...?
                    How to tell if your control is horizontal or vertical?
                    Code:
                    if (this.width > this.height) 
                    {
                       this.orientation = Control.Orientation.Horizontal;
                    }
                    Otherwise, please me more specific about where you are lost.

                    Comment

                    • maurelius
                      New Member
                      • Jun 2009
                      • 5

                      #11
                      Originally posted by tlhintoq
                      The logic for which part...?
                      How to tell if your control is horizontal or vertical?
                      Code:
                      if (this.width > this.height) 
                      {
                         this.orientation = Control.Orientation.Horizontal;
                      }
                      Otherwise, please me more specific about where you are lost.
                      No, :) not the orientation bit. I meant the event handlers but; I think that is solved now though.

                      The newest bit that has me is how you can dynamically change the options for a control property ie for the direction property how can you change it from having a choice of left and right to have a choice of up and down?

                      thanks for your reply

                      Comment

                      Working...