WindowsFormsHost添加 [英] WindowsFormsHost Add on
问题描述
如何将一个表单托管到另一个项目的另一个表单上?
How do I host one form onto another Form from another project?
我有2个项目有两个表单。项目A具有带有控件的主要主机表单。我只是尝试将带有Form的Project B引用到Project A Main Host表单。有没有办法做到这一点。
I have 2 projects that has two forms. Project A has the Main Host form with a control. I am simply trying to reference Project B with Form on to Project A Main Host form. Is there a way to do this.
项目B的表格b需要在项目A主要主机表格上方。我已将exe引用到Project A参考中。我可以访问项目B中的对象,但是我没有在工具箱中看到实际的表单。
Form b from Project B needs to go on top Project A Main Host form. I have reference the exe into the Project A references. I have access to the objects from Project B, But I do not see the actual form in the tool box.
pianoboyCoder
pianoboyCoder
推荐答案
嗨pianoboyCoder,
Hi pianoboyCoder,
对于迟到的回复感到抱歉。
Sorry for the late reply.
>>如何将一个表单托管到另一个项目的另一个表单上?
>>How do I host one form onto another Form from another project?
请参考以下步骤:
步骤1:创建两个名为Test1和Test2的项目,向Test1项目中的Form1添加一些控件,构建解决方案。
Step1: Create two projects named Test1 and Test2, add some controls to the Form1 within Test1 project, build solution.
Step2 :添加test1.exe作为对Test2的引用:
Step2: Add test1.exe as a reference to Test2:
步骤3:使用WindowsFormsTest1添加到WindowsFormsTest2中MainForm.cs的顶部。
Step3: Add using WindowsFormsTest1 to the top of the MainForm.cs within WindowsFormsTest2.
步骤4:在WindowsFormsTest2中向MainForm添加一个面板来托管Form1在WindowsFormsTest1中,以及添加它的代码:
Step4: Add a panel to MainForm within WindowsFormsTest2 to host Form1 within WindowsFormsTest1, and the code to add it:
using System.Windows.Forms;
using WindowsFormsTest1;
namespace WindowsFormsTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WindowsFormsTest1.Form1 f1 = new WindowsFormsTest1.Form1();
f1.TopLevel = false;
this.panel1.Controls.Add(f1);
f1.Show();
}
}
}
>>但我没有在工具箱中看到实际的表格。
>>But I do not see the actual form in the tool box.
你是什么意思?是否要将Form1(在Test1中)显示为工具?
What do you mean about this? Do you want to display the Form1(Within Test1) as a tool?
如果是,请将Form1(在test1中)的父类从Form更改为UserControl:
If so, please change the Form1(within test1)'s parent class from Form to UserControl:
using System.Windows.Forms;
namespace WindowsFormsTest1
{
public partial class Form1 : UserControl
{
public Form1()
{
InitializeComponent();
}
}
}
然后在WindowsFormsTest2项目中添加一个类来继承上面的Form1:
Then add a class in WindowsFormsTest2 project to inherit the above Form1:
using WindowsFormsTest1;
namespace WindowsFormsTest2
{
class ClassInheritFromTest1 : WindowsFormsTest1.Form1
{
}
}
运行项目然后关闭它,您可以发现工具箱中有一个控件显示,将其拖到MainForm并使用它:
Run the project then close it, and you can find there is a control showing in toolbox, drag it to the MainForm and use it:
希望这会有所帮助!
最诚挚的问候,
Stanly
这篇关于WindowsFormsHost添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!