Home All Groups Group Topic Archive Search About

disable Close button (X)

Author
23 Jan 2006 2:23 PM
Sam
Hi,
I've seen a large number of posts about this subject, and one answer
everyone seems to be happy with is the following:

Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_NOCLOSE As Integer = &H200
            cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
            Return cp
        End Get
    End Property

However I must say, it does NOT work with my environement (Framework
2.0, IDE : VBExpress). What I get is a close button being offset to the
right, so it is almost visible but not quite. I'm still  able to click
on a part of it and close the form.
Am I the only one in this situation ?

Thanks

Author
23 Jan 2006 2:30 PM
zacks
Using VS2005 and .NET 2.0, I disable the close button (Control Box)
with:

MyForm.ControlBox = False
Author
23 Jan 2006 2:39 PM
Sam
Thanks for the reply. However I do not want to use this property, as I
want to keep the maximise and minimise button. Sorry I should have
mention this in my first post.
Author
23 Jan 2006 2:46 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Sam" <samuelberthe***@googlemail.com> schrieb:
> I've seen a large number of posts about this subject, and one answer
> everyone seems to be happy with is the following:
>
> Protected Overrides ReadOnly Property CreateParams() As CreateParams
>        Get
>            Dim cp As CreateParams = MyBase.CreateParams
>            Const CS_NOCLOSE As Integer = &H200
>            cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
>            Return cp
>        End Get
>    End Property
>
> However I must say, it does NOT work with my environement (Framework
> 2.0, IDE : VBExpress).

The code works just fine on .NET 2.0, VS 2005 Professional, Windows XP SP2,
Visual Styles enabled.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
23 Jan 2006 3:22 PM
Sam
Herfried,

I believe you, but I swear, in my case, it does not work ;)
see the picture:

http://graphicsxp.free.fr/formclose.JPG

I can still highlight the close button at the very right of the child
form and click on it.
Can you help ?
Author
23 Jan 2006 3:24 PM
Herfried K. Wagner [MVP]
"Sam" <samuelberthe***@googlemail.com> schrieb:
> I believe you, but I swear, in my case, it does not work ;)
> see the picture:

Well, you forgot to say that you are talking about an MDI child form.
Sorry, I do not know a solution for the problem.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
23 Jan 2006 3:30 PM
Sam
Sorry I forgot to mention that indeed :(

anyone knows?

Thanks anyway
Author
23 Jan 2006 4:21 PM
Oenone
Sam wrote:
> Sorry I forgot to mention that indeed :(
> anyone knows?

I did this in VB6 using some API calls. The VB.NET equivalent is as follows:


\\\
Private Declare Function GetSystemMenu Lib "user32" _
  (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" _
  (ByVal hMenu As Integer, ByVal nPosition As Integer, _
  ByVal wFlags As Integer, ByVal wIDNewItem As Integer, _
  ByVal lpString As String) As Integer
Private Declare Function GetMenuItemID Lib "user32" _
  (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
Private Const MF_GRAYED As Integer = &H1
Private Const MF_BYCOMMAND As Integer = &H0

    [...]

Private Sub MDIChildForm_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.Load

  'Disable the Close button on load
  Dim hMenu As Integer
  Dim hItem As Integer

  hMenu = GetSystemMenu(Me.Handle.ToInt32, 0)
  hItem = GetMenuItemID(hMenu, 6)
  ModifyMenu(hMenu, hItem, MF_BYCOMMAND Or MF_GRAYED, -9, "&Close")

End Sub
///

There may be a better way to do this, but if so, I don't know what it is.

--

(O)enone
Author
23 Jan 2006 5:22 PM
Sam
thanks but i must be damned... :(
it gives me the exact same behaviour as described above with the link
to my screenshot...
I really don't know why I get this.