以下是我在实际应用程序中所做的一个简单示例
包含主要SUB的格式OpenNew.vb
步骤1 - 主要使用同一文件中的对话框提示用户输入(OpenNew.vb) 第2步 - 用户选择他们打开的项目类型,类型1或类型2 步骤3 - 关闭OpenNew对话框时填充strOpenNewResponse 第3步 - 根据选择打开表格
Public Class OpenNew
Public Shared strOpenNewResponse As String = Nothing
Public Shared Sub Main()
OpenNew.ShowDialog()
If strOpenNewResponse IsNot Nothing Then
Dim formToShow As Form = Nothing
Select Case strOpenNewResponse
Case "Type1"
formToShow = New Form1
formToShow.ShowDialog()
Case "Type2"
formToShow = New Form2
formToShow.ShowDialog()
End Select
End If
End Sub
End Class
Form1将有一个带有一个ToolStrip和一个TreeView的StatusBar:
Public Class Form1
Inherits Form
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Utilities.DisplayStatus("Loading, Please Wait...")
Me.Cursor = Cursors.WaitCursor
PopulateTreeView("Root Node")
Utilities.DisplayStatus("Process Complete")
Me.Cursor = Cursors.Default
End Sub
End Class
然后Class文件名为Utilities.vb
Public Class Utilities
Public Shared Sub DisplayStatus(ByVal strStatusMessage As String)
Form1.toolstripDisplayStatus.Text = strStatusMessage
Form1.toolstripDisplayStatus.Visible = True
Form1.statusstripParent.Refresh()
End Sub
End Class
当我这样做时,ToolStrip项永远不会更新。它只是保持默认值。
当然,如果我这样运行(没有课程)
Public Class Form1
Inherits Form
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Cursor = Cursors.WaitCursor
toolstripDisplayStatus.Text = "Loading, Please Wait..."
toolstripDisplayStatus.Visible = True
statusstripParent.Refresh()
PopulateTreeView("Root Node")
toolstripDisplayStatus.Text = "Process Complete"
toolstripDisplayStatus.Visible = True
statusstripParent.Refresh()
Me.Cursor = Cursors.Default
End Sub
End Class
我认为这可能是某种公共/私人冲突的问题,但我似乎无法弄明白。
我可能只是被这一点上的明显所蒙蔽
任何人都可以告诉我可能导致这种情况的原因吗?
为soohoonigan添加了一个屏幕
答案 0 :(得分:1)
formToShow = Form1
将有效(删除新内容)
您的Utilities类直接引用Form1,但您只有一个实例。或者,如果您需要Form1的多个实例,则可以将表单传递给Sub,如下所示:
Public Shared Sub DisplayStatus(ByVal temp As Form1, ByVal strStatusMessage As String)
temp.toolstripDisplayStatus.Text = strStatusMessage
temp.toolstripDisplayStatus.Visible = True
temp.statusstripparent.Refresh()
End Sub
并称之为:
Utilities.DisplayStatus(Me, "Loading, Please Wait...")