索引到选项卡控件上的控件集合

发布于 2024-12-23 02:24:26 字数 339 浏览 1 评论 0 原文

我有一个 C# 表单选项卡应用程序。每个 TabPage 的左侧都有一个菜单(Outlook 样式的导航面板),右侧有一个内容面板。

如果我想要标签页 0 的内容面板,我该如何获取它?我有点困惑,因为我不知道如何索引选项卡页上的控件集合。下面用红色下划线表示,所以我认为它是错误的。

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

编辑:删除面板子问题中的窗口。它将被转移到一个单独的问题。

抱歉,对于初学者的问题。我是一个 C/C++ 人员,有很多 MFC 时间,而 C# UI 目前有点令人沮丧。

I have a C# Forms tab application. Each TabPage has a menu on the left (Outlook style navigation panel), and a Panel on the right for content.

If I want the content panel for tab page 0, how would I go about fetching it? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2024-12-30 02:24:26
foreach (Control control in tabControl1.TabPages[0].Controls)
{
    // if (control.Name == "panel1")
}

您始终可以在 control.Controls 上递归调用此函数来查找任何层次结构中的控件。 control.Name 可用于查找您的特定控件。

您无法在面板内显示表单。您可以创建自定义控件,您可以在其中添加功能并将该控件添加到面板中。

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.Name can be used to find your specific control.

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.

浮光之海 2024-12-30 02:24:26

例如,为了创建一个新表单,您需要创建一个您想要创建的表单的变量。
例如,

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

如果您想在面板中显示该表单,那么面板将是所有者,请记住所有者和父级之间的区别
请粘贴您迄今为止拥有的代码,我们可以建议必要的更改

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

财迷小姐 2024-12-30 02:24:26

最后,如何在面板中显示窗口? - 您不想这样做。如果您希望窗口和面板共享一项 UI 功能,请创建一个具有所有功能的用户控件,然后可以将其放置在窗体或面板中。

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.

太傻旳人生 2024-12-30 02:24:26

封装复杂 UI 内容的一种可能性是创建一个 用户控件。通过这种方式,您可以创建一个可重用的复杂 UI,您基本上可以将其作为“blob”添加到表单中。

之所以

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

带有红色下划线是因为 Controls 集合返回一个 Control 可能是一个 Panel 但也可能是其他东西。所以你需要转换它:

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

另外:MSDN 有一些很好的资源 - 你应该利用它。

A possibility to encapsulate complex UI content is to create a UserControl. This way you can create a reusable piece of complex UI you can basically add as a "blob" inside a form.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文