Hide the close button on a VB 6.5 form

Posted on 2009-03-30
Medium Priority
Last Modified: 2013-12-20
I have a form and I want to hide the X in the top right hand corner so my user cannot close the form - they have to select one of the preset options.

There must be a property of the form that enables you to hide the X button?
Question by:sbsbats
  • 4
  • 3
  • 2
  • +1
LVL 25

Expert Comment

ID: 24022904
I'm not sure exactly what you are talking about. IS this a form you made? If so try the "ControlBox" property...set it to false.

Accepted Solution

mjsteele001 earned 2000 total points
ID: 24022951
Right click the form, and select View Code.  Copy and paste the following code onto the very top of the module.
Private Declare Function FindWindow Lib "user32" _
  Alias "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" _
  Alias "GetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
  Alias "SetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000
Private Sub UserForm_Initialize()
'this hides the X on the caption line
Dim hWnd As Long, a As Long
Dim V As Integer
V = CInt(Left(Application.Version, _
InStr(Application.Version, ".")))
'If V = 8 this is Word 97
If V = 8 Then
hWnd = FindWindow("ThunderXFrame", Me.Caption)
hWnd = FindWindow("ThunderDFrame", Me.Caption)
End If
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub

Open in new window


Author Comment

ID: 24023599
HI SStory,
Yes its a form I have created using VB 6.5
I have heard that before about the controlBox property - am i missing something here? I can't find this property - I have the usual e.g. Backcolor, Borderstyle, caption, font etc etc but where is the controlbox property
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.


Expert Comment

ID: 24027789
Sorry - I guess the tag mislead me - the code I provided is for VBA to be used in Word or Excel

Expert Comment

ID: 24028024
if you cannot find the property you can try to hide it programatically in the main or or form load sub

form1.controlbox = false

more info can be found here

LVL 25

Expert Comment

ID: 24028181
Hmm.. I'm sorry, but in my Word, VBA, I just created a form and it doesn't offer the .ControlBox property. Maybe they don't want you to do it.  It might be possible through a lot of API calls to create your own Window, but that would be a lot of work and I'm not sure the consequences.

When a "form" is created in VB or VBA, many system calls are made. One of them is to CreateWindow or CreateWindowEx.  A class giving all of the specifications of the window is sent there.  Of course once you created it I don't know how you'd properly link it back into VBA so that you'd receive the messages there unless you wanted to write the Window routine and all of that stuff. You will quickly get into a lot of work doing this.

Expert Comment

ID: 24028225
SStory - did you try the code I provided?  In VBA - it worked for me using Office 2007 and 2003.  This needs to be pasted at the top of the form module
LVL 25

Expert Comment

ID: 24028755

Having looked at your code, I do believe that would work. I forgot about changing the Window Long.

Is there anything he will have to do in terms of destroying the window/releasing resources? I don't think so from looking at it, but just thought I'd ask.

Expert Comment

ID: 24028893
I wouldn't know - this is code that I've used on one of my own applications, not sure where I picked it up.

Author Closing Comment

ID: 31564527
Used your code to do it in the end - worked sweet

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

569 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