Forms - Editing Form text boxes from a class

All,

Vb.Net 2005.

Say i have 2 forms that both have a textbox called textbox1, how can i set to update this from a different class.

i was expecting something like .........

' Call from either for1 or form 2
editTheBox(me)

then in the different class ....

sub editTheBox(ByVal frm as windows.Forms.Form)

frm.TextBox1.text = "hello"

end sub

This (in hindsight) obviously did not work because it did not know what form to reference, and i became aware that the only way i could get this to work was to use something like .....

sub editTheBox(ByVal frm as Frm1)

frm.TextBox1.text = "hello"

end sub

The problem is this only works for form1 now, but i wanted it to be generic to work for form1 and form 2. (In the real app this will be referenced by 3 forms if i can get it working).

Any ideas, anyone ?

Thanks,

Daz



yo_daz_ukAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
How about something like:

    Public Shared Sub UpdateTextbox(ByVal frm As System.Windows.Forms.Form)

        Dim ctl() As System.Windows.Forms.Control
        ctl = frm.Controls.Find("TextBox1", True)

        If ctl.Length = 1 Then
            If TypeOf ctl(0) Is TextBox Then
                CType(ctl(0), TextBox).Text = "Hello world"
            End If
        End If

    End Sub
0
 
yo_daz_ukAuthor Commented:
Thats great cheers!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.