Link to home
Start Free TrialLog in
Avatar of JackW9653
JackW9653Flag for United States of America

asked on

Move to specific tab page based on user selection from combo box

Hello experts,

I want to direct users to a specific tab page on the same windows form based on their selection from a combo box. For instance if they select "Reschedule" from the drop list I want the tab page with the scheduler to open. I'm using VB.Net 2003.
Thanks,

Jack
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Here is a shot program I put together to show you have to do thins.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage3 As System.Windows.Forms.TabPage
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.TabPage3 = New System.Windows.Forms.TabPage
Me.TabControl1.SuspendLayout()
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.Items.AddRange(New Object() {"Function 1", "Scheduler", "Function 3"})
Me.ComboBox1.Location = New System.Drawing.Point(88, 216)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 0
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage3)
Me.TabControl1.Location = New System.Drawing.Point(8, 8)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(272, 184)
Me.TabControl1.TabIndex = 1
'
'TabPage2
'
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(264, 158)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "Function 1"
'
'TabPage1
'
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(264, 158)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "Scheduler"
'
'TabPage3
'
Me.TabPage3.Location = New System.Drawing.Point(4, 22)
Me.TabPage3.Name = "TabPage3"
Me.TabPage3.Size = New System.Drawing.Size(264, 158)
Me.TabPage3.TabIndex = 2
Me.TabPage3.Text = "Function 3"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TabControl1)
Me.Controls.Add(Me.ComboBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TabControl1.ResumeLayout(False)
Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        ' The text on the tab must match the text in the combo box.
        ' This way if the order of the tabs are changed this will still
        ' work.
        For Each tp As TabPage In TabControl1.TabPages
            If ComboBox1.Text = tp.Text Then
                TabControl1.SelectedTab = tp
            End If
        Next

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.SelectedIndex = 0

    End Sub
End Class
Avatar of JackW9653

ASKER

Hello Fernando, thanks for the quick reply. Is there a way to do this when the tab page text doesn't match the selection? Could I use the index of the tab control page?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works Perfectly! Thanks Fernando.

Jack
Glad I could help.

Fernando