循环遍历 TabControl 中的控件
我正在寻找一种方法来循环浏览选项卡控件的特定选项卡上的控件。例如,我有一个带有以下选项卡的选项卡控件:
汽车, 宠物, 管理
在每个选项卡上都有几个用于显示/编辑/保存数据等的控件。在“保存”按钮上,我想循环浏览该特定选项卡的控件以检查是否已填写所有必填字段。
所以,如果我在“汽车”选项卡上并单击“保存”,我只想循环浏览“汽车”选项卡上的控件,而不是“宠物”或“管理”选项卡上的控件。
怎样才能达到这样的结果呢?
I am looking for a way to loop through controls on a particular tab of a tabcontrol. For example, I have a tabcontrol with the following tabs:
Cars,
Pets,
Admin
On each of these tabs are several controls to display/edit/save data, etc. On the "Save" button, I would like to loop through the controls for that particular tab to check whether all required fields have been filled in.
So, if I am on the Cars tab and click "Save," I want to loop ONLY through the controls on the Cars tab and NOT the Pets or Admin tabs.
How can achieve this result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
至于循环遍历 TabControl 的控件,您需要使用 Controls 属性。
这是关于 TabControl 的 MSDN 文章。
例子:
As for looping through a TabControl's controls, you need to use the Controls property.
Here's an MSDN article on the TabControl.
Example:
我认为值得注意的是,一般来说,您应该对您的应用程序采取更结构化的方法。例如,不是将所有控件都放在三个选项卡页上,而是在每个选项卡页上仅包含一个 UserControl。例如,
CarUserControl
、PetUserControl
和AdminUserControl
然后每个用户控件都知道如何创建正确的各自数据结构,因此您无需手动创建使用选项卡间循环等在同一抽象级别将所有内容组合在一起。这种关注点分离将使您的程序更容易推理,并且是为您未来的职业编写可维护代码的良好实践。
I feel it's important to note that, in general, you should take a more structured approach to your application. E.g., instead of having all the controls on three tab pages, include exactly one UserControl on each tabpage. A
CarUserControl
,PetUserControl
, andAdminUserControl
e.g. Then each user control knows how to create the proper respective data structure so you don't have to manually munge it all together at the same level of abstraction using inter-tab loops and whatnot.Such a separation of concerns will make it much easier to reason about your program and is good practice for writing maintainable code for your future career.
我想在我编写的应用程序的特定选项卡中获取 DataGridView 的示例。
Example where I wanted to get the DataGridView in a particular tab for an application I wrote.
Controls
属性是正确的选择...The
Controls
property is the way to go...TabControl 有一个 SelectedTab 属性,因此您可以执行以下操作:
TabControl has a SelectedTab property, so you'd do something like this:
我还需要禁用或启用选项卡的控件。不过,我不得不更通用一些。希望它对人们有帮助,我没有犯错误
I had the need to disable or enable controls of a tab as well. I had to go a bit more generic though. Hope it helps people and I didn't make a mistake