Winform继承和默认窗体大小

发布于 2024-08-21 11:31:36 字数 330 浏览 2 评论 0原文

我正在使用的应用程序将具有许多具有许多共享功能的表单。例如,每个表单都有一个 DataGridView、许多相同的按钮、许多相同的 UI 代码等等。

我想通过创建这个通用表单的基本版本来实现这一点,将其子类化为所有这些非常相似但不完全相同的子表单,并为每个表单添加我需要的任何附加控件和功能其中。

我已经发现它有助于使基本表单的控件受保护,因为这允许诸如锚定之类的功能正常工作。但是,我还没有找到一种方法来自动使派生表单的大小与基本表单的大小相同。

经验告诉我应该有一个简单的方法来做到这一点。虽然在创建每个派生表单后立即手动输入所需的大小并不是什么大问题,但我更愿意让一切尽可能干净、简单和自动。

An application I'm working will have a number of forms with a lot of shared functionality. For instance, each form will have a DataGridView, many of the same buttons, much of the same UI code and so on.

I'd like to implement this by creating a base version of this common form, subclass it for all these very-similar-but-not-quite-the-same child forms, and tack on whatever additional controls and features I need for each of them.

I've already figured out that it helps to make the base form's controls protected because this allows things like anchoring to work propertly. However, I have yet to find a way to automatically make the derived forms the same size as the base form.

Experience tells me there should be a simple way to do this. While it's not much of a problem to just type in the required size by hand for every derived form right after creating it, I'd prefer to make everything as clean, simple, and automatic as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-08-28 11:31:37
public partial class derivedForm : baseForm
{
    public derivedForm()
    {
        InitializeComponent();

        this.Width = base.Width;
        this.Height = base.Height;
    }
}
public partial class derivedForm : baseForm
{
    public derivedForm()
    {
        InitializeComponent();

        this.Width = base.Width;
        this.Height = base.Height;
    }
}
半暖夏伤 2024-08-28 11:31:37

为什么不让 BaseForm 设置自己的大小?

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;
    }
}

这不就是你想要的吗?

Why not make the BaseForm set the size of itself?

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 this do what you want?

靑春怀旧 2024-08-28 11:31:36

我发现有趣的是,您的派生形式不会自动继承其基本形式的大小,因为这应该可以工作,而无需您对此进行任何操作。

疑似问题原因:

我怀疑您的问题是由于您使用 Visual Studio 的表单设计器编辑表单而导致的。每当您编辑表单时,Windows 窗体设计器都会在表单的 InitializeComponent 方法中生成所需的代码。在所有生成的代码中,有一些设置表单大小的赋值,即使它与基本表单的大小相同。因此,如果您希望派生表单具有与基本表单相同的大小,则可能必须手动注释掉这些分配,即使您在创建派生表单后更改了基本表单的大小也是如此。 (但是,我不知道这是否会导致控件的定位和布局出现进一步的问题。)

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

注释掉这些行后,基本表单的 InitializeComponent 中设置的大小将是用于派生形式。

变通解决方案

您可以执行以下操作,这样就不必在每次编辑表单时手动注释掉设计者生成的代码:

创建从基本表单派生的表单;我们称之为FrozenBaseForm。您将从此类派生所有其他形式,而不是直接从基本形式派生。现在,在这个“中间”类中,您定义了一个新属性 ClientSize

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

这将导致对 ClientSize 的所有分配根本不起作用,因此保留了基础形式。说实话,这感觉像是黑客攻击,但似乎确实有效。顺便说一句,您可能必须以相同的方式隐藏 Size 属性。

如前所述,从 FrozenBaseForm 派生表单,而不是直接从 BaseForm 派生:

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

另一种选择(如果所有其他方法都失败的最后手段):

作为最后手段,您可以简单地忘记表单设计器,而只需在代码编辑器中手动定义派生表单(尽管我个人不想这样做):

public class DerivedForm : BaseForm
{
    public DerivedForm()
    {
        // make all necessary changes to the base form:
        ...
    }
}

I find it interesting that your derived forms do not automatically inherit the size from their base form, because this should work without you having to do anything about it.

Suspected cause of your problem:

I suspect your problem results from the fact that you're using Visual Studio's Forms Designer to edit the forms. Whenever you've edited a form, Windows Forms Designer generates the required code in the InitializeComponent method of your forms. Among all the generated code are assignments that set a form's size, even if it is identical to the base form's size. Therefore you might have to manually comment out those assignments if you want your derived form to have the same size as the base form, even when you change the base form's size after creating the derived forms. (However, I don't know if that might lead to further problems with the controls' positioning & layouting.)

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

Once these lines are commented out, the size as set in your base form's InitializeComponent will be used for the derived form.

A workaround solution:

You can do the following so that you don't have to manually comment-out designer-generated code every time you've edited a form:

Create an form derived from your base form; let's call it FrozenBaseForm. You will derive all other forms from this class instead of directly from the base form. Now, in this "intermediate" class, you define a new property ClientSize:

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

This will cause all assignments to ClientSize to have no effect at all and therefore preserve the size from the base form. This feels like a hack to tell the truth, but it seems to work. You might have to hide the Size property in the same way btw.

As said, derive your forms from FrozenBaseForm instead of from BaseForm directly:

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

Another option (last resort if all else fails):

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

public class DerivedForm : BaseForm
{
    public DerivedForm()
    {
        // make all necessary changes to the base form:
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文