VS 2010 [RESOLVED] Inherited form and button position-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] Inherited form and button position

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Resolved [RESOLVED] Inherited form and button position

    Hello.

    I have noticed something odd when i use inherited forms, and while i realize this is probably normal, i'd like to know if there is a way around this behavour.

    To replicate, create a new form (Form1), and add a button in the bottom right corner. Anchour the button to the bottom and right sides (remove the anchours for the top and left).

    Save and build the solution. Now add a new inherited form (Form2), and be sure to inherit it from Form1.

    With Form2 in front of you in the designer, grab the bottom right corner of the form, and stretch the form down and to the right. The button does not move. It doesn't stay anchoured to the bottom right corner.

    Anyone have any ideas on how i could overcome this??
    .
    ~Peter


  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Inherited form and button position

    The obvious answer would be to use a different way of doing whatever you're using inherited forms for, and not inherit forms.

    I've had nothing but trouble with inherited forms, frankly.

    I would suggest defining User Controls that can be composed quickly and easily on a form as a superior technique for all the scenarios where I've seen inherited forms used.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Inherited form and button position

    It's a known bug (since... 2005 or something, maybe even earlier) that will probably never get fixed. I've rarely used inherited forms, but in some cases they can be really useful. In my case I inherited from a dialog-like form, where there's two buttons in the bottom right corner. On all of the inheriting forms, the buttons were beyond the boundaries of the form and thus invisible. In that case I just set the button positions manually via code. You won't see the buttons during design-time, but during run-time they move to the correct spot.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,579

    Re: Inherited form and button position

    I just tried it in VS 2010 Professional and it worked for me. I did have a related issue though. Each time I built the project, the Location of the Button on the inherited form was changed to the same value as on the base form, which actually makes sense when you think about it. It's not convenient, but it's logical.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Inherited form and button position

    Quote Originally Posted by jmcilhinney View Post
    I just tried it in VS 2010 Professional and it worked for me. I did have a related issue though. Each time I built the project, the Location of the Button on the inherited form was changed to the same value as on the base form, which actually makes sense when you think about it. It's not convenient, but it's logical.
    Yes, that's the same problem I was having. The base form was larger than the inherited forms, so the buttons got moved beyond the boundary of the inherited form. I can see how it might be logical in terms of how inheritance works, but it's definitely not logical for an inherit form to behave like this; it makes them quite hard to use...

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,779

    Re: Inherited form and button position

    Quote Originally Posted by NickThissen View Post
    In that case I just set the button positions manually via code. You won't see the buttons during design-time, but during run-time they move to the correct spot.
    I have a 4-5 layer inherited form that does this exact thing. The base adds dialog buttons that are only created and positioned at run-time. Trying to do it any other way just gives you frustration! My layers only give access to properties, or simple panels that the inherited forms put their controls into. In most cases an interface would be better then inherited forms, but in some cases its just needed.

    One simple solution is to use a user control with all your form design, that is added at runtime to a form that has all the common stuff you need, ie dialog buttons, and use an interface with events to ensure that its kept nice and neat.

  7. #7

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post Re: Inherited form and button position

    Quote Originally Posted by Evil_Giraffe View Post
    The obvious answer would be to use a different way of doing whatever you're using inherited forms for, and not inherit forms.
    Yeah,... i totally get where you're coming from, but in this case i have complete forms that are being re-used.

    So i have a "base" form that contains a title, image, email button, save button, close button, etc. As well, the base form contains all the code to operate all those controls - for instance the email and close buttons work exactly the same regardless of what else is on the form.

    And of course, all of the forms that inherit from it will already have the controls and code in place, meaning i can keep the same look across all the forms. And i don't have to recode the same thing on 8 different forms.
    ~Peter


  8. #8

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up Re: Inherited form and button position

    Quote Originally Posted by NickThissen View Post
    In that case I just set the button positions manually via code. You won't see the buttons during design-time, but during run-time they move to the correct spot.
    This seemed like a reasonable work-around for this annoying issue. So i went ahead and coded up a little something to move the OK and Close buttons in the Form.Load like you suggested:

    Code:
            Const BUTTON_PADDING_BOTTOM_OR_RIGHT As Integer = 11
            Const BUTTON_PADDING_BETWEEN As Integer = 6
            Const FORM_PADDING_TOP_BOTTOM As Integer = 35
            Const FORM_PADDNG_LEFT_RIGHT As Integer = 9
    
            'Move both buttons to the bottom of the form
            Dim iNewTop As Integer = Me.Height - FORM_PADDING_TOP_BOTTOM - btnOK.Height - BUTTON_PADDING_BOTTOM_OR_RIGHT
            btnClose.Top = iNewTop
            btnOK.Top = iNewTop
    
            'Move first button into bottom right corner of form
            btnClose.Left = Me.Width - FORM_PADDNG_LEFT_RIGHT - btnClose.Width - BUTTON_PADDING_BOTTOM_OR_RIGHT
            
            'Move second button into bottom right of form, beside first button
            btnOK.Left = btnClose.Left - btnOK.Width - BUTTON_PADDING_BETWEEN
    It's not perfect, but it works. Thanks!
    .
    ~Peter


  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Inherited form and button position

    Another option (beside setting the control locations in code) is to just place the code of those buttons in methods (protected or public). Then remove the buttons themselves from the base form. Put the buttons in the inheriting forms instead, and just call the methods with the code they're supposed to run.

    Depending on the complexity of your form this might be less work then setting the button locations by code. Of course (again depending of the exact layout) you might be able to just place the buttons in a Panel or some other container control and set the location of that instead. Then you just have to set the location of one (or maybe a few) containers, instead of 38 buttons

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.