C ++中的表单继承 [英] form inheritance in c++

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

问题描述

大家好
我想为我的程序提供一个全局形式,所以我创建了一个简单的公共形式,然后以其他形式继承了它,我的代码是这样的:

hi all
I want to have a global form for my program so I create a simple public form and then I inherit it in other form , my code is this :

#pragma once
#include "Frm_Global.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;



namespace MyNameSpace {

	public ref class Frm_Bill : public Frm_Global
	{
	public:
		Frm_Bill(void)
		{
			InitializeComponent();
		}

	public:
		~Frm_Bill()
		{
			if (components)
			{
				delete components;
			}
		}



	private:
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->SuspendLayout();
			// 
			// Frm_Bill
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(573, 367);
			this->Name = L"Frm_Bill";
			this->Text = L"Frm_Bill";
			this->ResumeLayout(false);

		}
#pragma endregion
	};
}


但不能在设计器中加载全局表单!


but can not load the Global Form in designer !

推荐答案

首先请注意:"Frm_Global"的命名非常糟糕.从形式上讲,这并不重要,但是显示出一些误解.这种形式不再是全局"形式,而是任何其他形式,这仅仅是一种形式,它打算成为某些其他形式的基类,仅此而已.

现在,尚不清楚您需要加载"设计器的内容以及原因.也许,这是基于对设计师所做工作的一些误解.让我们概述最简单的步骤.首先,为基础表单创建类.如果确实使用设计器,则可以使用设计器查看使用它的情况.您也可以像其他任何类一样完全在代码中创建该类.后续步骤无关紧要.假设您将基本表单命名为BaseForm.

现在,您要创建一个派生表单.如果创建的是未经设计的,则只需编写它的代码并为该类指定继承列表.首先,如果在单独的类中执行此操作,则应添加#include并包括一个用于声明基本表单的头文件. (许多年来,我不禁想知道,诸如"include"之类的古老技术在XX年底甚至二十一世纪之前将如何生存!)然后,您应指定像public ref class FormBill : BaseForm { /* ... */ };这样的派生形式并完成.您也可以使用使用设计器的派生工具,但是最好使用设计器来创建此表单.在这种情况下,步骤将有所不同.设计器将为您生成带有继承列表的头文件,看起来像public ref class FormBill : public System::Windows::Forms::Form { /* ... */ };一样,您应该再次包含基本表单的头文件,并修改声明,将基本类System::Windows::Forms::Form替换为BaseForm .在这种情况下,使用派生形式FormBill的设计器,您将看到基类和派生类的控件.

同样,您可以继承其他.NET程序集定义的格式(无论使用哪种.NET语言编写).您应该通过定义派生形式的程序集引用该程序集,并使用命名空间名称完全限定基类. (实际上,在所有情况下,都可能需要用全部或部分名称空间来限定名称,而与将类放在程序集中的方式无关,因为您可以在同一程序集中使用不同的名称空间,并在程序集中共享相同的名称空间.) >
—SA
First note: the naming "Frm_Global" is pretty bad. Formally, it does not matter, but shows some misunderstanding. This form is no more "global" then any other form, this is just a form meant to be a base class for some other forms, nothing more.

Now, it''s not clear what do you need to "load" in designer and why. Maybe, this is based on some misconception on what designer does. Let''s overview the simplest steps. First, you create you class for you base form. If you do use using the designer, you can see work with it using the designer. You can also create this class as any other class, purely in code. This does not matter for further steps. Let''s assume you name you base form as BaseForm.

Now, you want to create a derived form. If you create is without designed, you simply write code of it and specify the inheritance list for the class. First, if you do it in a separate class, you should add #include and include a header file where you declared the base form. (Many years I cannot help wondering how such archaic technique as "include" could survive by the end of XX and even in XXI century!) Then, you should specify the derived form like this public ref class FormBill : BaseForm { /* ... */ }; and be done. You can also work with the derived from using the designer, but then you should better create this form using the designer. In this case, the steps will be different. The designer will generate you the header file with inheritance list already in place, looking something like public ref class FormBill : public System::Windows::Forms::Form { /* ... */ }; you should, again, include the header file of the base form and modify the declaration replacing the base class the System::Windows::Forms::Form with BaseForm. In this case, you will see the control of both base and derived class with the designer of the derived form FormBill.

Likewise, you can inherit the forms defined by other .NET assemblies (no matter written in what .NET language). You should reference this assembly by the assembly where you define the derived form and qualify the base class fully, with namespace name. (Actually, you may need to qualify the names with full or partial namespaces in all cases, not matter how the classes are placed in assembly, as you can use different namespaces in the same assembly and share identical namespaces across assemblies.)

—SA


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

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