|
-
Sep 8th, 2003, 01:02 PM
#1
Thread Starter
Hyperactive Member
Not reopening the same form
A friend of mine has a problem in VB.NET, and since I'm no expert on the .NET variant, I thought I'd ask it here on his behalf.
His problem is the following: He has a button which opens a form, but pressing the button several times in order opens the same form over and over again. Is there a way to put a stop to this??
Obey the dragon thing. Or not. Or possibly just a bit.
-
Sep 8th, 2003, 01:16 PM
#2
Sleep mode
Umm , you mean it open multiple instances of the form ? and he wants to open only one form ?
-
Sep 8th, 2003, 01:25 PM
#3
Thread Starter
Hyperactive Member
that's the general idea. I just called him and he said he was using a MDI interface. There's a menu-item that opens the child window and he can either create a new instance of the form in that button's code (causing said problem) or publicly declare the form once an have it destroyed by .NET Garbage collecting code titbits. So, there it is...
*edit: woopsie... the problem is that in the first case he can't stop the opening of multiple instances and in the second case he can only display the window once, after which garbage stuff kicks in and the instance is destroyefied
Last edited by The Dutch Dude; Sep 8th, 2003 at 02:11 PM.
Obey the dragon thing. Or not. Or possibly just a bit.
-
Sep 8th, 2003, 01:54 PM
#4
Sleep mode
So what's the problem now ?
-
Mar 7th, 2004, 05:38 AM
#5
Frenzied Member
Has anyone found a solution to this problem?
In .NET, it seems that you get multiple instances of the same form if you declare the form locally in a button click procedure.
The solution would seem to be to declare the form at form level rather than locally so that when you call Form1.Show, an existing instance of the form is displayed rather than creating a second or third instance of the same form.
However, once you close the form, the garbage collector disposes of it and you get an error next time you call Form1.Show
I have tried declaring the form at form level (Dim Form1 As Form1) and putting this in the button click procedure:
VB Code:
If IsNothing(Form1) = False Then
Form1.Dispose()
Form1= Nothing
Form1 = New Form1
End If
Form1.MdiParent = Me
Form1.Show()
Although this prevents multiple instances of the same form being created and allows you to re-open the same form, it you have an existing instance of Form1, it will be replaced with a fresh instance. I want to be able to re-activate an existing instance.
I have also tried:
VB Code:
If IsNothing(Form1) = False Then
Form1.Activate()
Else
Form1= New Form1
Form1.MdiParent = Me
Form1.Show()
End If
The problem with this is that once Form1 has been opened and closed, IsNothing(Form1) returns False. It only returns True prior to creating the first instance.
This was so straightforward in VB6.
-
Mar 7th, 2004, 07:02 AM
#6
Then why are you distroying the instance?
VB Code:
'Just use Me.Hide when you want the form to "hide"
Form1.Hide
and when you call Form1.Show it will come back with all the properties and controls just as they were before.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Mar 7th, 2004, 07:39 AM
#7
PowerPoster
Hi,
Isn't the simplest thing to do as follows:
On opening the form, set the button.enabled property to false.
In the form.closed event set the button.enabled property to true.
Or use the visible property if preferred.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Mar 7th, 2004, 08:53 AM
#8
Frenzied Member
<ABX,
I am destroying the instance of the form because the instance seems to exist even after the form is closed.
ie. IsNothing(Form1) returns False after closing the form.
I have even tried putting Me.Dispose in the Form's Closing event in the hope that that would make future calls to IsNothing(Form1) return True.
I want to be able to detect whether there is an active instance of the form (hidden by another form) and when the button that shows the form is clicked:
either
1. An existing instance is activated
or
2. A new instance is created if there is no existing instance
Thanks for the suggestion, taxes, but I want the button to be enabled so that an existing instance can be activated.
-
Mar 7th, 2004, 09:35 AM
#9
PowerPoster
Hi
"I want to be able to detect whether there is an active instance of the form (hidden by another form) and when the button that shows the form is clicked:
either
1. An existing instance is activated
or
2. A new instance is created if there is no existing instance
Thanks for the suggestion, taxes, but I want the button to be enabled so that an existing instance can be activated."
Come on, you're not thinking this thing through.
I understand you to say that you only want ONE instance of the form, but that it might be hidden.
Therefore, in the click event test to see what the text is. Assuming the original text of the button is "Create Form1",
if it is still "Create Form1" then create the instance and change the text to "Show Form1".
If the text is "Show Form1" and then show form1.
In the closed event of form1, change the text of the button to "Create Form1".
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Mar 7th, 2004, 09:42 AM
#10
Sleep mode
To stop multi-instantiation , this one trick :
VB Code:
'This class member
Private i As Integer = 0
'This goes in a button
If Not i = 1 Then
Dim frmChild As New Form2
frmChild.Show()
i = 1
End If
-
Mar 7th, 2004, 10:22 AM
#11
Frenzied Member
I have still got the problem of detecting whether an instance of the form object exists.
Why does IsNothing(Form1) return False after the Form1 has been closed?
Shouldn't closing Form1 set the Form1 object to nothing?
How do I explicitly set the form object to nothing when it is closed?
In VB6, it was
VB Code:
Unload Form1
Form1.Hide
Set Form1 = Nothing
The "Set" keyword is no longer supported but I can only call
Form1 = Nothing in the form class where Form1 is declared (at form level). I want to be able to set it to nothing in the closing event of the form that is being closed.
-
Mar 7th, 2004, 10:38 AM
#12
PowerPoster
Hi,
"I have still got the problem of detecting whether an instance of the form object exists."
If you use my suggestion, you will know that an instance of the form exists by checking the text of the button!!!!!!
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Mar 7th, 2004, 10:39 AM
#13
Sleep mode
When you close your form , it's disposed but not immediately . It's marked as orphan object and collected when the Garbage Collector is fired . Sadly , you can't guess when GC is fired . It's working randomizly under some vague rules .
-
Mar 7th, 2004, 10:57 AM
#14
yay gay
Well, just do a SingleTon class
\m/  \m/
-
Mar 7th, 2004, 11:23 AM
#15
Sleep mode
I would do it this way , if I were you :
MainForm :
VB Code:
'Class member
Dim frmchild As New childfrm
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
If i = 1 Then
frmchild.ActivateMe()
Else
frmchild.Show()
i = 1
End If
End Sub
and in the ChildForm (second form you're showing) , do this :
Add sub with Public modifier , something similar to this :
VB Code:
Public Sub ActivateMe()
Me.Focus()
End Sub
This would create the form if not existed , if it's already there , then it's would get focused .
-
Mar 7th, 2004, 11:29 AM
#16
-
Mar 7th, 2004, 09:22 PM
#17
Frenzied Member
Thanks for your suggesions, guys.
Edneeis, I used this from the first thread and it worked:
VB Code:
Public Sub ShowSingleInstance(ByVal childType As Type, ByVal args() As Object)
For Each child As Form In Me.MdiChildren
If child.GetType Is childType Then
'already loaded so bring to foreground
child.Activate()
Return
End If
Next
'not loaded yet so create
Dim frm As Form = Activator.CreateInstance(childType, args)
frm.MdiParent = Me
frm.Show()
End Sub
Public Sub ShowSingleInstance(ByVal childType As Type)
ShowSingleInstance(childType, Nothing)
End Sub
'syntax for no args
ShowSingleInstance(GetType(frmChildReplace))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|