How to disable the close window ("X") on the right top corner of a form in VB.NET.
I would like to keep minimise window and maximise window enable though.
Printable View
How to disable the close window ("X") on the right top corner of a form in VB.NET.
I would like to keep minimise window and maximise window enable though.
VB Code:
'In a module 'Call from any form, passing the Form object Option Explicit On Private Const MF_DISABLED = &H2& Private Const MF_BYPOSITION = &H400& Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Int32, ByVal bRevert As Int32) As Int32 Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Integer) As Integer Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Integer) As Integer Public Function DisableX(ByRef oForm As System.Windows.Forms.Form) '<VB/OUTLOOK GURU 11/09/2004 - REMOVES FORM SYSTEM MENU ITEMS BY POSITIONS> Dim hMenu As Int32 Dim nCount As Integer hMenu = GetSystemMenu(oForm.Handle.ToInt32, 0) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) Call DrawMenuBar(oForm.Handle.ToInt32) End Function
Hi,
You could put
If bCheck = False then e.Cancel = True
in the closing event of the form. Then when you come to the point in your code wher you want the form to close, set bCheck to true. (bCheck must have form scope of course)
True, but I like the greyed out disabled look and feel of removing the close option from the system menu. :D
By the time I'd written all that code it would be ME that had the "Greyed out look" :bigyello:
Grey huh, :D
VB Code:
'In Form1 DisableX(Me)
damn...posted in the wrong thread....good beer tho
kevin :wave:
Hello RobDog888,
I used the code that you provided in my module where I have some more codes for different forms. So my module looks like as foloows now:
VB Code:
Module mdlMain Public optionInterface As New frmOptions Public insertNewmember As New frmInsertmember Option Explicit On Private Const MF_DISABLED = &H2& Private Const MF_BYPOSITION = &H400& Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Int32, ByVal bRevert As Int32) As Int32 Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Integer) As Integer Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Integer) As Integer Public Function DisableX(ByRef oForm As System.Windows.Forms.Form) '<VB/OUTLOOK GURU 11/09/2004 - REMOVES FORM SYSTEM MENU ITEMS BY POSITIONS> Dim hMenu As Int32 Dim nCount As Integer hMenu = GetSystemMenu(oForm.Handle.ToInt32, 0) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) Call DrawMenuBar(oForm.Handle.ToInt32) End Function End Module
VB.NET does not like "Option Explicit On" so I commented it out and run the code. I could not achive the goal.
I am new in VB.NET so please bear with me and let me know how to handle this situation. Thank you.
It should go like this...
VB Code:
Option Explicit On Module mdlMain Public optionInterface As New frmOptions Public insertNewmember As New frmInsertmember Private Const MF_DISABLED = &H2& Private Const MF_BYPOSITION = &H400& Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As Int32, ByVal bRevert As Int32) As Int32 Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Integer) As Integer Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Integer) As Integer Public Function DisableX(ByRef oForm As System.Windows.Forms.Form) '<VB/OUTLOOK GURU 11/09/2004 - REMOVES FORM SYSTEM MENU ITEMS BY POSITIONS> Dim hMenu As Int32 Dim nCount As Integer hMenu = GetSystemMenu(oForm.Handle.ToInt32, 0) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) Call DrawMenuBar(oForm.Handle.ToInt32) End Function End Module
Hello RobDog888,
I tried the new version of this code you provided. Now when I run the program the close window ('X') is still visible on the frames ( in my case I have 3 frames so far) ie. Login Frame, User Option Frame, New Member Frame. When I click the ('X'), it closed the frame. Thus I cannot achieve my goal.
Any suggestion. Thank you.
I think you mean Form not frame ;)
Can you post your code that calls the function? Are you using MDI forms?
Sorry that's my bad. I meant form instead of frame.
What is MDI form?
I attched the whole project so that you can understand better. Thank you.
Fixed. You didnt make any calls to disable the x. I changed the folder name to add at the end "_Fixed" so
you can compare projects.
I placed this in all your form load events so it wil disable the x before it loads.VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Call DisableX(Me) End Sub
Thank you very much RobDog888. I acieved my gaol this time.
I am looking for some suggestions about my project. As you have seen the skeleton of my project. I am planning to use this project to store member information and at the same time retrieving the data from the database and updating.
I am wondering whether I can use MS access on the back ground to store the date or just simple text file.
If you have any other suggestion, you are more than welcome to inform me. Thank you very much.
It probably would be best to use Access to store the information. As I usually see that we an app
is deployed and used in the field, they usually are always wanting more.
I didnt take too much time going over it but you may also want to think about how the first user account
will be created so they can log in to create others. ;)
I am not worried about login for the first user or so on since my main concern is store the member information and manupolate those information when necessary.
How do I link VB.NET and MS Access?
Thank you.
You need to use the OleDbDataAdapter and OleDbConnection Objects in the Data toolbox.
Then when your setup you can use the DataSet or DataView.
Thank you very much. Please bear with since I am very much new with this terms. Could you please le me know about the foloowoing inquires?
How to use the OleDbDataAdapter and OleDbConnection Objects in the Data toolbox?
How to use the DataSet or DataView?
I dont have a whole lot of experience useing them yet, but I think you would get a better response if you
create a new thread on this new issue ;)
I will try to continue in there.