Tab页控制离开
我有一个选项卡控件和 3 个选项卡页。 ( C#)
如果我在选项卡 2 中,并编辑文本框值 然后单击选项卡 3,我需要验证文本框中输入的内容。 如果正确,我应该允许切换到选项卡 3,否则应该保留在选项卡 2 中 我该如何实现这一目标?
iam 目前正在处理 tabpage2 的“leave”事件, 我验证那里的文本框值,如果发现无效 我设置为 tabcontrol.Selectedtab = tabpage2;这确实 验证但切换到新选项卡!我怎样才能限制导航。
我是 C# 新手,所以可能我正在处理错误的事件!
这是相关代码:
private void tabpage2_Leave(object sender, EventArgs e)
{
if (Validatetabpage2() == -1)
{
this.tabcontrol.SelectedTab =this.tabpage2;
}
}
I have a tab control and 3 tabpages in it. ( C#)
if i am in tab 2, and edit a text box value
and then click tab 3, i need to validate what was enetered in the text box.
if correct i should allow to to switch to tab 3 else should remain in tab 2 it self
how do i achieve this?
iam curently handling the "leave" event of the tabpage2,
i validate the text box value there and if found invalid
i set as tabcontrol.Selectedtab = tabpage2; this does
the validation but switches to new tab! how could i restrict the navigation.
I am a novice to C#, so may be i am handling a wrong event!
Here is the relevant code:
private void tabpage2_Leave(object sender, EventArgs e)
{
if (Validatetabpage2() == -1)
{
this.tabcontrol.SelectedTab =this.tabpage2;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然其他方法可能有效,但验证事件是专门为此设计的。
这是它的工作原理。当选项卡控件的 SelectedIndex 更改时,将焦点设置到新选择的页面,并设置 CausesValidation = true。这确保了如果用户尝试以任何方式离开选项卡,将调用 Validating 事件。
然后在页面特定的验证事件中进行正常验证,并根据需要取消。
您需要确保在“显示表单”事件中设置初始选定的选项卡页(Form_Load 将不起作用),并连接选项卡页特定的验证事件。
这是一个例子:
While the other approaches may work, the Validating event is designed specifically for this.
Here's how it works. When the SelectedIndex of the tab control changes, set the focus to the newly selected page and as well as CausesValidation = true. This ensures the Validating event will called if the user tries to leave the tab in any way.
Then do your normal validation in a page specific Validating event and cancel if required.
You need to make sure to set the initial selected tab page in the Form Shown event (Form_Load will not work) and also wire up the tab page specific validating events.
Here's an example:
您可以使用 TabControl Selecting 事件来取消切换页面。在事件中将 e.Cancel 设置为 true 会阻止选项卡控件选择其他选项卡。
You can use the TabControl Selecting event to cancel switching pages. Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab.