lichSkywalker
lichSkywalker

Reputation: 45

How to display a particular tab from another form in vb.net

I am working on a vb.net project in VS 2010 that has multiple forms. I have, lets say, Form1 with a Button and Form2 with a TabControl.

How do i display a particular tab(eg.tab2 or tab3) when the user clicks on the button from form1.

Upvotes: 0

Views: 4316

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

To change the selected tab of a TabControl you can use SelectTab method or SelectedIndex property of your tab control.

Controls of a form are not accessible from outside of the form by default. You can let access to a control of a form by setting the Modifiers property of control to Public and GenerateMember property to true.

So go to designer of Form2, select your tab control, in properties window, set GenerateMember to true and set Modifier to be Public. Then you can access your tab control from Form1:

Dim f as New Form2()
f.TabControl1.SelectedIndex = 1 'It selects second tab
f.ShowDialog()

Note

In general as a good design guideline it's better not to expose your forms controls. In such case it's better to create property or method in your Form2 and use it to get/set selected tab of Form2 from Form1. For example you can see Munawar's answer.

Upvotes: 1

Munawar
Munawar

Reputation: 2587

You may create a public method on form with tab controls

Public void TabSelection( int tabIndex)
{

MyTabControl.SelectedTab =MyTabControl.TabPages[tabIndex];
}

VB.Net Code:

 Public Sub TabSelection(ByVal tabIndex As Integer)
        MyTabControl.SelectedTab = MyTabControl.TabPages(tabIndex)
    End Sub

Call above method from button click handler from other forms.

Upvotes: 0

Related Questions