none
Disable The Close Button RRS feed

Answers

  • Keep in mind that you can do it. But most people see it as a kind of virus. 



    Success
    Cor

    Sunday, June 30, 2019 4:40 PM
  • Hi,

    demo:

    Public Class Form1
        Dim tf As Boolean = True
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = "Close"
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            e.Cancel = tf
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            tf = IIf(tf = True, False, True)
            Label1.Text = IIf(tf = True, "Close", "Open")
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 7:12 AM
    Moderator

All replies

  • Hi

    You can Handle the FormClosing event and decide whether to continue and Close the Form or, skip the Close and Exit the Sub. If you have a variable or some other object that you can test to make the decision to Close or not to Close, that is the question.


    Regards Les, Livingston, Scotland

    Saturday, June 29, 2019 2:57 PM
  • Hi,
    you can disable the close button ("X"):

    Public Class Form1
    
      Private Const CP_NOCLOSE_BUTTON As Integer = &H200
      Protected Overrides ReadOnly Property CreateParams As CreateParams
        Get
          Dim myCp = MyBase.CreateParams
          myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
          Return myCp
        End Get
      End Property
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, June 29, 2019 3:18 PM
  • Hi Peter:

    Thanks for your reply. How would you turn enable the close button again?

    Thanks

    Saturday, June 29, 2019 3:24 PM
  • Hi,
    CreateParams will be used only once when the form will be created. 

    You can hide the whole ControlBox:

    Public Class Form1
    
      Private WithEvents btn As New Button With {.Text = "Set On/Off", .Dock = DockStyle.Top}
      Private OnOff As Boolean = True
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(btn)
      End Sub
    
      Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        OnOff = Not OnOff
        Me.ControlBox = OnOff
      End Sub
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, June 29, 2019 3:37 PM
  • Keep in mind that you can do it. But most people see it as a kind of virus. 



    Success
    Cor

    Sunday, June 30, 2019 4:40 PM
  • Hi,

    demo:

    Public Class Form1
        Dim tf As Boolean = True
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = "Close"
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            e.Cancel = tf
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            tf = IIf(tf = True, False, True)
            Label1.Text = IIf(tf = True, "Close", "Open")
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 1, 2019 7:12 AM
    Moderator