Question

I have literally just started programming VB today; so forgive my incompetence.

I currently have two window forms, one to navigate though different pages via TabControl (Which is called BusinessSalesPage.vb) and the other is separate (BusinessQuestion.vb). The second window form opens when a button is pressed on BusinessSalesPage.vb.

When the second window opens it has two buttons, I would like the user to be able to click one button which then takes them to the third tab from the TabControl that is on the first window (BusinessSalesPage.vb). Thanks in advance.

Here is my code:

Public Class BusinessQuestion
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl
Friend WithEvents TabPage3 As System.Windows.Forms.TabPage

Private Sub yesButn_Click(sender As Object, e As EventArgs) Handles yesButn.Click

    mainMenu.SelectedTab = TabPage3()

End Sub
End Class

Here is the error I am getting:

NullReferenceException was handled - Object reference not set to an instance of an object.

Edit

Public Class BusinessSalesPage
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mainMenu.SelectedTab = TabPage2
    BusinessQuestion.Show()

End Sub
End Class
Was it helpful?

Solution 2

You need a reference to the actual instance of the mainForm. You currently don't.

You can try passing a reference in the constructor:

Public Class BusinessQuestion
  ' code...
  Public Sub New(mainMenu As TabControl, tabPage3 As TabPage)
    InitializeControls()
    Me.mainMenu = mainMenu
    Me.TabPage3 = tabPage3
  End Sub
  ' code...
End Class

Your BusinessSalesPage should probably look something like this:

Public Class BusinessSalesPage
  Inherits System.Windows.Forms.Form

  Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mainMenu.SelectedTab = TabPage2

    Dim bq As New BusinessQuestion(mainMenu, TabPage3)
    bq.ShowDialog()        
  End Sub
End Class

OTHER TIPS

@LarsTech I tried this as well but mine is having issues with the code. On First Form when I type the mainMenu.SelectedTab = TabPage 1 I get error: "SelectedTab is not a member of System.Windows.Forms.MainMenu and TabPage1 is not declared. It may be inaccessible due to its protection level. On Second Form it doen not find InitializeControls, only InitializeComponents. I am in VS 2010 and here is my code:

First Form:

Public Class WCC
  Inherits System.Windows.Forms.Form

  Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mainMenu.SelectedTab = TabPage1

    Dim MSO As New MSO2014CC(mainMenu:=, TabPage1:=)
    MSO.ShowDialog()        

  End Sub
End Class

Second Form:

Public Class MSO2014CC
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl

  Public Sub New(mainMenu As TabControl, tabPage1 As TabPage)
    InitializeComponent()
    Me.mainMenu = mainMenu
    Me.TabPage1 = tabPage1

  End Sub
End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top