Solved

Hide the close button on a VB 6.5 form

Posted on 2009-03-30
10
684 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
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.

 
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

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

919 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

14 Experts available now in Live!

Get 1:1 Help Now