simple question regarding VB.NET windows forms

P

Paul M

Hi there,

i just need some clarification with using windows forms, as i have read and
understood that to use a .net window form you need to create a new instance
of it..

If i had 3 forms, FormA (main form), FormB and FormC....

FormA has code that initalizes both forms B and C. If i wanted the active
FormC, to do something to FormB, do i then have to initalize another
reference to FormB before i can access it, even though it is already open?

small code example would be greatly appreciated.

thanks,
 
C

Cor

Hi Paul,

I thought exactly as you where asking?

Cor

\\\form1
Private WithEvents frm3 As New Form3
Private WithEvents frm2 As New Form2
Private Sub frm2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm2.VisibleChanged
If frm2.inputfield <> "" Then
Me.Show()
Me.Label1.Text = frm2.inputfield
frm2.Dispose()
End If
End Sub
Private Sub frm3_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm3.VisibleChanged
If frm3.inputfield <> "" Then
Me.Show()
Me.Label1.Text = frm3.inputfield
frm3.Dispose()
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
frm3.Show()
frm3.TopLevel = True
frm2.Show()
frm2.TopLevel = True
End Sub
///
\\\form2 and 3 the same
Public inputfield As String
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
inputfield = "sometext"
Me.Hide()
End Sub
///
 
H

Herfried K. Wagner [MVP]

* "Paul M said:
i just need some clarification with using windows forms, as i have read and
understood that to use a .net window form you need to create a new instance
of it..

If i had 3 forms, FormA (main form), FormB and FormC....

FormA has code that initalizes both forms B and C. If i wanted the active
FormC, to do something to FormB, do i then have to initalize another
reference to FormB before i can access it, even though it is already open?

You will have to store references somewhere where they can be accessed
by the other forms, for example in public properties or by passing them
as parameters of methods.
 
P

Paul M

Thanks for your help.

Is it good practice to create public variables of all forms in a class
module, eg. Public fError as frmErrorLog....
and then when i need to access it, just do fError=new frmErrorLog?

If the form is already open, will fError be referencing that form?

thanks,
Paul.
 
H

Herfried K. Wagner [MVP]

* "Paul M said:
Is it good practice to create public variables of all forms in a class
module, eg. Public fError as frmErrorLog....
and then when i need to access it, just do fError=new frmErrorLog?

Make a Google Groups Search on "Singleton design pattern", maybe that's
what you are looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top