How to open a form in a determined tab? vb .net
I have a form with a tabcontrol and 4 tabs. I want to open a form with showdialog in a predetermined tab.
I've tried
OptionsForm.OPTS_TabControl1.SelectTab(1)
OptionsForm.OPTS_TabControl1.ShowDialog()
but it didn't work.
Any he开发者_开发问答lp? thanks
First Kudos for using Stackoverflow. It shows you paid attention to class =D
regarding your question, that piece of code you showed should be working. You should provide the actual error so we can try to figure out.
Does OptionsForm refer to the class or an object of a class you created?
Anyways, try to create an object of the form and then set the starting tab, like this:
Dim OptionsObject As New OptionsForm
OptionsObject.OPTS_TabControl1.SelectTab(1)
OptionsObject.OPTS_TabControl1.ShowDialog()
Another solution might be Overloading the Showdialog method, although it seems kind of an overshot.
Here's how: Inside your OptionsForm Code:
Public Overloads Sub Showdialog(ByRef TabNumber As Integer)
OPTS_TabControl1.SelectTab(TabNumber)
Return MyBase.ShowDialog()
then call the form using
optionsform.showdialog(1)
Note: Overloading is basically creating another instance of a subrotine that accepts different arguments. read the pages 342-358 of the manual if you wish to know more.
Since you are getting a NullReferenceException
, you should separate out the call to see where the null object is. Is the tab control null?.
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private tabControl1 As TabControl
Private tabPage1 As TabPage
Private tabPage2 As TabPage
Private Sub MyTabs()
Me.tabControl1 = New TabControl()
Me.tabPage1 = New TabPage()
Me.tabPage2 = New TabPage()
Me.tabControl1.Controls.AddRange(New Control() {Me.tabPage1, Me.tabPage2})
Me.tabControl1.Padding = New Point(15, 10)
Me.tabControl1.Location = New Point(35, 25)
Me.tabControl1.Size = New Size(220, 220)
' Selects tabPage2 using SelectedTab.
Me.tabControl1.SelectedTab = tabPage2
Me.tabPage1.Text = "tabPage1"
Me.tabPage2.Text = "tabPage2"
Me.Size = New Size(300, 300)
Me.Controls.AddRange(New Control() {Me.tabControl1})
End Sub
Public Sub New()
MyTabs()
End Sub
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
精彩评论