3 Vb.net 表格 - 我可以从一个表格中获取标题,但不能从另一个表格中获取标题?
3 Vb.net Forms - I Can get the title from one but not the other?
我的解决方案文件中有 3 个表单。每个里面都有一个Publicclass
(Form1 - MainControlWindow Class), (Form3 - ShaneTestFormVersion2 Class), (New_Popup_Chords - New_Popup_Chords Class)
程序从 MainControlWindow 的 Form1 开始 Class。我给它一个标题名称,然后将第二个表单 ShaneTestFormVersion2 class 调暗为 frmSongDisplay2 并显示它。
Me.Text = "Window_A"
Dim frmSongDisplay2 As New ShaneTestFormVersion2
frmSongDisplay2.Show()
然后,在 Form3.vb 页面的 ShaneTestFormVersion2 class 中,我将最后一个表单调暗为对话框,以便第二个表单在打开时停止处理:
Me.Text = "Window_B"
Dim NewChordsPopup As New New_Popup_Chords
NewChordsPopup.ShowDialog()
在我尝试使用第 3 种形式 (NewChordsPopup) 显示其他 2 种的 window 标题之前,这一切都很好而且花花公子:
MsgBox(ShaneTestFormVersion2.Text) 'Second Form opened
MsgBox(MainControlWindow.Text) 'First Form Opened
消息框作为默认 window 标题显示为 "Form3" 而不是 "Window_B" , 但第一个 window msgbox 显示为 "Window_A".
所有标题都以相同方式设置,windows 变暗并以相同方式显示。为什么 Form3 中的 ShaneTestFormVersion2 出现 "Form3" 而不是我分配的标题 "Window_B"?
旁注:
我还注意到我可以很好地访问 MainControlWindow 变量 (MainControlWindow.Var1) (Public Var1) 但无法访问 ShaneTestFormVersion2 变量 (ShaneTestFormVersion2.Var1) 除非它是 "Public Shared Var1"。
您需要传递参考。
在您的 ShaneTestFormVersion2 表单中添加构造函数:
Public Class ShaneTestFormVersion2
Private mainForm As Form
Public Sub New(mainForm as Form)
InitializeComponent()
Me.mainForm = mainForm
End Sub
那么在你的代码中,它将是:
MessageBox.Show(mainForm.Text)
或者您可以使用所有者 属性:
frmSongDisplay2.Show(Me)
然后在 ShaneTestFormVersion2 表单中,您将使用:
MessageBox.Show(Me.Owner.Text)
我认为在您的主窗体中发生了什么,您将新窗体声明为私有窗体,因此其他窗体将无法相互看到。将表单声明为 Friend 并像下面这样引用它
尝试更改您的声明..
Dim frmSongDisplay2 As New ShaneTestFormVersion2
到..
Friend frmSongDisplay2 As New ShaneTestFormVersion2
和..
MsgBox(ShaneTestFormVersion2.Text) 'Second Form opened
到..
MsgBox(MaincontrolWindow.frmSongDisplay2.Text) 'Second Form opened
我的解决方案文件中有 3 个表单。每个里面都有一个Publicclass
(Form1 - MainControlWindow Class), (Form3 - ShaneTestFormVersion2 Class), (New_Popup_Chords - New_Popup_Chords Class)
程序从 MainControlWindow 的 Form1 开始 Class。我给它一个标题名称,然后将第二个表单 ShaneTestFormVersion2 class 调暗为 frmSongDisplay2 并显示它。
Me.Text = "Window_A"
Dim frmSongDisplay2 As New ShaneTestFormVersion2
frmSongDisplay2.Show()
然后,在 Form3.vb 页面的 ShaneTestFormVersion2 class 中,我将最后一个表单调暗为对话框,以便第二个表单在打开时停止处理:
Me.Text = "Window_B"
Dim NewChordsPopup As New New_Popup_Chords
NewChordsPopup.ShowDialog()
在我尝试使用第 3 种形式 (NewChordsPopup) 显示其他 2 种的 window 标题之前,这一切都很好而且花花公子:
MsgBox(ShaneTestFormVersion2.Text) 'Second Form opened
MsgBox(MainControlWindow.Text) 'First Form Opened
消息框作为默认 window 标题显示为 "Form3" 而不是 "Window_B" , 但第一个 window msgbox 显示为 "Window_A".
所有标题都以相同方式设置,windows 变暗并以相同方式显示。为什么 Form3 中的 ShaneTestFormVersion2 出现 "Form3" 而不是我分配的标题 "Window_B"?
旁注:
我还注意到我可以很好地访问 MainControlWindow 变量 (MainControlWindow.Var1) (Public Var1) 但无法访问 ShaneTestFormVersion2 变量 (ShaneTestFormVersion2.Var1) 除非它是 "Public Shared Var1"。
您需要传递参考。
在您的 ShaneTestFormVersion2 表单中添加构造函数:
Public Class ShaneTestFormVersion2
Private mainForm As Form
Public Sub New(mainForm as Form)
InitializeComponent()
Me.mainForm = mainForm
End Sub
那么在你的代码中,它将是:
MessageBox.Show(mainForm.Text)
或者您可以使用所有者 属性:
frmSongDisplay2.Show(Me)
然后在 ShaneTestFormVersion2 表单中,您将使用:
MessageBox.Show(Me.Owner.Text)
我认为在您的主窗体中发生了什么,您将新窗体声明为私有窗体,因此其他窗体将无法相互看到。将表单声明为 Friend 并像下面这样引用它
尝试更改您的声明..
Dim frmSongDisplay2 As New ShaneTestFormVersion2
到..
Friend frmSongDisplay2 As New ShaneTestFormVersion2
和..
MsgBox(ShaneTestFormVersion2.Text) 'Second Form opened
到..
MsgBox(MaincontrolWindow.frmSongDisplay2.Text) 'Second Form opened