Problems writing to the StatusBar form a Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.StatusBar1.' 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.StatusBar1.Text = "Item " + CStr(Indx + 1) + " of " +
CStr(MyCount)
End Sub
 
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.StatusBar1.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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim c1 As New Class1(Me)
c1.setstatus()
c1 = Nothing
End Sub
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top