Refer to an Instance of a form

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I have some custom forms that contain their own properties and I call them
as such:

Dim myDialog As New myDialogForm

myDialog.mID = 1 ' sets the custom property mID to 1
myDialog.Text = "Here is my dialog"
myDialog.Show()

Now how do I refer to another form however when it's not created as an
instance by my code? I have been calling it by name, as above, assume a
myDialogForm is already open:

myDialogForm.mID = 1
myDialogForm.Text = "Here is my dialog"

This seems odd to me though because I'm not referring to a specific instance
of the dialog form. I've also had unexpected buggy behavior doing this. Is
there a better way to access an existing form?

Thanks,
Ryan
 
Hi,

You need to pass that instance to wherever yout want to access it from.
For example, assume you have form1 and your mydialog:

Dim myDialog As New myDialogForm
Dim myForm2 As New Form2(mydialog)

of course you need to overload the constructor in Form2 class and add
to it a veriable of type myDialogForm.

Then you can reference that instance using that veriable.

You cant call the veriables using the class name, unless they are
shared (static in C/C++ or java)

I hope I answered your question.

Ahmed
 
Hi Ryan,

Thank you for posting.

I think you have two options to refer to an instance of a form in another
form.

Suppose you have created an instance of a form and assigned it to a
variable "dialogfrm" of type Form. Then you want to refer to dialogfrm in
an instance of Form1.

Option 1: Pass the dialogfrm to Form1 as a parameter in Form1's New
procedure.
Here's a sample.

Private _dialogfrm As Form = Nothing
Public Sub New(ByRef dialog As Form)
InitializeComponent()
_dialogfrm = dialog
End Sub

Option2: Set up a public property in Form1 and then set the dialogfrm to
the property.
Here's a sample.

Private _dialogfrm As Form = Nothing
Public Property Dialogfrm() As Form
Get
Return _dialogfrm
End Get
Set(ByVal value As Form)
_dialogfrm = value
End Set
End Property

Thus, you could refer to the dialogfrm in the instance of Form1.

If you have anything unclear, don't hesitate to get in touch. I look
forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Linda,

Wow.. very clear and consise.. this is exactly what I was looking for.
Thanks once again for your wonderful advice. :)

Ryan
 
Hi Ryan,

Thanks for your response. You're welcome!

If you have any other questions, don't hesitate to contact us. It is always
our pleasure to be of assistance.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
Back
Top