• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3363
  • Last Modified:

How to fire tabControl.SelectedIndexChanged when form is loaded?

Hi Experts,

I have a form that has a tabControl with 2 tabs on it.

I have some code in the tabControl.SelectedIndexChanged event.

But when the form loads the tabControl.SelectedIndexChanged event does not fire.

How can I fire the tabControl.SelectedIndexChanged event (effectively clicking the first tab) when the form loads?

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
In the Load() event, set SelectedIndex to -1, then back to 0 (zero).

Make sure your code can handle the -1 value though:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TabControl1.SelectedIndex = -1
        TabControl1.SelectedIndex = 0
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If TabControl1.SelectedIndex <> -1 Then
            Debug.Print("SelectedIndexChanged: " & TabControl1.SelectedIndex)
        End If
    End Sub

End Class

Open in new window

LeighWardleAuthor Commented:
Idle_Mind - a brilliant solution!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now