hide or close?

ssfftt

Well-known member
Joined
Oct 27, 2005
Messages
163
Programming Experience
1-3
[solved]hide or close?

when i finish editing in a frm at runtime, i want it disappear, e.g. when i finish adding a new customer, i want customer form disppear, which doesnt effect any other forms. Should i call "me.hide" or "me.close"? whats the difference? if i use "me.hide", next time when i open this form, the procedures under form_onLoad event will still function? what about "me.close"?
 
Last edited:
Let's get something straight. Declarations refer to variables. A form is an instance of a class, so the variable is not the object. You can declare a form variable at the class level or locally, but that has no bearing on whether you should use Hide or Close to dismiss a form. Regardless of where the variable is declared, you would call Hide IF AND ONLY IF you need to redisplay the SAME INSTANCE at a later time. If you don't need the same instance then call Close. You should be calling Close in the vast majority of circumstances. For example, the following code declares a form variable at the class level and then it will create a new form when needed or just activate the existing form if there is one
VB.NET:
Private myForm As Form

Private Sub Button1_Click(...) Handles Button1.Click
    If myForm Is Nothing OrElse myForm.IsDisposed Then
        'A form has never been created or it has been destroyed, i.e. Closed.
        myForm = New Form
        myForm.Show()
    Else
        'A form is already open.
        myForm.Activate()
    End If
End Sub
 
thats very valuable explaination,thx all, just one more question plz: when click on the red "X" on top-right side, i.e. trying to close the form, i want to execute this chunk of code:
VB.NET:
        If MsgBox("Are you sure to close this window?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then            'MessageBox.Show("closed!", MsgBoxStyle.YesNo)            Me.Close()        End If
where can i put it? what procedure does the red "X" use?
 
You would use the Closing event handler of the form, and may I suggest two changes. Use MessageBox.Show rather than MsgBox in all circumstances, and use OKCancel rather than YesNo in this circumstance. YesNo implies that you are going to do what the user asked for anyway but you could also do something else as well, e.g. use YesNo for "Would you like to save your work before closing?". If the choice is to either do what was asked for or not then you should be using OKCancel.
VB.NET:
Private Sub  Form1_Closing(...) Handles MyBase.Closing
    If MessageBox.Show("Are you sure?", "Closing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then
        e.Cancel = True
    End If
End Sub
This code will be executed whenever the form tries to close. If you only want the message displayed when the Close button is used then you have two choices. You can either override the WndProc method to intercept Windows messages and identify the message that is sent when the Close button is used. There are examples of this around, quite possibly on this forum, so a search should turn up extra info. The other option would be to set a boolean variable whenever some other method is used to close the form, like a menu item. You would test this variable in the Closing event handler and if it is not set then the Close button must have been used.
 
thx jmcilhinney, I used your code, it worked perfectly!Now i fully understood and appreciate ur help very much, cheers~!
 

Latest posts

Back
Top