disable Close button (X)

S

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
 
Z

zacks

Using VS2005 and .NET 2.0, I disable the close button (Control Box)
with:

MyForm.ControlBox = False
 
S

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.
 
H

Herfried K. Wagner [MVP]

Sam said:
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.
 
H

Herfried K. Wagner [MVP]

Sam said:
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.
 
O

Oenone

Sam said:
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.
 
S

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top