Link to home
Start Free TrialLog in
Avatar of afpcos
afpcos

asked on

Hide X button on form

I need to let the min and max button show for a form but not the X (close) button in the top right of forms.

Can anyone help me with this?
ASKER CERTIFIED SOLUTION
Avatar of robbert
robbert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you could just put this in the form's unload event to prevent the close button from doing anything.

Private Sub Form_Unload(Cancel As Integer)
    Cancel = -1
End Sub
You can set the Form.ControlBox property to False, but this has the added effect of getting rid of the Min and Max buttons too.
Avatar of olx
olx


use the query unload method

in short:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    If UnloadMode = vbFormControlMenu Then Cancel = True: Exit Sub
   
End Sub

all the blah blah blah would say:

The QueryUnload Event occurs before a form or application closes. When an MDIForm object closes, the QueryUnload event occurs first for the MDI form and then in all MDI child forms. If no form cancels the QueryUnload event, the Unload event occurs first in all other forms and then in an MDI form. When a child form or a Form object closes, the QueryUnload event in that form occurs before the form's Unload event.

Syntax

Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)
Private Sub MDIForm_QueryUnload(cancel As Integer, unloadmode As Integer)

The QueryUnload event syntax has these parts:

cancel: An integer. Setting this argument to any value other than 0 stops the QueryUnload event in all loaded forms and stops the form and application from closing.

unloadmode: A value or constant indicating the cause of the QueryUnload event, as described in Return Values.
Return Values

The unloadmode argument returns the following values:

Constant, Value : Description
vbFormControlMenu, 0 :     The user chose the Close command from the Control menu on the form.

vbFormCode, 1 : The Unload statement is invoked from code.

vbAppWindows, 2 : The current Microsoft Windows operating environment session is ending.

vbAppTaskManager, 3 : The Microsoft Windows Task Manager is closing the application.

vbFormMDIForm, 4 : An MDI child form is closing because the MDI form is closing.

These constants are listed in the Visual Basic (VB) object library in the Object Browser.

Remarks

This event is typically used to make sure there are no unfinished tasks in the forms included in an application before that application closes. For example, if a user has not yet saved some new data in any form, your application can prompt the user to save the data.
When an application closes, you can use either the QueryUnload or Unload event procedure to set the Cancel property to True, stopping the closing process. However, the QueryUnload event occurs in all forms before any are unloaded, and the Unload event occurs as each form is unloaded.

hope this helps
Avatar of DanRollins
Hi afpcos,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept robbert's comment(s) as an answer.

afpcos, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer