views:

79

answers:

3

I have three forms: A, B and C.

Upon a button being clicked, A displays B. Similarly, B displays C. Both using the ShowDialog() method.

However when I use the Hide() method (inside a Button Click event) on form C to close it, form B also closes.

Why would this be? As far as I can see, it shouldn't be doing this. I certainly didn't write any code to tell it to do that.

Here is the code:

' from Form "A" (MainForm)
Private Sub OrdersDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDataGridView.CellDoubleClick
    ShowViewOrderForm(sender)
End Sub

Private Sub ShowViewOrderForm(ByVal CurrentDGV As DataGridView)
    If Not CurrentDGV Is Nothing Then
        Dim f As New ViewOrderForm
        f.SetOrderNo(CurrentDGV.CurrentRow().Cells().Item(0).Value)
        f.SetDeliveryServiceType(CurrentDGV.CurrentRow().Cells().Item(5).Value)

        f.ShowDialog()
    End If
End Sub

' from Form "B" (ViewOrderForm)
Private Sub IssueOrderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IssueOrderButton.Click
    Dim f As New IssueForm
    f.SetOrderNo(ThisOrderNo)
    f.ShowDialog()
End Sub

' from Form "C" (IssueForm)
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
    Me.Hide()
End Sub

UPDATE: I am an idiot. DialogResult was set to Cancel on the button as I'd copy+pasted it from the existing Close button and not realised that property was set. Thanks for your suggestions anyway everyone!

A: 

I have not been able to reproduce the behavior you are seeing with the code that you've supplied. Are there other settings on any of the forms (perhaps set in the designer) that could be causing this?

Also, the Hide() function does not actually close the window. It is the equivalent of changing the Visible property to False. Read about Hide here.

If you truly want to be closing the window, you should be calling Me.Close().

msergeant
Yes it was DialogResult set to Cancel - oops!
George Xyzzy
A: 

Try setting the mdiParent property of the child form:

    If IsNothing(_cases) Then
        _cases = New frmGrid        
        _cases.MdiParent = Me
        _cases.init(_main, 0, "", "")
        _cases.WindowState = FormWindowState.Maximized
    End If
    _cases.Visible = Me.mnuViewCaseFiles.Checked
Beth
Did you maybe enter this in the wrong question?
James B
A: 

The only thing I can think of is that you have an event handler in Form B that is hooked up to Form C's button click event... though how you would do that without realizing I can't imagine.

Try setting Form C's parent when you call show dialog in Form B's IssueOrderButton_Click event by doing

f.ShowDialog(Me)

I agree that you probably want to use Me.Close() rather than Me.Hide()

James B