Solved

Problem referencing open form

Posted on 2008-10-31
13
177 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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