使用MainForm和Panel制作子表单 [英] Making SubForms with MainForm And a Panel
问题描述
根据此页
使用表格作为子表格 [ ^ ]
i完全按页面描述
,但处于两个子表单或更多子状态不工作
假设我有一个MenuStrip有两个toolStripMenuItem Page1和Page2
当我点击第1页时添加了一个subForm1但是当我点击Page2没有任何反应
,因为SubForm2组件位于SubForm1组件后面
我如何告诉小组丢弃SubForm1并仅包含SubForm2
这是代码
private void page1ToolStripMenuItem_Click( object sender,EventArgs e)
{
Subform SubForm = new Subform();
SubForm.FormBorderStyle = FormBorderStyle.None;
SubForm.TopLevel = false ;
SubForm.ShowInTaskbar = false ;
SubForm.Show();
SubForm.Dock = DockStyle.Fill;
this .panel1.Controls.Add(SubForm);
}
private void page2ToolStripMenuItem_Click( object sender,EventArgs e)
{
subform1 SubForm = new subform1();
SubForm.FormBorderStyle = FormBorderStyle.None;
SubForm.TopLevel = false ;
SubForm.ShowInTaskbar = false ;
SubForm.Show();
SubForm.Dock = DockStyle.Fill;
此 .panel1 = 新 Panel();
this .panel1.Controls.Add(SubForm);
}
谢谢
不,这不是你的代码所做的 - 你试图用你的Page2代码中的新版本替换现有的Panel:
此 .panel1 = new Panel();
this .panel1.Controls.Add(SubForm);
那不做什么但是你想要 - 因为旧的面板仍然在Form.Controls集合中,而不是新面板 - 所以永远不会显示新的子表单。
将代码更改为先清空旧面板:
panel1.Controls.Clear();
panel1.Controls.Add(SubForm);它应该都可以工作。
BTW:你不需要一直指定这个
- 只有在你的意思是同名的两个变量版本混淆时才需要它:
< pre lang =c#> private string text = hello;
private string otherText = 12345;
private void 更改( string text)
{
this .text = 再见 跨度>; // 显式引用类级别变量
text + = there; // 隐式引用方法参数;
otherText = text; // 隐式引用类级别otherText和方法参数。
}
According to this Page
Use form as subform[^]
i do exactly what the page describes
but in the state of having two subforms or more it doesn't work
suppose i have a MenuStrip having two toolStripMenuItem Page1 and Page2
when i click Page 1 a subForm1 is added but when i click Page2 nothing happens
because the SubForm2 Components Lies behind SubForm1 Components
How can i tell the panel to discard SubForm1 and include only SubForm2
here is the code
private void page1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Subform SubForm = new Subform();
SubForm.FormBorderStyle = FormBorderStyle.None;
SubForm.TopLevel = false;
SubForm.ShowInTaskbar = false;
SubForm.Show();
SubForm.Dock = DockStyle.Fill;
this.panel1.Controls.Add(SubForm);
}
private void page2ToolStripMenuItem_Click(object sender, EventArgs e)
{
subform1 SubForm = new subform1();
SubForm.FormBorderStyle = FormBorderStyle.None;
SubForm.TopLevel = false;
SubForm.ShowInTaskbar = false;
SubForm.Show();
SubForm.Dock = DockStyle.Fill;
this.panel1 = new Panel();
this.panel1.Controls.Add(SubForm);
}
Thanks
No.,that isn't what your code is doing - you are trying to replace the existing Panel with a new version in your Page2 code:
this.panel1 = new Panel(); this.panel1.Controls.Add(SubForm);
That doesn't do what you want, however - because the old panel is still in the Form.Controls collection, not the new one - so the new subform1 is never displayed.
Change your code to just empty the old panel first:
panel1.Controls.Clear(); panel1.Controls.Add(SubForm);And it should all work.
BTW: You don't need to specifythis
all the time - you only need it when there is confusion as to which version of two variables with the same name you mean:
private string text = "hello"; private string otherText = "12345"; private void change(string text) { this.text = "Goodbye"; // explicitly references the class level variable text += " there"; // implicitly references the method parameter; otherText = text; // implicitly references class level otherText and method parameter. }
这篇关于使用MainForm和Panel制作子表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!