?
Solved

Hide the close button on a VB 6.5 form

Posted on 2009-03-30
10
Medium Priority
?
699 Views
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?
0
Comment
Question by:sbsbats
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 25

Expert Comment

by:SStory
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.
0
 
LVL 2

Accepted Solution

by:
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)
 
Else
 
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

0
 

Author Comment

by:sbsbats
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

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

Expert Comment

by:Zoooink
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

http://msdn.microsoft.com/en-us/library/aa234177(VS.60).aspx
0
 
LVL 25

Expert Comment

by:SStory
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.
0
 
LVL 2

Expert Comment

by:mjsteele001
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
0
 
LVL 25

Expert Comment

by:SStory
ID: 24028755
mjsteele001,

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.
0
 
LVL 2

Expert Comment

by:mjsteele001
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.
0
 

Author Closing Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

621 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