declare a form so that you can reference it's controls

  • Thread starter Thread starter Tanzen
  • Start date Start date
T

Tanzen

Here's what I'm trying to do in vb.net 2005.

I have a public declared method that sets the value of a control on a
form, but that form could change, so I don't want to code its actual
name. I want it to be a parameter in the module method so that you
pass the form name of whatever form called the method.

So let's say we call the method from form 1:

UpdateText(frmName)


Then in the module we have the actual method

Public Sub UpdateText(ByVal frmName as Form)

frmName.textbox1.text = "Hi, I'm Me."

end Sub

Now, the problem that arises is that frmName gets underlined by visual
studio and says: TextBox1 is not a member of
'System.Window.Forms.Form'. So apparently my parameter in the method
is wrong. But I can't find out what type the form should be. Can
anyone help me?

Thanks
 
TextBox1 is not a member of System.Windows.Forms.Form - it is a member
of the Form1 class, which is a subclass of System.Windows.Forms.Form.
Try this:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Utilities.SetControl(Me, \"ExampleText\")
End Sub
End Class

Public Class Utilities
Public Shared Sub SetControl(ByRef MyForm As Form1, ByVal
MyTextVal As String)
MyForm.TextBox1.Text = MyTextVal
End Sub
End Class

If you need the flexibility to set the text of forms that are of class
Form1, then pass a reference to the control instead of the form, like
this:

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Utilities.SetControl(Me.TextBox1, \\"ExampleText\\")
End Sub
End Class

Public Class Utilities
Public Shared Sub SetControl(ByRef MyTextBox As TextBox, ByVal
MyTextVal As String)
MyTextBox.Text = MyTextVal
End Sub
End Class

Thank you very much for the help. Most appreciated!
 
Back
Top