Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

it is possible to render disabled (o not visible) the button of closing (x) on the form caption, leaving the title bar visible ??

Thanks,

 

Paola

  • *Experts*
Posted
Here is some code I ported from VB6 awhile back.
    Private Const MF_BYPOSITION As Integer = &H400
   Private Const MF_REMOVE As Integer = &H1000

   Private Declare Function DrawMenuBar Lib "user32" _
         (ByVal hwnd As IntPtr) As Integer

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

   Private Declare Function GetSystemMenu Lib "user32" _
         (ByVal hwnd As IntPtr, _
          ByVal bRevert 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 Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim menuHandle As Integer
       Dim menuCount As Integer

       menuHandle = GetSystemMenu(Me.Handle, 0)

       If menuHandle > 0 Then
           menuCount = GetMenuItemCount(menuHandle)

           RemoveMenu(menuHandle, menuCount - 1, MF_REMOVE Or MF_BYPOSITION)
           RemoveMenu(menuHandle, menuCount - 2, MF_REMOVE Or MF_BYPOSITION)

           DrawMenuBar(Me.Handle)
       End If
   End Sub

Basically just grabs the system menu of the form and turns off the 'Close' option (which also affects the control box).

Posted

Just thought I'd offer up a suggestion that's simple, but may not be exactly what you're looking for.

 

If you don't care whether the close button is visible or grey'd out, you can use the Close event to cancel the operation, just be sure to use a boolean variable to determine whether or not the close operation should be canceled or not.

 

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassvalidatingtopic.asp

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted
I would recommend against that, for the simple reason that it can appear confusing to the user; if the close button appears clickable, it should do something. Otherwise the user may think something it wrong...
Posted
Yeah, that would be a problem. Nothing a little dialog box wouldn't fix though. "You cannot close this window because ... "
Gamer extraordinaire. Programmer wannabe.
Posted

in the forms closing event

 

i think its true

 

or false

one of them

 

 

u put this in

 

 

e.cancel = true or false or wateva and then it makes the close button do nothing

 

 

edit

 

oops just saw it was posted

oh well

Posted

a explanation:

When I first start my Windows.Forms application, I can

close it by clicking the "X" in the upper right. After

running for a while, this button no longer works.

 

it is a bug (at least it seems like)

 

then I added a separate button, and call the close method on the

window directly, and I would want to disabled the "X".

 

Paola

  • *Gurus*
Posted

There is a known issue in Windows Forms; if you are adding and removing controls dynamically at runtime, and you remove one which contains the focus, your close button will cease to work.

 

I'm betting that if you force the focus to some control that's always there just before you remove any controls, your close button problem will go away.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted (edited)

Here is a code I got in from somewhere.(Forgooten where? =Þ)

 

It shd works.

 

Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
   Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer

   Public Const SC_CLOSE = &HF060&
   Public Const MF_BYCOMMAND = &H0&

   Function RemoveXButton(ByVal iHWND As Integer) As Integer
       Dim iSysMenu As Integer
       iSysMenu = GetSystemMenu(iHWND, False)
       Return RemoveMenu(iSysMenu, SC_CLOSE, MF_BYCOMMAND)
   End Function

 Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       'Remove the close button of the form
       RemoveXButton(Me.Handle().ToInt32())
   End Sub

Edited by divil

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...