Hide the close button on a VB 6.5 form

Posted on 2009-03-30
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

726 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