It doesn't seem like resizable forms are inheritable
-
I've recently experimented with base and derived forms, and though it works fine with non-resizable forms, there are problems with resizable forms. As you know, with resizable forms, you use anchoring and docking to keep the controls in place and let the form resize gracefully. But when you inherit from a form that has anchored or docked controls, the inherited form does not display correctly in the Visual Studio Form Designer. It appears to compile and display correctly while running, but that too could break. Obviously, in an inherited form, both constructors execute, the base form's and the derived form's. It seems the problem is that one form's constructor sets measurements while the layout is suspended in the other form's constructor. I read about this in Roland Weigelt's blog Roland Weigelt - Visual Inheritance - How to escape Layout Hell[^] He explains that
Quote:
In the moment the constructor of the base class is left, the layout and the controls are perfect for a Form1 instance - but that's not what we want. The method InitializeComponent() of class Form2 contains the code that is necessary to achieve the desired layout by tweaking the base form and adding the second button. While setting the Text property to "Form2" is rather trivial, setting the ClientSize property causes serious trouble. The problem is that the size is set while the layout logic is suspended; i.e. the size of the form is changed, but the anchored elements of the base class are not updated.
And in Roland Weigelt - Visual Inheritance Revisited[^]
Quote:
Question: So why does docking work, but not anchoring? Docking tells a control to grab as much space as possible (for top/bottom/left/right: towards to the specified direction) until a border of the container is reached. When the size of the control's container changes, the border is moving, so the docked control will resize itself accordingly. Anchoring tells a control to keep a constant distance between a specified border (top/bottom/left/right) of the control and the corresponding border of the container at all times. Now if the container is resized while l
-
I've recently experimented with base and derived forms, and though it works fine with non-resizable forms, there are problems with resizable forms. As you know, with resizable forms, you use anchoring and docking to keep the controls in place and let the form resize gracefully. But when you inherit from a form that has anchored or docked controls, the inherited form does not display correctly in the Visual Studio Form Designer. It appears to compile and display correctly while running, but that too could break. Obviously, in an inherited form, both constructors execute, the base form's and the derived form's. It seems the problem is that one form's constructor sets measurements while the layout is suspended in the other form's constructor. I read about this in Roland Weigelt's blog Roland Weigelt - Visual Inheritance - How to escape Layout Hell[^] He explains that
Quote:
In the moment the constructor of the base class is left, the layout and the controls are perfect for a Form1 instance - but that's not what we want. The method InitializeComponent() of class Form2 contains the code that is necessary to achieve the desired layout by tweaking the base form and adding the second button. While setting the Text property to "Form2" is rather trivial, setting the ClientSize property causes serious trouble. The problem is that the size is set while the layout logic is suspended; i.e. the size of the form is changed, but the anchored elements of the base class are not updated.
And in Roland Weigelt - Visual Inheritance Revisited[^]
Quote:
Question: So why does docking work, but not anchoring? Docking tells a control to grab as much space as possible (for top/bottom/left/right: towards to the specified direction) until a border of the container is reached. When the size of the control's container changes, the border is moving, so the docked control will resize itself accordingly. Anchoring tells a control to keep a constant distance between a specified border (top/bottom/left/right) of the control and the corresponding border of the container at all times. Now if the container is resized while l
So, WinForms. Might helpt a bit if you include that info.
RobertSF wrote:
He advises to use docked panels, but I found this not to work, probably because WinForms has changed since 2003, when he wrote that article.
It didn't, but you found me in a non explaining mood. Did you really think that WinForms would change after these years?
RobertSF wrote:
Do you use form inheritance, and if so, how do you deal with this?
Yes. And elegant.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
So, WinForms. Might helpt a bit if you include that info.
RobertSF wrote:
He advises to use docked panels, but I found this not to work, probably because WinForms has changed since 2003, when he wrote that article.
It didn't, but you found me in a non explaining mood. Did you really think that WinForms would change after these years?
RobertSF wrote:
Do you use form inheritance, and if so, how do you deal with this?
Yes. And elegant.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
I've recently experimented with base and derived forms, and though it works fine with non-resizable forms, there are problems with resizable forms. As you know, with resizable forms, you use anchoring and docking to keep the controls in place and let the form resize gracefully. But when you inherit from a form that has anchored or docked controls, the inherited form does not display correctly in the Visual Studio Form Designer. It appears to compile and display correctly while running, but that too could break. Obviously, in an inherited form, both constructors execute, the base form's and the derived form's. It seems the problem is that one form's constructor sets measurements while the layout is suspended in the other form's constructor. I read about this in Roland Weigelt's blog Roland Weigelt - Visual Inheritance - How to escape Layout Hell[^] He explains that
Quote:
In the moment the constructor of the base class is left, the layout and the controls are perfect for a Form1 instance - but that's not what we want. The method InitializeComponent() of class Form2 contains the code that is necessary to achieve the desired layout by tweaking the base form and adding the second button. While setting the Text property to "Form2" is rather trivial, setting the ClientSize property causes serious trouble. The problem is that the size is set while the layout logic is suspended; i.e. the size of the form is changed, but the anchored elements of the base class are not updated.
And in Roland Weigelt - Visual Inheritance Revisited[^]
Quote:
Question: So why does docking work, but not anchoring? Docking tells a control to grab as much space as possible (for top/bottom/left/right: towards to the specified direction) until a border of the container is reached. When the size of the control's container changes, the border is moving, so the docked control will resize itself accordingly. Anchoring tells a control to keep a constant distance between a specified border (top/bottom/left/right) of the control and the corresponding border of the container at all times. Now if the container is resized while l
I inherit from resizable WinForms quite often (mostly to provide a common "look and feel" plus I use large image buttons and fonts that all forms need - and it works well:
public partial class FormBase : Form {
...
}public partial class FrmMain : FormBase {
...
}The "standard" buttons are set in FrmBase, and all derived forms include them. The only PITA for me is that the base form cannot be
abstract
because the designer can't cope unless you add a dummy concrete class "in the middle" for design mode only (which I hate because it means I'm not testing the same code I'll use in production). I've never had a problem with Anchoring or Docking - what exactly is happening to your derived form when you resize it?"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I inherit from resizable WinForms quite often (mostly to provide a common "look and feel" plus I use large image buttons and fonts that all forms need - and it works well:
public partial class FormBase : Form {
...
}public partial class FrmMain : FormBase {
...
}The "standard" buttons are set in FrmBase, and all derived forms include them. The only PITA for me is that the base form cannot be
abstract
because the designer can't cope unless you add a dummy concrete class "in the middle" for design mode only (which I hate because it means I'm not testing the same code I'll use in production). I've never had a problem with Anchoring or Docking - what exactly is happening to your derived form when you resize it?"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
OriginalGriff wrote:
I've never had a problem with Anchoring or Docking - what exactly is happening to your derived form when you resize it?
The problem happens as soon as I derive a form from a base form. The derived form appears in the visual designer like this https://i.ibb.co/5sDNBRP/Untitled.png[^] The derived form only shows the control box. I expand the form to its proper default size, and everything looks good. When I reopen that form in the designer, however, the controls are all jumbled and out of place. Curiously, if I compile and open the form during execution, it appears fine. The blog I linked in my first post describes the problem and its causes. Thanks for the reality check. I'll upgrade my VS 2017 to the latest, and maybe it's time to move to WPF and drop WinForms. :)
-
OriginalGriff wrote:
I've never had a problem with Anchoring or Docking - what exactly is happening to your derived form when you resize it?
The problem happens as soon as I derive a form from a base form. The derived form appears in the visual designer like this https://i.ibb.co/5sDNBRP/Untitled.png[^] The derived form only shows the control box. I expand the form to its proper default size, and everything looks good. When I reopen that form in the designer, however, the controls are all jumbled and out of place. Curiously, if I compile and open the form during execution, it appears fine. The blog I linked in my first post describes the problem and its causes. Thanks for the reality check. I'll upgrade my VS 2017 to the latest, and maybe it's time to move to WPF and drop WinForms. :)
Have you tried expanding the size?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Have you tried expanding the size?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Yes, and when expanded, the form looks fine, but after saving and reopening, then the controls are jumbled and out of place. However, I'll have to figure out what I did different because now the form reopens just fine. Thanks for your suggestions. Maybe it did solve the issue to use docked panels instead of putting the controls directly in the form. Is that a technique you use? If you have a chance, give the articles I linked a look. They're interesting, and the writer seems to know his stuff.
-
Yes, and when expanded, the form looks fine, but after saving and reopening, then the controls are jumbled and out of place. However, I'll have to figure out what I did different because now the form reopens just fine. Thanks for your suggestions. Maybe it did solve the issue to use docked panels instead of putting the controls directly in the form. Is that a technique you use? If you have a chance, give the articles I linked a look. They're interesting, and the writer seems to know his stuff.
Mostly, yes - though I generally create a UserControl if it's going to fill an area like a toolbar does, or provide a status bar. Those work fine docked to the top / bottom, then I'll use a central panel to drop other controls on.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Mostly, yes - though I generally create a UserControl if it's going to fill an area like a toolbar does, or provide a status bar. Those work fine docked to the top / bottom, then I'll use a central panel to drop other controls on.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Thanks. I think that was the issue. I must have used anchoring instead of docking. I feel a bit like Howie Mandel here. :laugh: David & Leeman: Howie Mandel Can't Read When Magicians Squeeze His Skull - America's Got Talent 2014 - YouTube[^]
It can get confusing sometimes - easy mistake to make!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!