[英]C#, WinForm in another winform
I need help.我需要帮助。 I have winform, like one which is shown on picture, so I want to improve next things, when I click on first button, on the right side will be showed content from another winform, is this possible?
我有 winform,就像图片上显示的那样,所以我想改进接下来的事情,当我点击第一个按钮时,右侧会显示另一个 winform 的内容,这可能吗? I don't want to use panels.
我不想使用面板。
Why don't you just embed a single UserControl
dynamically to represent the current page or if you want to do everything at design-time - a custom TabControl
?为什么不动态地嵌入一个
UserControl
来表示当前页面,或者如果您想在设计时完成所有操作 - 自定义TabControl
?
showed content from another winform
显示来自另一个 winform 的内容
You generally don't embed a popup window into another, rather controls.您通常不会将弹出窗口嵌入到另一个控件中。 Otherwise you have to deal with hiding Minimise, Maximise, Close etc.
否则,您必须处理隐藏最小化、最大化、关闭等问题。
You can use TabControl control -> for each specific content you want to display you just have to add a TabPage to TabControl and when a specific menu option is selected, just switch to required TabControl page.您可以使用TabControl控件 - >对于要显示的每个特定内容,您只需将拖布页添加到TabControl以及选择特定菜单选项时,只需切换到所需的TabControl页面即可。
myTabControl.SelectedIndex = 1; // for selecting and displaying page with index 1
To hide navigation header of myTabControl, set those props (in constructor or in Form_Load event:要隐藏 myTabControl 的导航标题,请设置这些道具(在构造函数或 Form_Load 事件中:
myTabControl.ItemSize = new Size(0, 1);
myTabControl.SizeMode = TabSizeMode.Fixed;
Then, you have only content of the TabControl page displayed, without navigation header.然后,您将只显示 TabControl 页面的内容,而没有导航标题。
First make it's this.IsMdiContainer = true;
首先使它成为
this.IsMdiContainer = true;
either from design time or run time .无论是设计时还是运行时。
then at the button's click event place the following code.然后在按钮的点击事件中放置以下代码。
childForm frm = new childForm();
frm.MdiParent = this; //the current mdi parent form
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
frm.Show();
You can use mdi (multiple-document interface): when you want a form start inside mainform, use this following code:您可以使用 mdi(多文档界面):当您希望在 mainform 中启动表单时,请使用以下代码:
Dim hotelForm As New HotelForm()
HotelForm.MdiParent = Me // Me is your parent form want to open hotelForm inside
HotelForm.Show()
All forms have a property TopLevel
, by setting this property to false, then you can deal with that form as a control, and add it to a panel control.所有表单都有一个属性
TopLevel
,通过将此属性设置为 false ,您就可以将该表单作为控件处理,并将其添加到面板控件中。
See below psuedo code:见下面的伪代码:
Form2 newForm = new Form2();
newForm.TopLevel = false;
myPanel.Controls.Add(newForm);
newForm.Show();
After that, your main form design should look like as a navigation control and a panel beside it docked "Fill", then on clicking any navigation button, just create the desired form and set TopLevel
to false, then show it on the panel之后,您的主表单设计应该看起来像一个导航控件和它旁边的面板停靠“填充”,然后单击任何导航按钮,只需创建所需的表单并将
TopLevel
设置为 false,然后将其显示在面板上
You have to put a Container control like a panel "pnlHost" in your form and use this for showing any form you want in it您必须在表单中放置一个像面板“pnlHost”这样的容器控件,并使用它来显示您想要的任何表单
private Form _currentForme;
private void ShowForm(Form frm)
{
_currentForme?.Close();
_currentForme = null;
_currentForme = frm;
_currentForme.TopLevel = false;
_currentForme.TopMost = false;
pnlHost.Controls.Clear();
_currentForme.FormBorderStyle = FormBorderStyle.None;
pnlHost.Controls.Add(_currentForme);
_currentForme.Dock = DockStyle.Fill;
_currentForme.Show();
}
MDI Form is the best solution for you. MDI Form 是您的最佳解决方案。
Use: this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;` to make your child Forms more beautiful in your project!
使用:this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;`让你的子窗体在你的项目中更漂亮!
If you want the MDI parent to auto-size the child form you can code like this.
如果您希望 MDI 父级自动调整子窗体的大小,您可以这样编码。
Code Example:代码示例:
private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}
private void btnHotel_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}
private void btnCaffe_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}
I would use MDI parent and child for this.我会为此使用 MDI parent 和 child。
But I guess you could also finish the child application, build the exe and initiate it as a new process from your Parent application.但我想您也可以完成子应用程序,构建 exe 并将其作为父应用程序的新进程启动。 Check the solution given here .
检查此处给出的解决方案。 I tried a similar approach long ago and it worked perfectly.
我很久以前尝试过类似的方法并且效果很好。
将当前表单设置为 MdiParent 然后使用 Datagridview 显示来自其他表单的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.