检查是否选择了特定选项卡页(活动)
我正在创建一个事件来检查选项卡控件中的特定选项卡页是否处于活动状态。
重点是,如果选项卡控件中的选项卡页是当前选定的选项卡,则会触发事件。有什么代码可以满足我的需要吗?
I am making an event to check if specific tab page in a tab control is active.
The point is, it will trigger an event if that tab page in a tab control is the currently selected tab. Any code that will give me what I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设您正在 Winform 中查看,该选项卡有一个
SelectedIndexChanged
事件,现在您可以在其中检查特定选项卡并继续执行逻辑
Assuming you are looking out in Winform, there is a
SelectedIndexChanged
event for the tabNow in it you could check for your specific tab and proceed with the logic
检查特定选项卡页是否是选项卡控件当前选定的页面很容易;只需使用选项卡控件的 SelectedTab 属性:
如果代码是基于除所选选项卡页之外的某些事件执行的,则这会更有用(在这种情况下,SelectedIndexChanged 将是更好的选择)。
例如,我有一个应用程序,它使用计时器定期通过 TCP/IP 连接轮询内容,但为了避免不必要的 TCP/IP 流量,我只轮询更新当前选定选项卡页面中的 GUI 控件的内容。
To check if a specific tab page is the currently selected page of a tab control is easy; just use the SelectedTab property of the tab control:
This is more useful if the code is executed based on some event other than the tab page being selected (in which case SelectedIndexChanged would be a better choice).
For example I have an application that uses a timer to regularly poll stuff over TCP/IP connection, but to avoid unnecessary TCP/IP traffic I only poll things that update GUI controls in the currently selected tab page.
在.Net 4中可以使用
OR
in .Net 4 can use
OR
无论出于何种原因,上述内容对我不起作用。这就是所做的:
其中 tabControl.SelectedTab.Name 是分配给 tabcontrol 本身页面的名称属性。
For whatever reason the above would not work for me. This is what did:
where tabControl.SelectedTab.Name is the name attribute assigned to the page in the tabcontrol itself.
我认为使用事件
tabPage1.Enter
更方便。当不同选项卡有不同的逻辑时,这比嵌套 if-else 语句更好。并且更适合将来添加新选项卡的情况。
请注意,如果加载表单并且默认打开
tabPage1
,则会触发此事件。I think that using the event
tabPage1.Enter
is more convenient.This is better than having nested if-else statement when you have different logic for different tabs. And more suitable in case new tabs may be added in the future.
Note that this event fires if the form loads and
tabPage1
is opened by default.这也可以。
This can work as well.