|
-
Feb 6th, 2003, 04:15 PM
#1
Thread Starter
Member
Multiple Forms Question [Resolved]
Hello,
This might be a really dumb question so please bear with me!!
I'm just starting with vb.net. I have an app with two forms. the first form has a menu item which opens up the second form.
Code in first Form:
Code:
Public Class Form1
Dim frmViewSelected As New frmViewSelImgs()
'
' Bunch o' Code
'
' Click Event For Menu Item
Private Sub mnuViewSelectedImgs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSelectedImgs.Click
With frmViewSelected
.myImgArray = UploadArray
.Show()
.Focus()
End With
End Sub
This works ok ... until I close the second window and then try to open it again. I get an error saying that I can't acces an object that has been disposed - something like that.
It seems to me that when form1 loads it creates the frmViewSelected object --- but when that window is closed the object is destroyed or disposed of and the main form dosen't know what object frmViewSelected is.
Is there a better way to do this? This method was suggested by a .net book. It was the only way suggested.
Thanks!
-Rich
Last edited by Richdef; Feb 7th, 2003 at 10:27 AM.
Rich J Defilippo
-
Feb 6th, 2003, 05:16 PM
#2
PowerPoster
Code:
Public Class Form1
'
' Bunch o' Code
'
' Click Event For Menu Item
Private Sub mnuViewSelectedImgs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSelectedImgs.Click
Dim frmViewSelected As New frmViewSelImgs()
With frmViewSelected
.myImgArray = UploadArray
.Show()
.Focus()
End With
End Sub
-
Feb 6th, 2003, 05:48 PM
#3
Fanatic Member
Originally posted by hellswraith
Code:
Public Class Form1
'
' Bunch o' Code
'
' Click Event For Menu Item
Private Sub mnuViewSelectedImgs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSelectedImgs.Click
Dim frmViewSelected As New frmViewSelImgs()
With frmViewSelected
.myImgArray = UploadArray
.Show()
.Focus()
End With
End Sub
Thats all good but it will make a new instance of the form, and in some cases you may not want this to happen.
I had the same problem earlier, and if you like me, and dont need a new instance of the form , on the forms closing event, cancel the close action, and hide the window
VB Code:
Private Sub ViewSelected_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
Me.Hide()
End Sub
now when you click your menu, it will show the form as expected
and one more thing, because it doesnt create a new instance of the form, the forms data will be unchanged, i.e. all textbox's will still have any values in them, you would have to empty all these on the closing event
-
Feb 7th, 2003, 10:05 AM
#4
Thread Starter
Member
Multiple Forms
You guys were right ... except I had to put a condition to prevent mulriple forms from opening up:
[vb .net code]
Public Class Form1
Dim frmViewSelected As frmViewSelImgs
'
' Bunch o' Code
'
Private Sub mnuViewSelectedImgs_Click ......
If frmViewSelected Is Nothing Then
frmViewSelected = New frmViewSelImgs()
End If
With frmViewSelected
.myImgArray = UploadArray
.Show()
.Focus()
End With
End Sub
-
Feb 7th, 2003, 10:11 AM
#5
Thread Starter
Member
sorry,
There was one other thing I wanted to say ...
I keep hitting tab in the code entry section on this forum and it messes me up!
But what I was saying is that the If statement to check if the from is an object prevents multiple forms from coming up.
Without that condition I would get multiple copies of the same form.
The cancel event in the secondary form's window was essential.
Thanks for all your help!
By the way for anyone else who might have this problem in the future the steps I took to solve multiple forms is as follows:
1. Dim the form object at the beginning of your main form object.
Code:
Public Class Form 1
Dim frmViewSelected As frmViewSelImgs
2 . In the Click Event that opens the second window:
Code:
If frmViewSelected Is Nothing Then
frmViewSelected = New frmViewSelImgs()
End If
With frmViewSelected
.Show()
.Focus()
End With
3. In the closing event for the second form make sure you cancel the action and hide the form.
Code:
Private Sub frmViewSelImgs_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
Me.Hide()
End Sub
That should do it. There may be a more efficient way but this works.
Last edited by Richdef; Feb 7th, 2003 at 10:16 AM.
Rich J Defilippo
-
Feb 7th, 2003, 10:13 AM
#6
Fanatic Member
looks like you pressed TAB again
-
Feb 7th, 2003, 10:16 AM
#7
Fanatic Member
and its
[vbcode]
'-- VB code here
[/vbcode]
to put your code inside vb tags
-
Feb 7th, 2003, 10:22 AM
#8
Thread Starter
Member
Thanks again guys!
This simple task was driving me nuts! How do I mark this as "Resolved" so that people can easily find it and refer to it if they have the same problem?
Is that even necessary?
By the way, hellswraith is right. .Net is cool! Its worth wrestling with if your new to it!
-Rich
-
Feb 7th, 2003, 10:25 AM
#9
Fanatic Member
use
[vbcode]
[/vbcode]
instead of
[code]
[/code]
to get syntax highligting 
and you change the topic to resolved, click the edit button on your first message in this thread, you can change the subject there
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
|