Shorter way to refer to another forms controls/events?

D

Dean Slindee

Is there a shorter way of referring to another forms controls/events than
creating a class hold forms, adding a form item each time a new form is
loaded, and then looping thru the class of forms to find the reference?
Like "friend", rather than like below:

Public Class frmMain

Private Sub frmMain_Load

clsForm.Add(Me)

End Sub

End Class



Public Class frmMinor

Private Sub frmMinor_Load

clsForm.Add(Me)

End Sub

Private Sub frmMinor_AnyEvent

Dim frm As Form

For Each frm In clsForm

If frm.Name = cfrmMain Then

Dim frmMain As frmMain = frm

Dim etvw As New
System.Windows.Forms.TreeViewEventArgs(frmMain.tvwAdmin.SelectedNode,
TreeViewAction.ByMouse)

Call frmMain.tvwAdmin_AfterSelect(Me, etvw)

Exit For

End If

Next

frm = Nothing

End Sub

End Class



Thanks,

Dean Slindee
 
P

Peter Huang

HI Dean,

I think you may try to use the inherited from

You may try to right click on the project and select Add/Add inherited
form, thus, you can add a new form inherits the original form.

If you have any question, please feel free to let me know.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Dean Slindee

I should have been a bit more clear. In this case, the forms are two
different, independent forms that need to talk to one another to syncronize
their displays. Inheriting may not help here.
 
P

Peter Huang

HI Dean,

You may try to use the Hashtable. For performance concern, I think you may
need to hold a weak reference in the Hashtable. If you use strong
reference, the form instance will not be collected by GC, because there is
a reference to the form(the reference in the hashtable)

[Define a Hashtable]
Module Module1
Public arrForm As New Hashtable
End Module

[add Form2 to hashtable]
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form2", wr)
End Sub

[add Form3 to hashtable]
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form3", wr)
End Sub

[add Form1 to hashtable]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form1", wr)
Dim fm As Form = New Form2
fm.Show()
fm = New Form3
fm.Show()
End Sub

[you can retrieve the Form from the form collection(hash table)]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As Form = CType(arrForm("Form2").Target, Form)
frm.Text = "hello"
End Sub

[INFO]
Q: Please explain how Long and Short Weak References work and the usage
scenarios for both.

Brian Harry: The differences between long and short weak references are
when the object reference inside the weak reference gets nulled out.

Q: So if the reference is held after GC then it is considered long?

Brian Harry: For short references, the contained reference gets nulled out
as soon as the object is deemed unreachable. This is ultimately more
performant because we remove the reference sooner. Long weak references are
nulled after the object has been finalized.

Q: Which brings us back to my question about when to use long and short
weak references?

Brian Harry: Long/Short¡ªAs a general rule, always use short weak
references. Only use long ones if you need to access the handle to find the
object even during finalization. This is a very rare need. There are some
caching schemes where this turns out to be valuable but I don't think I
could describe them very well without spending a while to think about it.

WeakReference Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWeakReferenceClassctorTopic2.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter Huang

HI Dean,

You may try to use the Hashtable. For performance concern, I think you may
need to hold a weak reference in the Hashtable. If you use strong
reference, the form instance will not be collected by GC, because there is
a reference to the form(the reference in the hashtable)

[Define a Hashtable]
Module Module1
Public arrForm As New Hashtable
End Module

[add Form2 to hashtable]
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form2", wr)
End Sub

[add Form3 to hashtable]
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form3", wr)
End Sub

[add Form1 to hashtable]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim wr As WeakReference = New WeakReference(Me, False)
arrForm.Add("Form1", wr)
Dim fm As Form = New Form2
fm.Show()
fm = New Form3
fm.Show()
End Sub

[you can retrieve the Form from the form collection(hash table)]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim frm As Form = CType(arrForm("Form2").Target, Form)
frm.Text = "hello"
End Sub

[INFO]
Q: Please explain how Long and Short Weak References work and the usage
scenarios for both.

Brian Harry: The differences between long and short weak references are
when the object reference inside the weak reference gets nulled out.

Q: So if the reference is held after GC then it is considered long?

Brian Harry: For short references, the contained reference gets nulled out
as soon as the object is deemed unreachable. This is ultimately more
performant because we remove the reference sooner. Long weak references are
nulled after the object has been finalized.

Q: Which brings us back to my question about when to use long and short
weak references?

Brian Harry: Long/Short¡ªAs a general rule, always use short weak
references. Only use long ones if you need to access the handle to find the
object even during finalization. This is a very rare need. There are some
caching schemes where this turns out to be valuable but I don't think I
could describe them very well without spending a while to think about it.

WeakReference Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWeakReferenceClassctorTopic2.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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