Problems writing to the StatusBar form a Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GrandpaB

    #1

    Problems writing to the StatusBar form a Class

    HI,
    I am attempting to write to the StatusBar on MyForm from within a class that
    I wrote. In the following code, VB does not like 'MyForm.StatusB ar1.' VB
    states, ' Reference to a non-shared member requires an object reference.'
    Can you explain what VB is telling me and how can I resolve this problem.
    I'm relatively new to OOP and .NET. Thanks.
    Private Sub StatusUpdate()
    MyForm.StatusBa r1.Text = "Item " + CStr(Indx + 1) + " of " +
    CStr(MyCount)
    End Sub

    --
    GrandpaB
  • Jerry H.

    #2
    Re: Problems writing to the StatusBar form a Class

    the long and short of it is that your Child form has no access to the
    Statusbar on "Myform"

    Here is one way to to it:

    Child class (your class that needs to set the Statusbar). There are
    two subroutines here: "New" receives the name of the object that has
    the statusbar, and the LocalfrmMain object is set to reference it. In
    the "setstatus" subroutine, you can set the statusbar to whatever you
    like


    Public Class Class1
    Dim LocalfrmMain As Form1
    Public Sub New(ByRef MyOwner As Object)
    LocalfrmMain = MyOwner
    End Sub
    Public Sub setstatus()
    LocalfrmMain.St atusBar1.Text = Now.ToString
    End Sub
    End Class


    In MyForm (which has the status bar), when you declare your class, pass
    the word "Me" to it

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim c1 As New Class1(Me)
    c1.setstatus()
    c1 = Nothing
    End Sub

    Comment

    • GrandpaB

      #3
      Re: Problems writing to the StatusBar form a Class

      Jerry,

      Thanks, that did the trick. If you don't mind, I'd appreciate a reference
      or explaination of what you did to Improve my knowledge and understanding.
      --
      GrandpaB

      Comment

      Working...