setting a form property value from a class?

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

Guest

--button on the form
Private Sub btnClsTest_Click(...) Handles btnClsTest.Click
Dim a As New clsTest
End Sub

-------------------------------------------------------
'--property on my form
Public Property captionText() As String
Get

End Get
Set(ByVal Value As String)
Me.Text = Value
End Set
End Property
------------------------------------------------

Public Class clsTest
Public Sub New()
Dim frm As New Form1
frm.captionText = "test from clsTest"
End Sub
End Class

How can I make the text "test from clsTest" in the class constructor show
up in the caption bar of the form that instantiates this class?

Thanks,
Rich
 
Hi,

You need to pass a reference to the form to the class. In your
example you created a new form1 and changed the captiontext. The form was
never shown. Try something like this instead.

Public Class clsTest
Public Sub New(frm as form1)
frm.captionText = "test from clsTest"
End Sub
End Class


Ken
-------------------------
--button on the form
Private Sub btnClsTest_Click(...) Handles btnClsTest.Click
Dim a As New clsTest
End Sub

-------------------------------------------------------
'--property on my form
Public Property captionText() As String
Get

End Get
Set(ByVal Value As String)
Me.Text = Value
End Set
End Property
------------------------------------------------

Public Class clsTest
Public Sub New()
Dim frm As New Form1
frm.captionText = "test from clsTest"
End Sub
End Class

How can I make the text "test from clsTest" in the class constructor show
up in the caption bar of the form that instantiates this class?

Thanks,
Rich
 
OK. That was the trick I was forgetting - to pass the form in as an
argument. Man, I wasn't thinking about "New" that is not the same instance
of the original form. Now it works correctly.

Thanks very much for your help.
Rich
 
In my not so humble opinion the proper way for dealing with these types of
situations is via event handlers. Declare an event in your class and have the
form be the consumer of the event. That way you can have the form determine
how it wants to react to whatever happened in your class, as opposed to the
class controlling the form's behavior.
 
Back
Top