C# 如何在两个不同的选项卡上显示相同的控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/513276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:21:06  来源:igfitidea点击:

How to display the same control on two different tabs?

c#vb.netwinformscontrolstabcontrol

提问by

I'm using VB.NET

我正在使用 VB.NET

I need same control (ListBox) to be displayed on 2 different tabs.

我需要在 2 个不同的选项卡上显示相同的控件 (ListBox)。

Is it mandatory to create 2 different ListBox instances?

是否必须创建 2 个不同的 ListBox 实例?

采纳答案by JMD

If you don't need design-time support you can simply, at runtime, change the ListBox instance's Parent from one tab to the other (making sure to set the positioning appropriately, of course).

如果您不需要设计时支持,您可以在运行时简单地将 ListBox 实例的 Parent 从一个选项卡更改为另一个选项卡(当然,确保正确设置定位)。

Essentially, it's:

本质上,它是:

listBox1.Parent = tabControl1.TabPages[1];

listBox1.Parent = tabControl1.TabPages[1];

In the end though, you'll probably find it easier to just have two ListBox's with the same data source.

但最后,您可能会发现只拥有两个具有相同数据源的 ListBox 会更容易。

回答by Kevin Tighe

Yes, I think you'll need a ListBox control on each tab. If they have the same data you can use the same DataSource for both though.

是的,我认为每个选项卡上都需要一个 ListBox 控件。如果它们具有相同的数据,您可以为两者使用相同的 DataSource。

回答by Eppz

Yes, add a new instance on each tab.

是的,在每个选项卡上添加一个新实例。

回答by GWLlosa

If you want full designer support, you'll need two boxes. If doing it in code is enough, you can create a single listbox on form load, and manually add a reference to it to each tab page.

如果您想要全面的设计师支持,您将需要两个盒子。如果在代码中这样做就足够了,您可以在表单加载时创建一个列表框,并手动将对其的引用添加到每个标签页。