Solved

Problem referencing open form

Posted on 2008-10-31
13
175 Views
Last Modified: 2012-05-05
I am having a problem referencing anything on an already opened form. I open the form like this

 Dim frm As New frmAddProperty(TblPropertyBindingSource)
        Dim result As DialogResult = frm.ShowDialog
        frm.Dispose()

Why from another form can I not reference a buttn on the open form?
0
Comment
Question by:wiswalld
  • 6
  • 5
  • 2
13 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 22853146
By default the objects on a form are private.  Change the modifers for the control you want from private to public.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22853176
This is the first line of the code for the button I cannot seem to reference. I am trying to reference like

frmPersons.Button2_Click(Nothing, Nothing)

 Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
0
 
LVL 15

Expert Comment

by:SRigney
ID: 22853282
I'm not understanding what you want to do.   If you make the button public, by setting the modifier to public you should be able to call any method you want to on it.  

If you need something above and beyond that, you should be able to add your own public method to the form and have it do everything that you need internally, while your calling form calls the new public method that you provided.   That gives you better encapsulation.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22853360
You DISPOSED of it:

    frm.Dispose()

How exactly can you access something on a disposed form?  Dispose() tells .Net to COMPLETELY GET RID OF IT.

Also, you need to differentiate between an instantiated form and the default instance.

When you create instances with the "new" keyword then you need to pass that variable around so that you can use it to access the form instance.

If you use the default instance, which is just the name of the form, then you can access it from anywhere by just using the name.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22853683
Thats not it. What I mean is if I put a button on form1 (lets say) and type in form2.Button2_Click after I put in Form2. I should get a list of options like controls on Form2. I do not see them and if I just type them in I get the error.
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22853786
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22854053
What version VB.Net are you using?...
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 22854531
It sounds like you are using VB.Net 2003 (or below)...and if not you can still learn something new.

Instead of using a Module and Public Variables as in the link you provided, consider implementing the SINGLETON pattern.  It is a method of ensuring that only ONE instance of an Object can ever be created.  It uses a Shared accessor which means you can obtain the same instance from ANYWHERE in your project.

In the below example, you can use frmPersons.Instance() from anywhere in your project to get a reference to the currently open instance of frmPersons.
Public Class Form1
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fp As frmPersons = frmPersons.Instance

        fp.Show()

    End Sub
 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        frmPersons.Instance.Button1.PerformClick()

    End Sub
 

End Class
 

Public Class frmPersons
 

    Private Shared WithEvents _Instance As frmPersons
 

    Private Sub New()

        ' This call is required by the Windows Form Designer.

        InitializeComponent()
 

        ' Add any initialization after the InitializeComponent() call.

    End Sub
 

    Public Shared Function Instance() As frmPersons

        If frmPersons._Instance Is Nothing Then

            frmPersons._Instance = New frmPersons

        End If

        Return frmPersons._Instance

    End Function
 

    Private Shared Sub _Instance_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Instance.Closed

        frmPersons._Instance = Nothing

    End Sub
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MessageBox.Show("Hello from frmPersons!")

    End Sub
 

End Class

Open in new window

0
 
LVL 13

Author Comment

by:wiswalld
ID: 22855148
2005

I will try that.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22855703
2005 huh?...You don't need the Singleton then.  As I mentioned before, just use the "default instance" of the forms.  They are like built-in singletons that make it easier to communicate between forms.  Just like singletons, they are useful for when you only ever need ONE instance of a Form.

To get the default instance, just use the name of the form:

    frmPersons.Show() ' <--- Without using the "New" keyword

See:
http://msdn.microsoft.com/en-us/library/ms233839(VS.80).aspx
and:
http://msdn.microsoft.com/en-us/library/87y2hdsf(VS.80).aspx

    "You can access the forms provided by the My.Forms object by using the name of the form, without qualification. Because the property name is the same as the form's type name, this allows you to access a form as if it had a default instance. For example, My.Forms.Form1.Show is equivalent to Form1.Show."
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22876473
Idle Mind

I was opening the form and using the bindingsource from the current form to the new form.

Dim frm As New frmAddProperty(TblPropertyBindingSource)


Then in the load of the second form setting the bindingsource equal to the bindingsource on the first form.

Thats why I was getting the error
0
 
LVL 13

Author Comment

by:wiswalld
ID: 22876511
When I use the Singleton method it works fine.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22876677
Glad we helped you figure it out.  You have several options then...  =)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now