0

I have an application where I do not want the user to be able to close the form. Okay, easy enough. Just set ControlBox = false for the form.

However, I would like to keep the application icon in the upper left corner of the form. Minor I know, but details mean something to me.

Setting the Controlbox = false also makes the application's icon go away in the upper left corner of the form. Is there a way around this??

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
busarider29
  • 185
  • 5
  • 18

2 Answers2

2

Here is the code I used.

My VB.Net version of it.

Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overrides ReadOnly Property CreateParams() As Windows.Forms.CreateParams
    Get
        Dim mdiCp As Windows.Forms.CreateParams = MyBase.CreateParams
        mdiCp.ClassStyle = mdiCp.ClassStyle Or CP_NOCLOSE_BUTTON
        Return mdiCp
    End Get
End Property
LeeG
  • 708
  • 5
  • 14
  • Very nice. The Close button still appears, but is greyed and disabled. The Maximise and Minimise buttons still work. – Blackwood Feb 04 '15 at 14:04
  • Do I just copy and paste the code or is there something more I need to do to it in my specific code. I am not familiar with creating my own properties as I'm still very new to VB and VS. The copied code is not working for me. I have no errors and it compiles fine, but its not working. Close button is not greyed out and still works and closes the form. – busarider29 Feb 04 '15 at 16:13
  • Nevermind, its working now for some reason. Maybe I didn't build it correctly or something?? Anyway, works great. Thank you. – busarider29 Feb 04 '15 at 16:32
  • Okay, will do. Suggestion to the moderators of this site though - Change the check mark icon for accepting answers to maybe a button that states "Accept this Answer". I was looking all over the screen for something to show me where to accept. One doesn't know unless he/she mouses over the check icon. I did a google search and it seems there have been many new users that had trouble finding the check answer button, or that it was even a button at all. Maybe adjust the check to make it look more like a clickable button?? Just a helpful suggestion. – busarider29 Feb 20 '15 at 21:38
1

I needed an option that would disable the close conditionally (like the standard MessageBox does for a YesNo question) so the accepted answer wouldn't work for me or possibly I failed to see how I could get it to work. I ended up with this

Private Const MF_BYPOSITION As Int32 = &H400
Private Const MF_REMOVE As Int32 = &H1000
Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr
Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32
Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32

Public Sub DisableCloseButton(ByVal hwnd As IntPtr)
    Dim hMenu As IntPtr, n As Int32
    hMenu = GetSystemMenu(hwnd, 0)
    If Not hMenu.Equals(IntPtr.Zero) Then
        n = GetMenuItemCount(hMenu)
        If n > 0 Then
            RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)
            RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)
            DrawMenuBar(hwnd)
        End If
    End If
End Sub

Call it via

DisableCloseButton(MyForm.Handle)

As I was using it for a custom message box, I didn't test how to re-enable.

topshot
  • 885
  • 6
  • 21