Visually remove/disable close button from title bar .NET

前端 未结 13 964
后悔当初
后悔当初 2020-12-03 21:51

I have been asked to remove or disable the close button from our VB .NET 2005 MDI application. There are no native properties on a form that allow you to grey out the close

13条回答
  •  情歌与酒
    2020-12-03 22:31

    Based on the latest information you added to your question, skip to the end of my answer.


    This is what you need to set to false: Form.ControlBox Property

    BUT, you will lose the minimize and maximize buttons as well as the application menu (top left).

    As an alternative, override OnClose and set Cancel to true (C# example):

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
        {
            e.Cancel = true;
        }
    
        base.OnFormClosing(e);
    }
    

    If neither of these solutions are acceptable, and you must disable just the close button, you can go the pinvoke/createparams route:

    How to disable close button from window form using .NET application

    This is the VB version of jdm's code:

    Private Const CP_NOCLOSE_BUTTON As Integer = &H200
    Protected Overloads Overrides ReadOnly Property CreateParams() As    CreateParams
       Get 
          Dim myCp As CreateParams = MyBase.CreateParams 
          myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON 
          Return myCp 
       End Get 
    End Property 
    

提交回复
热议问题