Results 1 to 8 of 8

Thread: [RESOLVED] Winforms Visual Inheritance. Inherited control anchoring/positioning not working.

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Resolved [RESOLVED] Winforms Visual Inheritance. Inherited control anchoring/positioning not working.

    Hi All

    I had this problem years ago and was able to solve it but can't, for the life of me, remember how. Pretty sure it was just a property I needed to set somewhere.

    I'm adding wizards to an analysis software. The wizards will walk through a series of forms, all of which will share some common behaviour including Previous, Next and Cancel buttons. It seems a no brainer to have frmWizard form with those buttons on it and then inherited other forms from it.

    Here's my frmWizard:-
    Name:  frmWizard.PNG
Views: 222
Size:  4.6 KB
    Note, the buttons are anchored to bottom right.

    I inherit from frmWizard creating a larger form and this happens:-
    Name:  frmModel.PNG
Views: 261
Size:  6.4 KB

    If I resize the form I can see that the buttons are actually still anchored bottom right but they seem to be taking their initial position from the top left of the form.

    Any idea how I fix this?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    I admit I don't fully understand the issue.
    Are you saying that the buttons will stay on top even if you resize the second inherited form on the designer?
    If that is the issue then try to open close the design form and start debugging the project.
    Eventually VS will "wake up" and fix the issue other than that I fail to see the problem.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    How exactly did you add the inherited form? I just added a form to a project, made the form smaller, added a Button and anchored it to the bottom, right corner. I then tried to add an inherited form to the same project and it didn't show me the existing form to use as a base. It would only let me browse for an external library. Instead, I just added a regular form and then went into the designer code file and changed the base class. After doing that, I went back to the designer and the Button was displayed and it was in the location that you would want, i.e. the same as the base form relative to the bottom, right corner. Maybe you should try doing that instead of using the Inherited Form item template, assuming that that is what you're doing.

  4. #4

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    The way I'm inheriting is simply to change ": Forms" to ": frmWizard" in the code behind.


    Are you saying that the buttons will stay on top even if you resize the second inherited form on the designer?
    Not quite. Once it's been created it anchors to the bottom right, so resizing the form causes the buttons to move according to the anchoring. This is true at both Design and Runtime.

    However, at the point of actually inheriting (changing the code behind as above) the buttons are placed in the top left of the form, regardless of the size of the form.

    Instead, I just added a regular form and then went into the designer code file and changed the base class.
    I think you're describing what I'm doing, changing the : Form to : myBaseForm - have I read you correct? If so, so see what I'm describing you should be able to do the following:-
    1. Create the base form with buttons anchored bottom right.
    2. Create a normal form but make it substantially bigger than the base form.
    3. Go into the code file and change it to inherit from the base form rather than "Form".
    4. Go to the designer, you should see that the buttons are taking their initial position from the base form's top left.

    Actually, this does make sense. The initial position is taken from the Location property and that has no concept of Bottom Right - it's always Top Left. it's using that at the point of inheriting and then applying the anchoring on any resizing that's done after that.

    Incidentally, I have just found a horribly hacky and frustrating way around this. I can size the derived form the same as the base form, then make it inherit from the base form, I can the resize the form as I see fit and the buttons remain in the correct position relative to the bottom right. This is frustrating though and a bit error prone.

    Edit> Gah! Scratch that hacky solution. Rebuilding the project repositions the buttons at the top left so that doesn't work after all.
    Last edited by FunkyDexter; Aug 3rd, 2021 at 03:19 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    It sounds like you are doing exactly what I did but I'm seeing the behaviour you want, rather than the behaviour you say that you're seeing. I thought the same as you, i.e. that the Location properties would be the same, but it seemed that, when I did it, the system was smart enough to know that, based on the Anchor value, the Location should be set relative to the bottom, right instead of the top, left.

    One thing I did different was that I only had one Button, rather than three. Not sure whether that would make a difference.

  6. #6

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    From the darkest depths of my age addled memory the solution bubbled to the surface.

    The buttons have a "Modifiers" property that sits under the Design category. This defaults to Private. If you change it to Public, the frmDerived.Designer.cs file get's it's own set of values for the buttons location settings, meaning it can retain those settings between derived instances instead of re-inheriting them from the base form each time. I expected Protected to work but it doesn't seem to, don't know why. Public is giving me the behaviour I want - pending some testing.

    JM, I'm curious, what is the modifier setting on your button?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    That makes sense. I didn't even notice that this was in the C# forum and I created a VB project. Controls in VB projects are Public by default.

  8. #8

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Winforms Visual Inheritance. Inherited control anchoring/positioning not working

    That tracks then. Thanks for the help guys
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

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



Click Here to Expand Forum to Full Width