Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem referencing open form

Posted on 2008-10-31
13
178 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cannot connect to sqlserver 8 35
Complex SQL statement in VB.NET 7 31
System.net.Sockets Error 5 28
VB.net Duplicating a table - primary key not created 3 32
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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