C#中的视觉继承问题 [英] A problem with visual inheritance in C#

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

问题描述

我在Visual Studio C#中的视觉继承遇到了意外的问题.有一个基本形式,从中可以继承几个派生形式.一切都可以很好地编译,没有错误,但是派生的表单看起来与基本表单完全一样,即使它们不应该(派生的表单具有一些基本表单不需要的控件;这些控件是在设计时添加的,而不是在设计时添加的)在运行时(如果有帮助的话);以某种方式这些控件在运行时不会出现在派生的窗体上.项目中没有表单的所有类在编译时也会显示相同的基本表单,即使它们没有从其继承.

知道是什么原因造成的吗?我已经多次查看了我所有的代码,但仍然看不出它有什么问题.

谢谢您提供信息.

I am having an unexpected problem with visual inheritance in Visual Studio C#. There is a base form, from which several derived forms inherit. Everything compiles nicely, without errors, but the derived forms look exactly like the base form, even though they shouldn''t (the derived forms have some controls that the base form doesn''t; those controls were added at design time, not at run time, in case this helps; somehow those controls don''t appear on the derived forms at run time). Any classes in the project that don''t have forms also show the same base form when compiled, even though they don''t inherit from it.

Any idea what could be causing this? I''ve looked over all my code multiple times and still don''t see what''s wrong with it.

Thank you for the information.

推荐答案

奇怪.
我只是试过了(直接从表单派生不是我经常做的事情,所以我想确定)

我有一个名为frmPurchaseType
的现有表格 我添加了一个名为frmDeleteMe
的表格 我将frmDeleteMe.cs文件更改为:
Odd.
I just tried it (deriving directly from a form is not something I do that often, so I wanted to be sure)

I have an existing form called frmPurchaseType
I added a form called frmDeleteMe
I changed the frmDeleteMe.cs file to:
public partial class frmDeleteMe : frmPurchaseType


我在设计器中打开了frmDeleteMe,将其放大,并添加了一个新按钮.所有frmPurchaseType控件都存在.
我在主表单加载事件中添加了代码:


I opened the frmDeleteMe in the designer, enlarged it, and added a new button. All the frmPurchaseType controls were present.
I added code to my main form load event:

frmDeleteMe f = new frmDeleteMe();
f.Show();


我运行了该应用程序.显示的形式是frmDeleteMe,带有额外的按钮,以及所有frmPurchaseType控件.

您做了什么我没做的事?


I ran the app. The form that showed was frmDeleteMe, with the extra button, and all frmPurchaseType controls.

What did you do that I didn''t?


表单继承可以正常工作.您看到的只是一个错误,仅此而已.不看项目就很难说出什么问题.

注意:与其他形式没有父母形式"之类的东西. 好吧,很正式,它确实存在,因为FormControl;但是,如果您尝试使某事物成为Form实例的父对象,则除非将Form.TopLevel分配给true,否则将引发异常.在这种情况下,您可以将孩子做成表格,但是为什么呢?结果很丑. [结束编辑]由于Control是形成控件的子级的父级.表单可以是所有者表单".您试图说的是基类".

—SA
Form inheritance works without any problem. What you see is just a bug, nothing else. Without a look at you project it''s hard to say what''s wrong.

A note: there is no such thing as "parent form" in relation to other form. Well, quite formally, it does exist as Form is Control; but if you try to make something a parent of a Form instance, an exception is thrown unless you assign a Form.TopLevel to true; in this case you can make a form a child, but why? The results of it are ugly. [END EDIT] As a Control is a parent to form children, which are controls. A form can be the "Owner form". What you tried to say is "base class".

—SA


这篇关于C#中的视觉继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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