Forms - Editing Form text boxes from a class

Posted on 2006-04-12
Last Modified: 2010-04-23

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

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 ?



Question by:yo_daz_uk
    LVL 52

    Accepted Solution

    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

    Author Comment

    Thats great cheers!!!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now