disabled close button on form

Paola

New member
Joined
Jun 24, 2003
Messages
3
Location
Italia
it is possible to render disabled (o not visible) the button of closing (x) on the form caption, leaving the title bar visible ??
Thanks,

Paola
 
Here is some code I ported from VB6 awhile back.
Code:
    Private Const MF_BYPOSITION As Integer = &H400
    Private Const MF_REMOVE As Integer = &H1000

    Private Declare Function DrawMenuBar Lib "user32" _
          (ByVal hwnd As IntPtr) As Integer

    Private Declare Function GetMenuItemCount Lib "user32" _
          (ByVal hMenu As Integer) As Integer

    Private Declare Function GetSystemMenu Lib "user32" _
          (ByVal hwnd As IntPtr, _
           ByVal bRevert As Integer) As Integer

    Private Declare Function RemoveMenu Lib "user32" _
          (ByVal hMenu As Integer, _
           ByVal nPosition As Integer, _
           ByVal wFlags As Integer) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim menuHandle As Integer
        Dim menuCount As Integer

        menuHandle = GetSystemMenu(Me.Handle, 0)

        If menuHandle > 0 Then
            menuCount = GetMenuItemCount(menuHandle)

            RemoveMenu(menuHandle, menuCount - 1, MF_REMOVE Or MF_BYPOSITION)
            RemoveMenu(menuHandle, menuCount - 2, MF_REMOVE Or MF_BYPOSITION)

            DrawMenuBar(Me.Handle)
        End If
    End Sub
Basically just grabs the system menu of the form and turns off the Close option (which also affects the control box).
 
This should do it a lot easier for you. In your load procedure insert the following code

Me.ControlBox = False
 
Just thought Id offer up a suggestion thats simple, but may not be exactly what youre looking for.

If you dont care whether the close button is visible or greyd out, you can use the Close event to cancel the operation, just be sure to use a boolean variable to determine whether or not the close operation should be canceled or not.

http://msdn.microsoft.com/library/d...emwindowsformscontrolclassvalidatingtopic.asp
 
I would recommend against that, for the simple reason that it can appear confusing to the user; if the close button appears clickable, it should do something. Otherwise the user may think something it wrong...
 
Yeah, that would be a problem. Nothing a little dialog box wouldnt fix though. "You cannot close this window because ... "
 
in the forms closing event

i think its true

or false
one of them


u put this in


e.cancel = true or false or wateva and then it makes the close button do nothing


edit

oops just saw it was posted
oh well
 
a explanation:
When I first start my Windows.Forms application, I can
close it by clicking the "X" in the upper right. After
running for a while, this button no longer works.

it is a bug (at least it seems like)

then I added a separate button, and call the close method on the
window directly, and I would want to disabled the "X".

Paola
 
There is a known issue in Windows Forms; if you are adding and removing controls dynamically at runtime, and you remove one which contains the focus, your close button will cease to work.

Im betting that if you force the focus to some control thats always there just before you remove any controls, your close button problem will go away.
 
I already gave the answer to this problem, why would he want to disguise the problem by removing the close button altogether?
 
Back
Top