如何在C#中显示面板内的表单 [英] How can I display a form inside a panel in C#

查看:90
本文介绍了如何在C#中显示面板内的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中创建了一个C#项目。我创建了一个名为Home的主窗体和两个名为AddBatches和AddMembers的子窗体。我在Main表单中使用了一个面板,在面板中显示AddBatches表单信息。但最终我想在面板中显示AddBatches,并在AddBatches中单击按钮,我想在面板内显示Addmembers表单。我该怎么办?



我尝试过:



我使用以下代码在面板中显示AddBatches;



AddBatches AddB = new AddBatches();

AddB.TopLevel = false;

AddB.FormBorderStyle = FormBorderStyle.None;

panel4.Controls.Clear();

panel4.Controls.Add(AddB );

AddB.Show();

I have created a C# project in Visual Studio. I've created a main form named "Home" and two sub-form named "AddBatches" and "AddMembers". I've used a panel in "Main" form to show the "AddBatches" form information into the panel. But ultimately I want to display "AddBatches" inside the panel and by a button click in AddBatches, I want to display "Addmembers" form inside the panel too. How can I??

What I have tried:

I've used the following code to display "AddBatches" inside the panel;

AddBatches AddB = new AddBatches();
AddB.TopLevel = false;
AddB.FormBorderStyle = FormBorderStyle.None;
panel4.Controls.Clear();
panel4.Controls.Add(AddB);
AddB.Show();

推荐答案

我这样做的方法是不使用表格 - 创建控件您可以将每个表单作为UserControl放置,您可以直接将其添加到Panel,或将其停靠在单独的表单中并在那里显示。这样,你没有做任何不寻常的事情,比如尝试在表单中嵌入表单,隐藏/显示信息变得简单:

The way I'd do it is not to use a Form - create the controls you would have put on each Form as a UserControl and you can add that to a Panel directly, or dock it into a separate Form and display it there. That way, you aren't doing anything unusual like trying to embed a form in a form, and hiding / displaying the info becomes simple:
myAddBatchesControl.Visible = false;
myAddMemberControl.Visible = true;



And

myAddBatchesControl.Visible = true;
myAddMemberControl.Visible = false;

它还允许您添加其他表单而不需要任何戏剧性,只需通过扩展使可见和使隐藏的代码包含新的控件。

您将所有功能封装到一个表单中,没有任何缺点。

It also allows you to add other "forms" without any drama, just by extending the "make visible" and "make invisible" code to include the new controls.
You get all the advantages of "encapsulating" the functions into a form, with none of the disadvantages.


这篇关于如何在C#中显示面板内的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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