Urbycoz Urbycoz - 3 months ago 15
Vb.net Question

Set selected tab without selectedIndexChanged event firing

Is there a way to set the selected tab of a tabControl without the selectedIndexchanged event firing?

e.g. Here the event fires when selecting myTabPage2 because myTabPage1 is the default:

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

Answer

When you set the new tabpage you could remove the event handler

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

It's important to enclose everything in a Try Finally to recover correctly from exceptions