简体   繁体   English

在Tab控件上索引到控件集合

[英]Indexing into Controls Collection on Tab Control

I have a C# Forms tab application. 我有一个C#表单标签应用程序。 Each TabPage has a menu on the left (Outlook style navigation panel), and a Panel on the right for content. 每个TabPage的左侧都有一个菜单(Outlook样式的导航面板),右侧有一个Panel来显示内容。

If I want the content panel for tab page 0, how would I go about fetching it? 如果我想要选项卡页面0的内容面板,我将如何获取它? I'm a bit stumped because I don't know how to index into the controls collection on a tab page. 我有些困惑,因为我不知道如何在选项卡页上索引到控件集合。 The following is underlined in red, so I believe its wrong. 以下用红色下划线标出,因此我相信它是错误的。

Panel panel = tabControl.TabPages[0].Controls["Panel"];

EDIT: remove Window in Panel sub question. 编辑:删除面板子问题中的窗口。 It will be moved to a separate question. 它将移至另一个问题。

Sorry about the beginner questions. 对不起,初学者的问题。 I'm a C/C++ guy with lots of MFC time, and C# UI is a bit frustrating at the moment. 我是一位拥有大量MFC时间的C / C ++人,而C#UI目前有点令人沮丧。

in order to create a new form for example you need to create a variable of what ever form that it is you want to create. 例如,为了创建一个新表格,您需要创建一个您想要创建的表格形式的变量。 example

Form2 frm2 = new Form2();
frm2.Show(); 

if you want to show that form in the panel then the panel would be the Owner keep in mind the difference between Owner and Parent please paste what ever code you have so far and we can suggest the necessary changes 如果您想在面板中显示该表单,那么面板将是所有者,请记住所有者和父代之间的区别,请粘贴您到目前为止所拥有的代码,我们会建议您进行必要的更改

foreach (Control control in tabControl1.TabPages[0].Controls)
{
    // if (control.Name == "panel1")
}

You can always call this recursively on control.Controls to find a control in any hierarchy. 您始终可以在control.Controls上递归调用此control.Controls以在任何层次结构中查找控件。 control.Name can be used to find your specific control. control.Name可用于查找您的特定控件。

You can't show a Form, inside a Panel. 您无法在面板内显示表单。 You could create Custom Control where you can add your functionality and add that control to a Panel. 您可以创建自定义控件,在其中可以添加功能并将该控件添加到面板中。

Finally, how does one display a Window in a Panel? - you don't want to do that. -你不想那样做。 If you want a window and a panel to share a piece of UI functionality, create a user control with all the the functionality and then you can place it in a form or in a panel. 如果希望窗口和面板共享一部分UI功能,请创建具有所有功能的用户控件,然后将其放置在窗体或面板中。

A possibility to encapsulate complex UI content is to create a UserControl . 封装复杂的UI内容的一种可能性是创建一个UserControl This way you can create a reusable piece of complex UI you can basically add as a "blob" inside a form. 这样,您可以创建可重复使用的复杂UI,基本上可以将其添加为表单中的“ blob”。

The reason why 之所以

Panel panel = tabControl.TabPages[0].Controls["Panel"];

is underlined red is because the Controls collection returns a Control which might be a Panel but also might be something else. 带红色下划线的原因是Controls集合返回一个Control ,该Controls可能是一个Panel ,也可能是其他Controls So you need to cast it: 因此,您需要强制转换:

Panel panel = tabControl.TabPages[0].Controls["Panel"] as Panel;
if (panel != null)
{
     // got a panel here so do something
}

Also: MSDN has some good resources - you should make use of it. 另外:MSDN有一些很好的资源-您应该利用它。

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

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