Disable the X

Is there anyway that i can disable the X close button but i still want to have maximize and minimize. I just want X to be either gone or X to be grayed out so it can't be used.

Answer this question

Disable the X

  • gi aai

    Dim frm as new FormX

    frm.controlbox = false


  • Barry Dyson

    I know about that but that disables the entire control box and i need minimize and maximize in there, i just dont want the X close in there.
  • fitzglitz

    Or without the API:


    Protected Overrides ReadOnly Property CreateParams() As CreateParams

    Get

    Dim cp As CreateParams = MyBase.CreateParams

    Const CS_NOCLOSE As Integer = &H200

    cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE

    Return cp

    End Get

    End Property


     


  • kkennedy1008

    Does anyone have an idea on how to make this work, again i want to disable the x close button and i dont want it to be visible on the form but i do want the minimize on there so the control box is not an option for me. Is this possible

  • Dinesh Patel

    Ok the code that you posted doesn't work in vb 2005, but indeed if you can give me a working code for the one you posted and maybe an idea on how to go about changing the behaviour
  • ScottMcKeown

    Thanks that was exactly what i needed Tada it works perfectly.

    do you also know the answer to make it completely gone i mean the grayout works perfect but for another application i like to have it gone.

  • Florin Lazar - MSFT

    TaDa,

    This works great and does just what I want. I translated it into C#:

    private const int CS_NOCLOSE = 0x200;
    protected override CreateParams CreateParams
    {
       
    get
       
    {
          CreateParams cp =
    base.CreateParams;
          cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
          
    return cp;
       }
    }

    JWTK


  • Nookie

    Hi Andrej,

    Closing event of a form as that signature :

    Private Sub frmConsigne_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       ' Code here
    End Sub

     

    Therefore, "CloseReason" is not a member of System.ComponentModel.CancelEventArgs

    How to deal with then

    Thanks in advance.
    With regards,
    AlkPonn

  • WThabet

    Why don't you simply change the behaviour of your X button Instead of removing it, assign another function to it (minimize window, message box, ...). Why waste a button  Smile


    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       if (e.CloseReason == CloseReason.UserClosing)
       {
          this.WindowState = FormWindowState.Minimized;
          e.Cancel = true;
       }
    }

     



  • Auschunky





    Option Explicit On
    Option Strict
    On

    Public

    Class CloseButton   Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As Integer
      Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer
      Private Const SC_CLOSE As Integer = &HF060
      Private Const MF_BYCOMMAND As Integer
    = &H0
      Private Const MF_GRAYED As Integer
    = &H1
      Private Const MF_ENABLED As Integer
    = &H0
      Private Sub New()
      End Sub
      Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form)
        ' The return value specifies the previous state of the menu item (it is either
        ' MF_ENABLED or MF_GRAYED). 0xFFFFFFFF indicates   that the menu item does not exist.
        Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or
    MF_GRAYED)
          Case
    MF_ENABLED
          Case
    MF_GRAYED
          Case
    &HFFFFFFFF
            Throw New
    Exception("The Close menu item does not exist.")
          Case
    Else
        End
    Select
      End
    Sub

    End

    Class





     



    try this in the form load event CloseButton.Disable(me)


  • Quinch

    Its working !!

    Grande TaDa !!!

    Me salvaste la tarde....


  • SammyR.

    I'm sorry, the code was in C#. The VB equivalent would be:


    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            WindowState = FormWindowState.Minimized
            e.Cancel = True
        End If
    End Sub

     


    Basically, what this snippet does, is preventing the form from closing, (e.Cancel = True) and instead it minimizes it (WindowState change). Note that it not only affects the X button but prevents the form from closing at all (I can't say if this is the behaviour you want).



  • Christopher Zahrobsky

    hi,

    I tried out this coding. but i am getting this error.

    'User.CreateParams': no suitable method found to override

    Can u suggest some solution for this.

    Thanks..



  • Disable the X