将控件添加到在 C# .net 中的 tabcontrol 中动态生成的选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/996182/
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
adding a control to tabs generated dynamically in tabcontrol in C# .net
提问by nidhiac
I am new to C# .NET.
我是 C# .NET 的新手。
Can some one help me out with the below problem:
有人可以帮我解决以下问题吗:
I have a TabControl in my WindowsForm application, where the tab pages are generated dynamically. The content to be displayed on each tab would be fetched from my database. I need some kind of control (which can display the fetched data) that I can add on each tab page (which would be same for all tabs) such that I can associate some kind of event, say click, on that added control.
我的 WindowsForm 应用程序中有一个 TabControl,其中的标签页是动态生成的。每个选项卡上显示的内容将从我的数据库中获取。我需要某种控件(它可以显示获取的数据),我可以在每个选项卡页面上添加它(这对于所有选项卡都是相同的),以便我可以在添加的控件上关联某种事件,比如单击。
Can anyone tell me how to do this programmatically & write the click event for all the controls added?
谁能告诉我如何以编程方式执行此操作并为添加的所有控件编写单击事件?
回答by MDStephens
I'm not sure I completely understand your problem but my initial thoughts are that you could dynamically create a datagrid or something similar for each tab that you are dynmically creating. You could then bind the datasource for the grid and then add the grid as a control to your tabpage.
我不确定我是否完全理解您的问题,但我最初的想法是您可以为动态创建的每个选项卡动态创建数据网格或类似的东西。然后您可以绑定网格的数据源,然后将网格作为控件添加到您的标签页。
Something like...
就像是...
DataGridView gv = new DataGridView();
gv.DataSource = //whatever your source is
this.tabPage1.Controls.Add(gv);
You would then have all the events associated with the grid to work with.
然后,您将拥有与网格关联的所有事件。
回答by Srikanth V M
Please refer the below link! You will get more detail in this regard.
请参考以下链接!您将获得这方面的更多详细信息。
Creating a tab control with a dynamic number of tabs in Visual Studio C#
回答by Travis
I'm thinking data binding is going to be your best bet for displaying this information. You can create a list of objects and use a DataTemplate to format the data. You can apply the DataTemplate to a quite a few objects. I generally use the ItemsControl and ListBox
我认为数据绑定将是显示此信息的最佳选择。您可以创建对象列表并使用 DataTemplate 来格式化数据。您可以将 DataTemplate 应用于相当多的对象。我一般使用 ItemsControl 和 ListBox
http://msdn.microsoft.com/en-us/library/ms750612.aspx
http://msdn.microsoft.com/en-us/library/ms750612.aspx
good luck
祝你好运