Search code examples
.netvb.netcontrolstoolstrip

Unable to update ToolStrip using class file


Here is a simple example of what I am doing in the real application

FORM CONTAINING MAIN SUB OpenNew.vb

Step 1 - Main prompts user for input using dialog in same file (OpenNew.vb) Step 2 - Users Selects What type of Project they are opening, Type 1 or Type 2 Step 3 - strOpenNewResponse is populated when OpenNew dialog is closed Step 3 - Form is opened based on selection

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 would have a StatusBar with one ToolStrip and a TreeView:

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

Then the Class file is named 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

When I do this the ToolStrip Item never updates. It just stays the default value.

Of Course if I run it this way (without the class)

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

I thought it might be a problem with some sort of Public/Private conflict but I still can't seem to figure it out.

I might just be blinded by the obvious at this point

Can anyone give me an idea what might be causing this?

ADDED A SCREENSHOT for soohoonigan

enter image description here


Solution

  • formToShow = Form1 will work (remove the New)

    Your Utilities class is referencing Form1 directly, but you have just an instance of that. Alternatively, if you need multiple instances of Form1 you could pass in the form to the Sub like this:

    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
    

    and call it like such:

    Utilities.DisplayStatus(Me, "Loading, Please Wait...")