简体   繁体   English

在Windows窗体中,是否可以加载表单,使其位于另一种形式?

[英]in windows forms, is it possible to load a form so its located in another form?

lets say i have a main form which have a lot of functionallity. 假设我有一个主要形式,具有很多功能。
this form have a tab control in which each tab contain some set of functionality. 此表单有一个选项卡控件,其中每个选项卡包含一些功能集。
what i want to do is when i click on each tab controls button i want to load a form into the client area of the tab control. 我想要做的是当我点击每个选项卡控件按钮我想将表单加载到选项卡控件的客户端区域。
so instead of having a lot of controls in the main form , i will only have set of forms and each form will have its control. 所以我不会在主窗体中有很多控件,而只会有一组窗体,每个窗体都有自己的控件。
i think this is will be better in term of managing the controls so i dont have like 150 control on the main form. 我认为这在管理控件方面会更好,所以我对主表单上没有150控件。 so basically i want to load a form on another form and not show the form in a seperate view. 所以基本上我想在另一个表单上加载一个表单而不是在单独的视图中显示表单。
if its not possible with forms then can i use another control that will group the controls and will be loaded on the main form? 如果表单不可能,那么我可以使用另一个控件来对控件进行分组并将其加载到主窗体上吗?
thanks 谢谢

Alternate 1 : 替代1:

You can make each of the form as a User Control and then you can load the appropriate user control in a blank panel in your main form whenever required. 您可以将每个表单设置为用户控件,然后您可以在需要时在主表单的空白面板中加载相应的用户控件。

You should be able to find a way to communicate between your form and those user controls. 您应该能够找到一种在表单和用户控件之间进行通信的方法。

Alternate 2 : 替代2:

You can show the appropriate Form using ShowModal() method, with the main form as parent, that way user can finish the work with the child form, before coming back to the main form. 您可以使用ShowModal()方法显示相应的Form,主窗体作为父窗体,这样用户可以在返回主窗体之前完成子窗体的工作。

Disadvantages here are user wont be able to interact with the main form as long as the child form is closed. 这里的缺点是,只要子表单关闭,用户就无法与主表单进行交互。

I would recommend looking into User Controls . 我建议调查用户控件

User Controls come with a designer, just like forms, and have a rich event model to tap into. 用户控件带有设计器,就像表单一样,并且具有丰富的事件模型可供使用。 Unlike forms, they are easy to embed into other controls and forms. 与表单不同,它们很容易嵌入到其他控件和表单中。 As a matter of fact, user controls will show up in your toolbox to drag-and-drop onto another form. 事实上,用户控件将显示在您的工具箱中,以便拖放到另一个表单上。

It's at least worth taking a look at. 这至少值得一看。

Following code adds one Form to a panel in another form. 以下代码将一个表单添加到另一个表单的面板中。

Add this code in Form1 在Form1中添加此代码

        Form2 ff = new Form2();
        ff.TopLevel = false;

        ff.Dock = DockStyle.Fill;
        ff.ControlBox = false;
        ff.Text = "";

        panel1.Controls.Add(ff);
        ff.Show();

The flip side is your panel should be big enough to accomodate the form... 另一面是你的面板应该足够大以容纳形式......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM