TabPage 验证单击当前选定选项卡时触发的事件

发布于 2024-08-30 13:29:10 字数 702 浏览 9 评论 0原文

我正在做 如何防止用户更改 TabControl 中选定的选项卡页?

一切正常。但是,如果我当前选择了 tabpage1 并且用户单击 tabpage1 本身,则会发生 tabpage1 的验证事件。稍后,当用户单击 tabpage2 时,tabpage1 的验证事件不会触发。

发生的情况是,如果我在 tabpage1 的验证事件中执行 e.Cancel ,在上述情况下,当用户错误地单击 tabpage1 且已经选择了 tabpage1 时,它会提示用户“您想留下吗?”在当前选项卡上保存数据或从当前选项卡移动?”。如果用户点击Stay但不进行任何更改。然后,当他正确单击 tabpage2 时,tabpage1 的 Validating 事件不会触发。

我已在此处上传了示例应用程序。您可以运行并查看行为以正确理解问题

I'm doing things as said in How do I prevent the user from changing the selected tab page in a TabControl?

Things are working fine. But the validating event of tabpage1 occurs if I've tabpage1 is currently selected and user clicks on tabpage1 itself. and later when user clicks on tabpage2 validating event for tabpage1 doesn't fire.

What happens is if I do e.Cancel in validating event of tabpage1, in the above case, when user clicks on tabpage1 by mistake having tabpage1 already selected, it will prompt user that "Do you want to stay on current tab to save data or move from the current tab?". and if user clicks Stay but doesn't do any changes. And then when he correctly clicks tabpage2, Validating event of tabpage1 is not firing.

I've uploaded the sample application here. You can run and see the behavior to properly understand the problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2024-09-06 13:29:10

使用 TabControl.Selecting 事件 代替。

像这样使用它:

tabControl1.Selecting += tabControl1_Selecting;

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    e.Cancel = !(can switch tab);
}

Use TabControl.Selecting Event instead.

Use it like this:

tabControl1.Selecting += tabControl1_Selecting;

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    e.Cancel = !(can switch tab);
}
倥絔 2024-09-06 13:29:10

这与验证应该的行为很相似,只是与标签页的匹配不太幸运。

您可以使用 Selecting/Selected 事件并自己编写更多逻辑。或者放弃选项卡页面或单独验证它们的想法。

This is kinf of how validating should behave, it's just not a lucky match with tabpages.

You could use the Selecting/Selected events and write some more logic yourself. Or give up on either tabpages or the idea that they are to be validated individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文