Results 1 to 4 of 4

Thread: [2008] Open and Close(all but first) with modal or zorder forms

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    2

    [2008] Open and Close(all but first) with modal or zorder forms

    I'm trying to get multiple forms to open/close in a certain way. I want them to either be modal, stay on top with zorder, or something similar...so, the user will have to: a) give input, b) can ask for help in that form with another button, or LogOut. It needs to be able to close all other forms back to the first (form1) with a LogOut button at other point. So, right off I was playing with modal and couldn't get Form2 to close. I'm not sure if this is the better way to approach it though or the details of how. If anyone has any better ways, I'd be glad to hear, thanks. (VS 2008)

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim form2Active As New Form2
            form2Active.ShowDialog()
        End Sub
    End Class
    
    Public Class Form2
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim form3Active As New Form3
            form3Active.ShowDialog()
        End Sub
    End Class
    
    Public Class Form3
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Form2.Close()
            Me.Close()
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Open and Close(all but first) with modal or zorder forms

    Form3 should not call Me.Close. It should set its own DialogResult property. The value you set will be returned by ShowDialog, so you can return a different value depending on whether Form2 should close itself too.

    Alternatively, Form3 could expose a public property that indicates whether Form2 should close or not. After ShowDialog returns, Form2 can test that property of Form3 and then decide whether to close based on the value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Open and Close(all but first) with modal or zorder forms

    Your code in form3 is flawed. Since in form2 you declared form3 as a dialog form, form2 will no longer respond until form3 is closed. Therefore in form 3 when you use Form2.Close(), you're going to error out or simply have no effect. Do you want the forms to collapse like dominoes? Then simply change your code for form2 and form3 to this:

    Code:
    Public Class Form2
        Private Sub Button2_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs _
        ) Handles Button2.Click
            Dim form3Active As New Form3
            form3Active.ShowDialog()
            Me.Close()
        End Sub
    End Class
    
    Public Class Form3
        Private Sub Button3_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs _
        ) Handles Button3.Click
            Me.Close()
        End Sub
    End Class
    Of course, this is a very rough piece of code, which will always close form2 as soon as form3 is closed. If you want more flexibility to choose whether to close form2 when form3 is closed, then you can use the DialogResult property like this:

    Code:
    Public Class Form2
        Private Sub Button2_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs _
        ) Handles Button2.Click
            Using form3Active As New Form3
                If form3Active.ShowDialog() = Windows.Forms.DialogResult.OK Then _
                    Me.Close()
            End Using
        End Sub
    End Class
    
    Public Class Form3
        Private Sub Button3_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs _
        ) Handles Button3.Click
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End Sub
    End Class

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    2

    Re: [2008] Open and Close(all but first) with modal or zorder forms

    Thank you jmcilhinney and MaximilianMayrhofer. This was a simplicaton of what I'm having the forms do, but I'm going to try returning a boolean flag for logout from each form. I can evaluate that and if bolLogOutFlag is true, skip over opening new modal forms and skip entering incomplete data into database, returning to the login form. I'll post again once I get it working or run into trouble.

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