Joe Morgan
Joe Morgan

Reputation: 1771

VB.NET Form Hiding Issue

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().

Upvotes: 0

Views: 2005

Answers (3)

Beth
Beth

Reputation: 9617

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

Upvotes: 1

Ganesh R.
Ganesh R.

Reputation: 4385

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.

Upvotes: 1

danish
danish

Reputation: 5600

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.

Upvotes: 2

Related Questions