How to use controls on another Form the real easy way***

dynamic_sysop

Well-known member
Joined
Oct 1, 2002
Messages
1,039
Location
Ashby, Leicestershire.
Hi guys , ive been working on an easier way to load a form and send info back to the starting form / another form. theres no need to modify the Sub New really , this is all you need to do :
from the form you already have open to call the second form :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frmKid As New Form3()
        Me.AddOwnedForm(frmKid)
        frmKid.Show()
    End Sub

from the second form ( eg: Form2 ) to set some text in Form1s Textbox :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frmBoss As Form1 = Me.Owner

        frmBoss.TextBox1.Text = "test" ///put some text in Form1s textbox.
    End Sub

i think this may save a bit of messing for people :)
 
I tried I got an error message . An unhandled exception of type System.InvalidCastException occurred in WindowsApplication1.exe
Additional information: Specified cast is not valid.
 
Hi,

I really dont think this is the right answer in .NET. Maybe it would be correct in vb6 but this is totally not compatible with the OO - language where .NET stands for.
To do it in a more .NET way I suggest you create some events for setting some text in the second form.
example : Public Event MustSetText(Sender as object, Msg as string)

In the button click event off your second form raise the event :
Raise event MustSetText(Me, "Hi from form2!")

Then catch the event in the first form

On_Form2_MustSetText(Sender as object, Msg as string)
Me.textBox1.text = Msg
 
Back
Top