在Visual Studio C#中使用动态数量的选项卡创建选项卡控件

时间:2023-01-19 20:53:54

How to create a tab control with a dynamic number of tabs in Visual Studio C#?

如何在Visual Studio C#中使用动态数量的选项卡创建选项卡控件?

I've got a database with a table customers. I need to create a form that would show tabs with the first letters of customers' last name (only those first letters, for which there are entries in the table should be present). Each tab should contain a DataGrid control with the corresponding customers. I connect to the database using DataSet.

我有一个桌面客户数据库。我需要创建一个表单,显示客户姓氏的第一个字母(只有那些首字母,表中的条目应该存在)。每个选项卡都应包含与相应客户的DataGrid控件。我使用DataSet连接到数据库。

Where should I insert the code snippet that would generate such tabs? Can I do that with the existing tab control or should I create a custom control?

我应该在哪里插入会生成此类标签的代码段?我可以使用现有的选项卡控件执行此操作,还是应该创建自定义控件?

4 个解决方案

#1


You can generate dynamic tabs with the existing TabControl. Here is an example of how it can be done in a somewhat sort of pseudo code form...

您可以使用现有的TabControl生成动态选项卡。这是一个如何以某种伪代码形式完成它的例子......

TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;

foreach (Char c in lastNameList)
{
    TabPage tabPage = new TabPage();
    tabPage.Text = c.ToString();

    DataGrid grid = new DataGrid();

    grid.Dock = DockStyle.Fill;
    grid.DataSource = dataForTheCurrentLoop;

    tabPage.Controls.Add(grid);
    tabControl.Controls.Add(tabPage);
}

this.Controls.Add(tabControl);

#2


You would add the code to generate the tabs where you determine what letters are required to be shown, probably when you either retrieve the data or in the form's OnLoad() method. You should be able to dynamically add/remove tabs from the built-in tab control. You can check the designer code for some idea how to do it, or the docs.

您可以添加代码以生成选项卡,您可以在其中确定需要显示哪些字母,可能是在检索数据时或在表单的OnLoad()方法中。您应该能够从内置选项卡控件动态添加/删除选项卡。您可以查看设计器代码,了解如何操作或文档。

Note that it isn't necessarily a good idea to add a separate tab for each character. 26 tabs (which will happen when your database gets reasonably large) is a pretty awful number of tabs for someone to look through-- it won't necessarily make things faster at all.

请注意,为每个字符添加单独的选项卡不一定是个好主意。 26个选项卡(当你的数据库变得相当大时会发生这种情况)是一个非常可怕的选项卡供人们查看 - 它根本不会让事情变得更快。

Instead, consider providing a dynamic filtering mechanism, similar to the search box on Vista's start menu. Your user can type a single character (assuming you aren't writing some sort of kiosk or touch-screen-only software) and zoom immediately to the relevant names. This would work ideally with a ListView in List or Details mode.

相反,请考虑提供动态过滤机制,类似于Vista开始菜单上的搜索框。您的用户可以键入单个字符(假设您没有编写某种自助服务终端或仅限触摸屏的软件)并立即缩放到相关名称。理想情况下,这将在列表或详细信息模式下使用ListView。

#3


I don't remember the specifics now. But just look at the code in the XXX.designer.cs file for a form you have that contains a tab control. There you'll see the code generated to add a new tab. Just replicate those lines, you can add a new tab whenever you want.

我现在不记得具体细节了。但是,只需查看XXX.designer.cs文件中包含选项卡控件的表单中的代码即可。在那里,您将看到生成的代码以添加新选项卡。只需复制这些行,您可以随时添加新选项卡。

#4


It sounds like the best route for you would be to create your own custom tab control class. It could inherit from tab control for the bulk of its functionality and properties for the datagrid and whatever else custom you need. Then when you get your customers, you can create a tab for each letter you need and setup the corresponding properties.

听起来你最好的方法是创建自己的自定义选项卡控件类。它可以从标签控件继承其大部分功能和数据网格属性以及您需要的任何其他自定义。然后,当您获得客户时,可以为所需的每个字母创建一个选项卡,并设置相应的属性。

#1


You can generate dynamic tabs with the existing TabControl. Here is an example of how it can be done in a somewhat sort of pseudo code form...

您可以使用现有的TabControl生成动态选项卡。这是一个如何以某种伪代码形式完成它的例子......

TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;

foreach (Char c in lastNameList)
{
    TabPage tabPage = new TabPage();
    tabPage.Text = c.ToString();

    DataGrid grid = new DataGrid();

    grid.Dock = DockStyle.Fill;
    grid.DataSource = dataForTheCurrentLoop;

    tabPage.Controls.Add(grid);
    tabControl.Controls.Add(tabPage);
}

this.Controls.Add(tabControl);

#2


You would add the code to generate the tabs where you determine what letters are required to be shown, probably when you either retrieve the data or in the form's OnLoad() method. You should be able to dynamically add/remove tabs from the built-in tab control. You can check the designer code for some idea how to do it, or the docs.

您可以添加代码以生成选项卡,您可以在其中确定需要显示哪些字母,可能是在检索数据时或在表单的OnLoad()方法中。您应该能够从内置选项卡控件动态添加/删除选项卡。您可以查看设计器代码,了解如何操作或文档。

Note that it isn't necessarily a good idea to add a separate tab for each character. 26 tabs (which will happen when your database gets reasonably large) is a pretty awful number of tabs for someone to look through-- it won't necessarily make things faster at all.

请注意,为每个字符添加单独的选项卡不一定是个好主意。 26个选项卡(当你的数据库变得相当大时会发生这种情况)是一个非常可怕的选项卡供人们查看 - 它根本不会让事情变得更快。

Instead, consider providing a dynamic filtering mechanism, similar to the search box on Vista's start menu. Your user can type a single character (assuming you aren't writing some sort of kiosk or touch-screen-only software) and zoom immediately to the relevant names. This would work ideally with a ListView in List or Details mode.

相反,请考虑提供动态过滤机制,类似于Vista开始菜单上的搜索框。您的用户可以键入单个字符(假设您没有编写某种自助服务终端或仅限触摸屏的软件)并立即缩放到相关名称。理想情况下,这将在列表或详细信息模式下使用ListView。

#3


I don't remember the specifics now. But just look at the code in the XXX.designer.cs file for a form you have that contains a tab control. There you'll see the code generated to add a new tab. Just replicate those lines, you can add a new tab whenever you want.

我现在不记得具体细节了。但是,只需查看XXX.designer.cs文件中包含选项卡控件的表单中的代码即可。在那里,您将看到生成的代码以添加新选项卡。只需复制这些行,您可以随时添加新选项卡。

#4


It sounds like the best route for you would be to create your own custom tab control class. It could inherit from tab control for the bulk of its functionality and properties for the datagrid and whatever else custom you need. Then when you get your customers, you can create a tab for each letter you need and setup the corresponding properties.

听起来你最好的方法是创建自己的自定义选项卡控件类。它可以从标签控件继承其大部分功能和数据网格属性以及您需要的任何其他自定义。然后,当您获得客户时,可以为所需的每个字母创建一个选项卡,并设置相应的属性。