设置选定的选项卡而不选择selectedIndexChanged事件触发

时间:2012-04-26 11:01:55

标签: vb.net visual-studio-2008 tabcontrol

有没有办法在没有selectedIndexchanged事件触发的情况下设置tabControl的选定选项卡?

e.g。选择myTabPage2时会触发事件,因为myTabPage1是默认值:

Private Sub setupTabControl
    If blnHasAccount=true then
       MyTabControl.selectedTab=myTabPage1
    else
       MyTabControl.selectedTab=myTabPage2
    End if
End sub

Private Sub MyTabControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTabControl.SelectedIndexChanged
    'Do stuff (but only when user has actually changed the tab themselves)
End Sub

2 个答案:

答案 0 :(得分:1)

设置新标签页时,可以删除事件处理程序

Try
    RemoveHandler MyTabControl.SelectedIndexChanged, AddressOf Me.MyTabControl_SelectedIndexChanged
    .. do your works to change page here
    .. and then reconnect
Finally
    AddHandler MyTabControl.SelectedIndexChanged, AddressOf Me.MyTabControl_SelectedIndexChanged
End Try

将所有内容都包含在Try Finally中以便从异常中正确恢复

非常重要

答案 1 :(得分:-1)

可以伪造并检查ComboBox.Enabled作为在SelectedIndexChanged事件中使用的标志,同时进行更改,然后在完成后重新设置为true。然而,这些事件仍会触发(如果它们?),但至少可以确保全局'ComboBox'变量的状态。