Paola Posted June 24, 2003 Posted June 24, 2003 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 Quote
*Experts* Volte Posted June 24, 2003 *Experts* Posted June 24, 2003 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 SubBasically just grabs the system menu of the form and turns off the 'Close' option (which also affects the control box). Quote
donnacha Posted June 25, 2003 Posted June 25, 2003 This should do it a lot easier for you. In your load procedure insert the following code Me.ControlBox = False Quote Hamlet
*Experts* Volte Posted June 25, 2003 *Experts* Posted June 25, 2003 Then you remove the Min and Max buttons too... Quote
wyrd Posted June 25, 2003 Posted June 25, 2003 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 Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Volte Posted June 25, 2003 *Experts* Posted June 25, 2003 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... Quote
wyrd Posted June 25, 2003 Posted June 25, 2003 Yeah, that would be a problem. Nothing a little dialog box wouldn't fix though. "You cannot close this window because ... " Quote Gamer extraordinaire. Programmer wannabe.
Winston Posted June 25, 2003 Posted June 25, 2003 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 Quote
Paola Posted June 25, 2003 Author Posted June 25, 2003 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 Quote
*Gurus* divil Posted June 25, 2003 *Gurus* Posted June 25, 2003 Are you adding and removing controls dynamically at runtime? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Gurus* divil Posted June 25, 2003 *Gurus* Posted June 25, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
hibiki1979 Posted June 27, 2003 Posted June 27, 2003 (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 June 27, 2003 by divil Quote
*Gurus* divil Posted June 27, 2003 *Gurus* Posted June 27, 2003 I already gave the answer to this problem, why would he want to disguise the problem by removing the close button altogether? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.