views:

152

answers:

3

I have a custom form, B. B is created by A, which has a handle to B.VisibleChanged.

B only has an OK and Cancel button on it, and I want to do some logic when OK is hit.

B's OK button is dealt with like this:

Me.Result = Windows.Forms.DialogResult.OK
Me.Hide()

The code in A is properly hit and run, but it never hides B. When I check the values of the properties on B, it shows me Visible = False.

Does anyone have any suggestions as to the possible cause of this issue?

Edit This form was shown using the Show() command, as I'm making a later call to have the form flash using FlashWindow().

+2  A: 

Not exactly sure about your question.

  1. why not use me.Close() instead of me.Hide?
  2. Is it OK to have multiple instances of B at a time? If not, go for ShowDialog.

If you can rephrase the question, someone can probably resolve your problem.

danish
I originally was using me.Close, but I was having to instantiate a new one every time it hit a certain point in code, which could be considerably often. I would use ShowDialog, but then that doesn't allow me to use FlashWindow() on it.
Joe Morgan
can you post some code?
danish
If you use ShowDialog, then you won't have to create a new instance every time, you can reuse the same instance because closing it does not dispose of it. But if you just call Show, when you call Close, the form is automatically disposed.
Chris Dunaway
+1  A: 

I suppose you want to display a messagebox with an ok & cancel button. Instead of using a form use a mesagebox. eg:

    DialogResult dgResult = MessageBox.Show("Click Yes or No", "Test App", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
    if (DialogResult.OK == dgResult)
    {
        //Do what you want.
    }
    else
    {
        //Do nothing.
    }

If you are going to use a form, to do that & wanted to modify the parent's form, it would be advisable to use delegates to prevent form B from modifying form A's variables.

Else: (Not recommended)

Declare form B as a member variable of form A. when required instantiate form B. do B.ShowDialog(); internally in OK & cancel do this.dispose(); Again when you need form B just instantiate. re - instantiation will not be too much of an overhead if you dont call it very often.

But if you only need OK Cancel, use message box instead.

Ganesh R.
+1  A: 

The show/hide approach works for me:

Public Class frmViewChild   ' your form A
Private WithEvents _edit As frmEdit

'code

Private Sub editCell()
 Dim PKVal As String
 Dim PKVal2 As String
 Dim fieldOrdinalPos As Integer
 Dim isLastField As Boolean

 If _edit Is Nothing Then
  _edit = New frmEdit
  _edit.MdiParent = Me.MdiParent
 End If
 'code
 _edit.init(<params>)
 If Not _edit.isNothing Then
  _edit.Show()
  _edit.WindowState = FormWindowState.Maximized
  _edit.BringToFront()
 End If
End Sub

'code

Private Sub _edit_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _edit.VisibleChanged
 If Not _edit.Visible Then
  WindowState = FormWindowState.Maximized  ' revert after closing edit form
 End If
End Sub

Public Class frmEdit     ' your form B
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
 Dim ret As Integer

 doOK(ret)
 If ret > -1 Then ' no error
  Me.Hide()  ' close form, but didn't cancel
 End If
End Sub

HTH

Beth