Solved

Hide X button on form

Posted on 2002-05-09
5
400 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:afpcos
5 Comments
 
LVL 15

Accepted Solution

by:
robbert earned 100 total points
ID: 6999294
I've never seen it hidden but here's how to disable it:

---------------------------------------------

Option Explicit

Private Declare Function GetSystemMenu Lib "user32" _
        (ByVal hwnd As Long, ByVal bRevert As Long) _
        As Long
Private Declare Function RemoveMenu Lib "user32" _
        (ByVal hMenu As Long, ByVal nPosition As Long, _
        ByVal wFlags As Long) As Long
Private Const MF_BYPOSITION = &H400&

Public Sub RemoveCloseMenu(frm As Form)
    Dim hSysMenu As Long
    ' Get the system menu for the form
    hSysMenu = GetSystemMenu(frm.hwnd, 0)
    ' Remove the close item
    Call RemoveMenu(hSysMenu, 6, MF_BYPOSITION)
    ' and the seperator
    Call RemoveMenu(hSysMenu, 5, MF_BYPOSITION)
End Sub

Private Sub Form_Load()
    RemoveCloseMenu Me
End Sub
0
 
LVL 18

Expert Comment

by:Crash2100
ID: 7000250
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
0
 
LVL 2

Expert Comment

by:ventond
ID: 7000388
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.
0
 
LVL 1

Expert Comment

by:olx
ID: 7003655

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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7722509
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now