Visually remove/disable close button from title bar .NET

前端 未结 13 943
后悔当初
后悔当初 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:20

    What jmweb said here is OK as well. The X close button won't go if you cancel the event on form closing. But doing so, you need to release the processes the form needs and then closing the form.

    Me.Dispose()
    Me.Close()
    

    This worked for me using Menu Strip.

    0 讨论(0)
  • 2020-12-03 22:20

    go to properties and select from bored style as none

    0 讨论(0)
  • 2020-12-03 22:27

    You can disable the close button and the close menu item in the system menu by changing the "class style" of the window. Add the following code to your form:

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

    This will not just stop the window from getting closed, but it will actually grey out the button. It is C# but I think it should be easy to translate it to VB.

    0 讨论(0)
  • 2020-12-03 22:28

    You should be able to override the OnClose event of the form. This is common when an application minimizes to the System Tray when "closed".

    0 讨论(0)
  • 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 
    
    0 讨论(0)
  • 2020-12-03 22:32
    Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Beep()
        e.Cancel = True
    End Sub
    
    0 讨论(0)
提交回复
热议问题