Solved

Hide the close button on a VB 6.5 form

Posted on 2009-03-30
10
682 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 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)
 

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
 
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
A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

708 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

11 Experts available now in Live!

Get 1:1 Help Now