JimLaydon JimLaydon - 3 months ago 13
Vb.net Question

Pass a form name to a function in a module

I need to reference a textbox on a form from a module. The following works great except I have to Hard Code the form name (frmForm1). I am using this from several forms. How can I have the function use the form name I am sending (Me)?

I call the function, and I would like to send the form name, from my form:

ModuleFunction(Me)


In the Module I have this Function:

Public Function ModuleFunction(ByRef frmFrom As frmForm1) As Boolean

frmFrom.txtTextBox1.Text = "something"
......

End Function

Answer

Get around this by using Controls instead:

Public Function ModuleFunction(ByRef frmFrom As Form) As Boolean

    frmFrom.Controls("txtTextBox1").Text = "something"
    ......

End Function

You should be able to pass in a frmForm1 directly because it inherits from Form. But as soon as you tried to access the member txtTextBox1 it wouldn't compile, because Form has no knowledge of that specific member - only its child does. But Form does have a defined Controls collection containing all of these control members, allowing you to access your control through the magic of polymorphism.

Comments