Disable/Enable Close Button in Windows Form using VB.NET

This tutorial will teach you how to create program that can disable and enable a close button in the windows form using vb.net language. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add a GroupBox and two buttons for Disable and Enable Close button named Button1 and Button2 respectively. You must design your interface like this: output 3. Now, we will do the coding. We will create first the enums named Choice and Position to get the Position of closing and the options such as disable or enable the close button.
  1. Enum choice
  2. Disable = 0
  3. Enable = 1
  4. End Enum
  5. Enum Position
  6. rmclose = 6
  7. End Enum
Create also a structure named xPont to get the coordinates.
  1. Structure xPont
  2. Dim x, y As Int64
  3. End Structure
Declare this functions using the user32 library.
  1. Declare Function RemoveMenu Lib "user32" (ByVal menu As Int32, ByVal Pos As Int32, ByVal u As Int32) As Int32
  2. Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Int32, ByVal rseve As Int32) As Int32
  3. Declare Function WindowFromPoint Lib "user32" (ByVal x As Int64, ByVal y As Int64) As Int32
  4. Private Const MF_POSITION As Int32 = &H400
Now, lets code for the Disable Close Button. Have this code below:
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim menu As Int64
  3. 'disable the close button
  4. menu = GetSystemMenu(Me.Handle.ToInt32, choice.Disable)
  5. RemoveMenu(CInt(menu), Position.rmclose, MF_POSITION)
  6. End Sub
And the enable close button with this:
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. Dim menu2 As Int64
  3. 'enable the close button
  4. menu2 = GetSystemMenu(Me.Handle.ToInt32, choice.Enable)
  5. RemoveMenu(CInt(menu2), Position.rmclose, MF_POSITION)
  6. End Sub
Download the source code and try it! For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Submitted byForrest Trump (not verified)on Fri, 03/22/2019 - 20:05

WindowFromPoint and xPont is unneccessary.

Add new comment