Solved

close button?

Posted on 2002-03-14
8
179 Views
Last Modified: 2012-05-04
in a form can we disable or make the close button disable?
0
Comment
Question by:pranay01
8 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 6866893
Look at the form's controlbox property.

Also, the form's borderstyle property can affect the presence of a close box, since control boxes don't exist on dialog forms.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6866989
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Const MF_BYPOSITION = &H400&

Private Sub RemoveMenus(frm As Form, _
    remove_restore As Boolean, _
    remove_move As Boolean, _
    remove_size As Boolean, _
    remove_minimize As Boolean, _
    remove_maximize As Boolean, _
    remove_separator As Boolean, _
    remove_close As Boolean)
Dim hMenu As Long
   
    ' Get the form's system menu handle.
    hMenu = GetSystemMenu(hwnd, False)
   
    If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
    If remove_separator Then DeleteMenu hMenu, 5, MF_BYPOSITION
    If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
    If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
    If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
    If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
    If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub Form_Load()
    ' Remove the Close system menu item and the
    ' menu separator.
    RemoveMenus Me, False, False, _
        False, False, False, True, True
End Sub

Cheers
0
 
LVL 4

Accepted Solution

by:
trkcorp earned 50 total points
ID: 6867043
aikimark is correct but,
What type of form is it?  On a "normal" window the control box property = false can remove it but it also removes the icon (system menu) and the Min/Max buttons.  
MDI Forms don't have these properties but on a MDI form GetWindowLong & SetWindowLong APIs can accomplish the same thing. But, I do not believe you can get rid of the close and maintain the min/max buttons nor can you just disable the close button.  (Someone prove me wrong, please!)
If you want MDI form code to do what I described, tell me and I will post it for you.
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6867061
Ark, you proved me wrong and I thank you!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:Crash2100
ID: 6867098
a simple thing you could do is put the following line of code in the form's unload event.  And this will make it so whenever you click the close button it won't do anything.

Private Sub Form_Unload(Cancel As Integer)
    Cancel = -1
End Sub
0
 

Author Comment

by:pranay01
ID: 6867240
thank u trkcorp!
plz post the codes for an MDI form for me !!
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6867325
Ark's Code would work for you too, and maybe you can mix & match but here you go:

'This will set the form to fixed, remove the min/max buttons, tell the user not to try and reset the
size and prevent him from doing so.

Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long

Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000  
Const WS_MAXIMIZEBOX = &H10000
Const WS_SYSMENU = &H80000

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long

CurStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
'you can remove any of these to affect the form as you wish.
CurStyle = CurStyle And Not (WS_MINIMIZEBOX) 'max box
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX) 'min box
CurStyle = CurStyle And Not (WS_THICKFRAME) 'set fixed size
CurStyle = CurStyle And Not (WS_SYSMENU) 'like control box = false on "normal" form

NewStyle = SetWindowLong(Me.hwnd, GWL_STYLE, CurStyle)
End Sub

'add this if you want, it kind of depends on whether you
'leave the system menu intact (see Ark's stuff) whether you
'would want this or whether you set a fixed border.
Private Sub MDIForm_Resize()
Static bMsgd As Boolean
Me.WindowState = vbNormal
If Not bMsgd Then
MsgBox "You are not allowed to resize this window!"
bMsgd = True
Else
bMsgd = False
End If
End Sub




0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6867535
I like the way this works with Ark's code used too.  Try this:

Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
  (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
  ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Const MF_BYPOSITION = &H400&

Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const WS_SYSMENU = &H80000

Private Sub RemoveMenus(frm As Form, _
   remove_restore As Boolean, _
   remove_move As Boolean, _
   remove_size As Boolean, _
   remove_minimize As Boolean, _
   remove_maximize As Boolean, _
   remove_separator As Boolean, _
   remove_close As Boolean)
Dim hMenu As Long
   
' Get the form's system menu handle.
hMenu = GetSystemMenu(hwnd, False)
'Do NOT rearrange this code as it operates by relative position!
If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
If remove_separator Then DeleteMenu hMenu, 5, MF_BYPOSITION
If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long

CurStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
CurStyle = CurStyle And Not (WS_MINIMIZEBOX)
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX)
'CurStyle = CurStyle And Not (WS_THICKFRAME)
'CurStyle = CurStyle And Not (WS_SYSMENU)

NewStyle = SetWindowLong(Me.hwnd, GWL_STYLE, CurStyle)

RemoveMenus Me, True, False, _
    False, True, True, True, True

Me.Show

End Sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA to format 3 80
VB6 add a minute to the date time value 8 47
add text to end of existing text in file 16 62
Hide vba in gp 7 49
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now