Winform inheritance and default form size

The application in which I work will have several forms with a lot of common functions. For example, each form will have a DataGridView , many of the same buttons, most of the same user interface code, etc.

I would like to realize this by creating a basic version of this general form, a subclass for all of these very similar, but not exactly the same child forms, and I need to adhere to any additional controls and functions for each of them.

I already found out that this helps to make the controls of the database protected , because it allows you to do things like bind to work. However, I have yet to find a way to automatically make derived forms the same size as the base form.

Experience tells me that there should be an easy way to do this. Although this is not a big problem, just enter the required size manually for each derivative form immediately after its creation, I would prefer to make everything as simple, simple and automatic as possible.

+4
source share
3 answers

I'm curious that your derived forms do not automatically inherit size from their base form, because this should work without having to do anything with it.

The alleged cause of your problem:

I suspect that your problem is due to the fact that you are using Visual Studio Forms Designer to edit forms. Whenever you edit a form, Windows Forms Designer generates the required code in the InitializeComponent method of your forms. Among all the generated code are destinations that specify the size of the form, even if it is identical to the size of the base form. Therefore, you may need to manually comment on these assignments if you want your derived form to be the same size as the base form, even if you resized the base form after creating the derived forms. (However, I do not know if this could lead to further problems with the positioning and layout of the controls.)

 // Code to be commented out in your derived form InitializeComponent method: this.AutoScaleDimensions = new System.Drawing.SizeF(...); this.ClientSize = new System.Drawing.Size(...); 

After these lines are commented out, the size specified in your basic InitializeComponent form will be used for the derived form.

Workaround:

You can do the following so that every time you edit the form, you do not have to manually comment on the code generated by the generated design:

Create a form derived from your base form; call him FrozenBaseForm . You will receive all other forms from this class, and not directly from the base form. Now in this "intermediate" class you define a new ClientSize property:

 public class FrozenBaseForm : BaseForm { new public SizeF ClientSize { get { return base.ClientSize; } set { } } } 

This will cause all ClientSize assignments to have no effect and therefore save the size from the base form. It seems to crack the truth, but it seems to work. You may need to hide the Size property in the same way as btw.

As said, derive your forms from FrozenBaseForm , and not from BaseForm directly:

 public class DerivedForm1 : FrozenBaseForm { ... } public class DerivedForm2 : FrozenBaseForm { ... } ... 

Another option (the latter, if all else fails):

As a last resort, you can simply forget about Forms Designer and simply define derived forms manually in the code editor (although I personally would not want to do this):

 public class DerivedForm : BaseForm { public DerivedForm() { // make all necessary changes to the base form: ... } } 
+3
source
 public partial class derivedForm : baseForm { public derivedForm() { InitializeComponent(); this.Width = base.Width; this.Height = base.Height; } } 
0
source

Why not make BaseForm set to fit?

 public partial class BaseForm : Form { public BaseForm() { InitializeComponent(); // you could hardcode these or retrieve these values from a // config file or something this.Width = 640; this.Height = 468; } } 

Wouldn't that do what you want?

0
source

Source: https://habr.com/ru/post/1301112/


All Articles