Results 1 to 9 of 9

Thread: Disabling close [X] button in VB.Net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    London,UK
    Posts
    16

    Angry Disabling close [X] button in VB.Net

    See subject! Tried the VB6 implementation that I found on the site but get the error "Hwnd is not a member of 'My_Prog_name.MainForm'" when trying to compile for debugging. Any ideas anyone??!?

    I'm assuming there's probably a very easy way to do this in VB.net instead of grabbing the forms handle.

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If I remember correctly, the VB6 property Hwnd is called Handle in VB.NET.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    London,UK
    Posts
    16
    Thanks Frans, but I did try that and got a "Value of Type 'System.InPtr' (referring to 'Me.Handle' of course) cannot be converted to Long. This makes sense when you look at the code:

    'Declarations

    'Paste this into the declaration section:

    Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, ByVal bRevert As Long) As Long

    Declare Function GetMenuItemCount Lib "user32" _
    (ByVal hMenu As Long) As Long

    Declare Function DrawMenuBar Lib "user32" _
    (ByVal hwnd As Long) As Long

    Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, ByVal nPosition As Long, _
    ByVal wFlags As Long) As Long
    Public Const MF_BYPOSITION = &H400&
    Public Const MF_REMOVE = &H1000&
    Form Code

    'Then paste this into the form:

    Private Sub Form_Load()
    Dim hSysMenu As Long
    Dim nCnt As Long

    'First, show the form
    Me.Show

    'Get handle to our form's system menu
    '(Restore, Maximize, Move, close etc.)
    hSysMenu = GetSystemMenu(Me.hwnd, False)

    If hSysMenu Then
    'Get System menu's menu count
    nCnt = GetMenuItemCount(hSysMenu)

    If nCnt Then

    'Menu count is based on 0 (0, 1, 2, 3...)

    RemoveMenu hSysMenu, nCnt - 1, _
    MF_BYPOSITION Or MF_REMOVE

    RemoveMenu hSysMenu, nCnt - 2, _
    MF_BYPOSITION Or MF_REMOVE 'Remove the seperator

    DrawMenuBar Me.hwnd
    'Force caption bar's refresh. Disabling X button

    Me.Caption = "Try to close me!"
    End If
    End If
    End Sub



    Look at the declarations section. We are calling "user32.dll" to handle the window. I'm sure the .Net framework probably has a more efficient way of doing this.

    This is where I'm stuck. I think I'm nearly there but need re-assurance that I'm going the right way about this.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    API usage is different between VB6 and .NET you cant just take Vb6 api code and paste into .NET. paremeter types have to be change Long to Integer, Integer to Short, cant use Any, etc. Plus you have to use COM interop. But instead of doing all that, ....

    Set the property Form.ControlBox to false.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    London,UK
    Posts
    16
    Getting there. I do understand that you can't just paste VB6 code into VB.net and "it'll work". I used the VB6 code as a means of getting an idea of how it used to be done. As it turns out I don't think any of the code will be of any use to me.

    Thanks for the advice Cander but what I need is a little more sophisticated than just dis-abling the controlbox!!

    The program still needs to minimize and maximize from the toolbar but it can't close as there will probably be open files if the main process isn't stopped in the correct manner. This is the main reason that I need to handle the close box directly. The other reason is so that I can deploy it to the system tray in later releases when the close button is clicked.

    Can anybody provide me with a decent answer??!? There must be some-one with the knowledge!!

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    OK, I'm sure there must be a .NET way to do it, but here is your code converted as it was.

    In a module:
    VB Code:
    1. Declare Function GetSystemMenu Lib "user32" _
    2. (ByVal hwnd As IntPtr, ByVal bRevert As Integer) As IntPtr
    3.  
    4.     Declare Function GetMenuItemCount Lib "user32" _
    5.     (ByVal hMenu As IntPtr) As Integer
    6.  
    7.     Declare Function DrawMenuBar Lib "user32" _
    8.     (ByVal hwnd As IntPtr) As Integer
    9.  
    10.     Declare Function RemoveMenu Lib "user32" _
    11.     (ByVal hMenu As IntPtr, ByVal nPosition As Integer, _
    12.     ByVal wFlags As Integer) As Integer
    13.     Public Const MF_BYPOSITION As Integer = &H400
    14.     Public Const MF_REMOVE As Integer = &H1000

    In your form (I used form2):
    VB Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim hSysMenu As IntPtr
    3.         Dim nCnt As Long
    4.  
    5.         'First, show the form
    6.         Me.Show()
    7.  
    8.         'Get handle to our form's system menu
    9.         '(Restore, Maximize, Move, close etc.)
    10.         hSysMenu = GetSystemMenu(Me.Handle, 0)
    11.  
    12.         If Not hSysMenu.Equals(0) Then
    13.             'Get System menu's menu count
    14.             nCnt = GetMenuItemCount(hSysMenu)
    15.  
    16.             If nCnt Then
    17.  
    18.                 'Menu count is based on 0 (0, 1, 2, 3...)
    19.  
    20.                 RemoveMenu(hSysMenu, nCnt - 1, _
    21.                 MF_BYPOSITION Or MF_REMOVE)
    22.  
    23.                 RemoveMenu(hSysMenu, nCnt - 2, _
    24.                 MF_BYPOSITION Or MF_REMOVE) 'Remove the seperator
    25.  
    26.                 DrawMenuBar(Me.Handle)
    27.                 'Force caption bar's refresh. Disabling X button
    28.  
    29.                 Me.Text = "Try to close me!"
    30.             End If
    31.         End If
    32.  
    33.     End Sub

  7. #7
    Junior Member
    Join Date
    Feb 2002
    Location
    Colorado
    Posts
    23

    Re: Disabling close [X] button in VB.Net

    Originally posted by DWetherilt
    The program still needs to minimize and maximize from the toolbar but it can't close as there will probably be open files if the main process isn't stopped in the correct manner. This is the main reason that I need to handle the close box directly.
    I'm still a little new at this , so I apologize in advance if I am misunderstanding your situation, but I know that if the user closes a form using the X button, you can catch the event, and even cancel it, if you want. So another way to solve your problem may be to catch that event and write code that makes sure that the process is stopped correctly before closing the program.

    I saw some sample code somewhere (I can try to find it again, if you would like) that showed how to catch a close event and determine where it came from (closing the form by hitting the close button, closing the parent form, using the form menu, etc.). The point of the sample code that I saw was to handle the scenario when the user clicked the close [X] button. If anyone wants to see the code, let me know I'll try to find it and post it here.
    LDD
    ----------------
    "Character is what you do in the dark." Dwight L. Moody

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    you might find this helpful

    http://www.vbforums.com/showthread.p...hreadid=159297

    that link is to a thread where i talk to myself about adding a menu item to a forms system menu not it's menu bar it's system menu

    after rereading that damn i sound like a jerk
    Magiaus

    If I helped give me some points.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    London,UK
    Posts
    16

    Cool Frans is the man!!

    Frans. Thanks for doing that conversion - it seems blatently obvious now!!! It worked a treat!! Hope I can help you one day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width