1

我今天才刚刚开始编写 VB。所以请原谅我的无能。

我目前有两种窗口形式,一种通过 TabControl 浏览不同的页面(称为 BusinessSalesPage.vb),另一种是独立的(BusinessQuestion.vb)。当在 BusinessSalesPage.vb 上按下按钮时,将打开第二个窗口窗体。

当第二个窗口打开它有两个按钮时,我希望用户能够单击一个按钮,然后将它们从第一个窗口 (BusinessSalesPage.vb) 上的 TabControl 带到第三个选项卡。提前致谢。

这是我的代码:

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

这是我得到的错误:

已处理 NullReferenceException - 对象引用未设置为对象的实例。

编辑

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
4

2 回答 2

1

@LarsTech我也试过这个,但我的代码有问题。在第一个表单上,当我键入 mainMenu.SelectedTab = TabPage 1 时出现错误:“SelectedTab 不是 System.Windows.Forms.MainMenu 的成员,并且未声明 TabPage1。由于其保护级别,它可能无法访问。在第二个表单上它没有找到 InitializeControls,只有 InitializeComponents。我在 VS 2010 中,这是我的代码:

第一种形式:

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

第二种形式:

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
于 2013-07-31T14:03:59.773 回答
0

您需要对 mainForm 的实际实例的引用。你目前没有。

您可以尝试在构造函数中传递引用:

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

您的 BusinessSalesPage 应该看起来像这样:

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
于 2013-04-01T18:44:49.937 回答