Relate Windows Forms

For a long time I had this problem, so now how to solve that I share with you.

If from one form to another form called windows windows either property. Show or. ShowDialog until I close the second form I have no control of the main form ... unless these two forms are "related". Consider a simple example to understand the concept and how to solve it.

The image is clearly a main form with a button "move red panel" and a red panel. Pressing fomrulario open one second with four buttons (up, down, left and right). Pressing either button that does is move the red panel in the direction indicated.

Formulariosconectados

see the code of Form1

 Public Class Form1
     Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         As New Form2 Dim vfrmForm2
         vfrmForm2.formulario1 = I
         vfrmForm2.ShowDialog ()
     End Sub
 End Class

and code of Form2

 Public Class Form2

     Form1 As Form1 Public

     BtnArriba_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArriba.Click
         formulario1.Panel1.Top formulario1.Panel1.Top = - 1
     End Sub

     BtnDerecha_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDerecha.Click
         formulario1.Panel1.Left = formulario1.Panel1.Left + 1
     End Sub

     BtnAbajo_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbajo.Click
         formulario1.Panel1.Top = formulario1.Panel1.Top + 1
     End Sub

     BtnIzquierda_Click Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIzquierda.Click
         formulario1.Panel1.Left formulario1.Panel1.Left = - 1
     End Sub
 End Class

As you can see does not take much code to "connect" the forms. Moreover, we need only one extra line on each form. In Form2 I will highlight the following statement: Public form1 As Form1 and Form1 vfrmForm2.formulario1 = Me. This causes the Form1 instantiated in memory is linked to declared Form2 form1. From here and I can relate and work with any Form1 control, such as simply calling it formulario1.panel1 panel1.

I hope you find it so useful with it is for me.

3 Responses to "Windows Forms Relate"

  1. Josel said:

    Very good, also if you have chance you could explain how to handle the MDI Form

    Salduso

  2. romina says:

    the buttons in the second window samples can be assigned movement keys?

  3. leonel says:

    this interesting, but that button corresponds to the red box located in the from1, pq does not work example.

Comment