C#将文本框添加到特定面板 [英] C# add textbox to a specific panel

查看:112
本文介绍了C#将文本框添加到特定面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个非常简单的问题,但我似乎没有找到解决方案。



我在我的c#项目中添加了一个面板。



如何在主窗体布局和面板之间切换设计器?

另外(但可能在与上述问题相同的答案)如何将文本框添加到特定面板或主窗体?



问候。



更新:

经过一番实验后,我找到了这个可行的解决方案:

如果你想与之合作通过菜单选项激活的多个面板这对我有用:



在我的测试用例中我有

1表格:Form1

1 Menustrip:menuStrip1

1 MenuStrip option1:ToolStripMenuItem1(激活Panel1)

1 MenuStrip option2:ToolStripMenuItem2(激活Panel2)

1面板:Panel1

1面板:Panel2



我想要当从menustrip选项中点击时,面板将填满整个表格。



问题是我不明白有一个简单的方法来选择面板GUI没有移动它们。

我还不清楚这个。

总有一个在VS中直接可见。



因此,如果您将2个面板添加到主窗体,并给它们Dock:Fill,则无法访问主窗体或第一个添加的面板。

作为最后一个添加的面板是可见的。

您可以认为在添加第二个面板之前将所有需要的控件添加到第一个面板。

这显然是正确的,但是当你是开始一个项目,并且不知道所需的范围或需求,而不是你需要改变面板上的控件。



我找到的工作这些方法:

1)如果你添加第二个面板,VS将它添加到第一个面板,而不是表格。

所以去Form1.Designer.cs并找到this.Controls.Add(this.panel2);并将其从Panel1的控件列表中删除,并将其添加到表单的控件列表中。

  //    
// panel1
//
this .panel1.Controls.Add( this .panel2);



删除并放入部分Form1

  //    
// Form1
//
this .Controls.Add( .panel1);
.Controls.Add( this .panel2);





2)如果你想在面板1上工作,但现在看到Form.cs [Design]中的面板2,只需改变面板列出的面板表单的控制列表:

这将面板1显示为活动面板:

  //    
// Form1
//
this .Controls.Add( this .panel1);
.Controls.Add( this .panel2);





这将面板2显示为活动面板:

  //    
// Form1
//
this .Controls.Add( this .panel2);
.Controls.Add( .panel1);

解决方案

您只需将面板上的文本框控件拖放即可。就是这样!



IDE中的表格或面板布局之间没有切换。


请阅读本教程:创建您的第一个C#应用程序 [ ^ ]



如需了解更多信息,请参阅:创建您的第一个Visual C#应用程序 [ ^ ]


最后将控件动态添加到面板



this.panel1.controls.add(TextBox1中);

Hi, probably a very simple question, but I don't seem to find the solution my self.

I've added a panel in my c# project.

How can I switch in the designer between the main form layout and the panel(s)?
Also (but probably within the same answer as the above question) how can I add a textbox to a pecific panel or main form?

Regards.

UPDATE:
After some experimenting with this I came to this "workable" solution:
If you want to work with multiple panels which are being activated by a menu option here's a way which worked for me:

In my test case I have
1 Form: Form1
1 Menustrip: menuStrip1
1 MenuStrip option1: ToolStripMenuItem1 (activates Panel1)
1 MenuStrip option2: ToolStripMenuItem2 (activates Panel2)
1 Panel: Panel1
1 Panel: Panel2

I want the panels to fill the whole form when clicked from the menustrip option.

The problem was that I didn't understand that there's a simple way of selecting a panel in the GUI without moving them around.
Still this is not clear to me.
There's always one which is directly visible in VS.

So if you add the 2 panels to the main form, and give them Dock: Fill, you can't reach the main Form or the first added panel.
As the last added panel is visible.
You could think that you add all needed controls to the first panel before you add the second one.
This is Obviously true, but when you are starting a project, and do not know the scope of whisess or needs, than you need to alter the controls on a panel.

The work around to this I found in these methodes:
1) If you add the second panel, VS adds it to the first panel, instead of the Form.
So go to Form1.Designer.cs and find "this.Controls.Add(this.panel2);" and remove it from the controls list of Panel1 and add it to the controls list of the Form.

//
// panel1
//
this.panel1.Controls.Add(this.panel2);


Remove and place in section Form1

// 
// Form1
//
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);



2) If you want to work on panel 1 but now see the panel 2 in the Form.cs[Design], just alter the sequense the panels are listed in the control list of the Form:
This shows the panel 1 as active panel:

//
// Form1
//
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);



This shows the panel 2 as active panel:

//
// Form1
//
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);

解决方案

You just drag and drop the textbox control on the panel. That's it!

There is no "switching" between Form or Panel layout in the IDE.


Please, read this tutorial: Creating Your First C# Application[^]

For further information, please see: Creating Your First Visual C# Application[^]


in last add your control to panel dynamically

this.panel1.controls.add(textbox1);


这篇关于C#将文本框添加到特定面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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