How can i block opening a form that is already open 
Author Message
 How can i block opening a form that is already open

In VB6 writing:

formname.show

if the form is already open it get focus, but with .net this is not possible
with the new code a new form is opened every time

Thanks, and sorry for my english



Fri, 19 Mar 2004 18:16:54 GMT  
 How can i block opening a form that is already open
In VB6 writing:

formname.show

if the form is already open it get focus, but with .net this is not possible
with the new code a new form is opened every time

Thanks, and sorry for my english



Fri, 19 Mar 2004 18:09:55 GMT  
 How can i block opening a form that is already open
one way to do is to create objects for all the forms in a module file. Then, while calling the form, check if the form is 'nothing'. If the form is nothing, it is being opened for the first time. Create a new object and show the form. If the form is not nothing, don't create a new instance but simply call the show method.
Note: in show, the load event will not fire!! So if that event is necessary, unload the previous form and create a new instance.

Warm Regards,
 Apoorva Tiwari
Tata Consultancy Services
Website www.tcs.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Fri, 19 Mar 2004 20:24:00 GMT  
 How can i block opening a form that is already open
There's a particular way, that i dont like, but sometimes u can use it, try
it.  Not my favorite, but its an idea
use formname.DefInstance.Show



Quote:
> In VB6 writing:

> formname.show

> if the form is already open it get focus, but with .net this is not
possible
> with the new code a new form is opened every time

> Thanks, and sorry for my english



Sun, 21 Mar 2004 14:16:24 GMT  
 How can i block opening a form that is already open
It if I understand your problem correctly you would like to have only a
single instance of a class (form) and that the show method for that class be
called on that single instance.  If this is the case what you are looking
for is called the "Singleton Design Pattern" and here is an off the top of
my head implementation in VB.NET.

As it was shortened for clarity the designer generated code was removed so
you will have copy and paste the relevant pieces below into your own form.

Public Class Form1
    Inherits System.Windows.Forms.Form

    'This is a top of the head example of implementing a Singleton in
VB.NET.
    'For details on the Singleton Design Patter see
http://www.castle-cadenza.demon.co.uk/single.htm
    'Frank Gorgenyi (Microsoft)

    'This will be the reference to the single allowed instance.
    Private Shared objInitialInstance As Form1

    Public Sub New()
        MyBase.New()

        If objInitialInstance Is Nothing Then
            'Since we have never been created we can set our share variables
safely.
            objInitialInstance = Me
        Else
            'This will cause any 'new' operations on the class beyond the
first to fail.
            Throw New System.Exception("Class is a Singleton, use the
.Singleton method to get an instance.")
        End If

    End Sub

    Public Shared ReadOnly Property Singleton() As Form1
        Get
            If Not (objInitialInstance Is Nothing) Then
                'If we have an instance return it.
                Return objInitialInstance
            Else
                'Otherwise create a new one and return it
                Return New Form1()
            End If
        End Get
    End Property

    Public Shared Sub ShowSingleton()
        'Use the property to ensure we have a valid instance and call it's
show method.
        Singleton.Show()
    End Sub

End Class

Hope this helps.

Frank Gorgenyi
Microsoft

Quote:

> There's a particular way, that i dont like, but sometimes u can use it,
try
> it.  Not my favorite, but its an idea
> use formname.DefInstance.Show




> > In VB6 writing:

> > formname.show

> > if the form is already open it get focus, but with .net this is not
> possible
> > with the new code a new form is opened every time

> > Thanks, and sorry for my english



Sun, 21 Mar 2004 16:12:01 GMT  
 How can i block opening a form that is already open
I created a test app in VB6 that consisted of one MDI parent form with a button to show a single MDI child form and then converted it to .NET to see what happens

It works as I would expect... that is you can click the button to show the child form, clicking the button a second time simply shifts the focus to the child form, but if you close the child form you can click the button and show it again.

The code that the upgrade wizard produces is fairly complicated but is similar to what Frank Gorgenyi has proposed (his code works in that it will prevent a second instance of the original child form but if you close the child form you can't ever open it again).

The code is ugly but appears to be pretty robust (I couldn't make it do anything but what it was supposed to do).  This is something that I am going to have to use in a major project I'm working on and I really wish this capability was built into the system somehow.

Joe Griffith

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sun, 28 Mar 2004 23:02:37 GMT  
 How can i block opening a form that is already open
A simple solution suggested by Apoorva:

in a module write:
Public form1 As application.form_name

then when you have to open the form:

If form1Is Nothing Then

form1= New application.form_name()

End If

form1.Show()

form1.Focus()

For me this work very well!!!!!!!!!!

Bye Fausto



Mon, 29 Mar 2004 17:56:19 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How to open a file for read that is already open by another application

2. Mail merge from Access try to open an already open DB

3. ShellExecute only opens file.htm if browser already open

4. Open an already opened file

5. Open form, close form, open form = disposed object exception

6. Error 2007 - you already have an open database object called xx - sub form problem

7. setting focus when form is already open

8. Check, if windows form is already opened

9. how to change the textbox in a form already open in the back

10. Form already open/minimized

11. Opening a form when another form is opened

12. If opened with shift, open form?

 

 
Powered by phpBB® Forum Software