Results 1 to 9 of 9

Thread: Multiple Forms Question [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33

    Question 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

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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

  3. #3
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    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:
    1. Private Sub ViewSelected_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         e.Cancel = True
    3.         Me.Hide()
    4. 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

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33

    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
    Rich J Defilippo

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    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

  6. #6
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    looks like you pressed TAB again

  7. #7
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    and its

    [vbcode]
    '-- VB code here
    [/vbcode]

    to put your code inside vb tags

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Ft. Lauderdale, FL
    Posts
    33
    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
    Rich J Defilippo

  9. #9
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    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
  •  



Click Here to Expand Forum to Full Width