Windows窗体继承 [英] Windows Form inheritance

查看:273
本文介绍了Windows窗体继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一堆,所有具有相同的属性形式和通过指定构造函数的参数初始化的形式构造的属性。

I want to create a bunch of forms that all have the same properties and initialize the properties in the forms constructor by assigning the constructor's parameters.

我试图创造一个类,从继承的形式,然后让我所有的形式从类继承,但我觉得,因为我不能调用的InitializeComponent(),我是有一些问题。

I tried creating a class that inherits from form and then having all of my forms inherit from that class, but I think since I couldn't call InitializeComponent(), that I was having some problems.

什么是如何做到这一点的一些C#代码?

What is some C# code on how to do this?

推荐答案

父的的InitializeComponent

The parent's InitializeComponent should be called by having your constructor call base() like this:

public YourFormName() : base()
{
    // ...
}

(你的父窗体应该有的InitializeComponent 调用在其构造,你没拿说出来,是吗?)

(Your parent Form should have a call to InitializeComponent in its constructor. You didn't take that out, did you?)

不过,你要下山的路是不是一个会与设计师发挥很好,因为你不会是能够得到它的实例表单在设计时这些参数(你必须提供一个参数的构造函数,它的工作)。您还可以碰到的地方分配父属性第二次,或者为它们分配是从什么,如果你使用代码的参数化的构造函数你可能想要不同的问题。

However, the road you're going down isn't one that will play nicely with the designer, as you aren't going to be able to get it to instantiate your form at design time with those parameters (you'll have to provide a parameterless constructor for it to work). You'll also run into issues where it assigns parent properties for a second time, or assigns them to be different from what you might have wanted if you use your parametered constructor in code.

只具有形式的性质,而不是使用构造带参数的坚持。对于表单,你就会有自己头疼的问题。

Stick with just having the properties on the form rather than using a constructor with parameters. For Forms, you'll have yourself a headache.

这篇关于Windows窗体继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆