disable the exit button in MDI form

How to disable the exit button in MDI form in vb.net. if that button c

Page 1 of 1

14 Replies - 20814 Views - Last Post: 22 August 2007 - 10:37 PM Rate Topic: ****- 1 Votes

#1 gmbalaa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07

disable the exit button in MDI form

Post icon  Posted 10 August 2007 - 03:45 AM

Any body help me.....
pls


thanks in advance.


Gmbalaa
Is This A Good Question/Topic? 1
  • +

Replies To: disable the exit button in MDI form

#2 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 04:06 AM

im not understanding the question... explain plz
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 04:13 AM

Are you referring to the little x in the upper right hand corner?
Was This Post Helpful? 0
  • +
  • -

#4 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 04:31 AM

if its the little X in the corner then just set the form's ControlBox property to False.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 04:38 AM

He can also stop it in the Form_Closing Event (which will fire if you click the x), but I need to know what hes looking for first
Was This Post Helpful? 0
  • +
  • -

#6 gmbalaa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 05:48 AM

View PostTophCoder, on 10 Aug, 2007 - 04:31 AM, said:

if its the little X in the corner then just set the form's ControlBox property to False.



thanks for your valuable reply.. but the icon of the MDI form also be disabled. Is there any other method to disable the exit button..



View PostTophCoder, on 10 Aug, 2007 - 04:06 AM, said:

im not understanding the question... explain plz



In right top corner minimize,maximize and also the exit button appear in MDI form,I want to disable that event..



View PostPsychoCoder, on 10 Aug, 2007 - 04:13 AM, said:

Are you referring to the little x in the upper right hand corner?



yes of course. if you find any solution post to me..
Was This Post Helpful? 0
  • +
  • -

#7 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 05:51 AM

so your saying you have a icon for the program in the title bar, and if u use my method it takes the icon away with all the other things...

psycocoders method i think doesnt remove the button but just disables it... ithink
Was This Post Helpful? 0
  • +
  • -

#8 gmbalaa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 05:59 AM

View PostTophCoder, on 10 Aug, 2007 - 05:51 AM, said:

so your saying you have a icon for the program in the title bar, and if u use my method it takes the icon away with all the other things...

psycocoders method i think doesnt remove the button but just disables it... ithink




yes..

and also one i need another help. I write a compose mail program, On that form, I used rich text box for message content with formatted string option (bold,italic,underline,color,hyperlink and so on). while i send that content, it will convert into html format and display the message as html tag.
when the user hit the view as html button it shows the message what i have typed while compose the mail.
Was This Post Helpful? 0
  • +
  • -

#9 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: disable the exit button in MDI form

Posted 10 August 2007 - 07:18 AM

try here
http://www.vbcity.co...p=form_disablex
Was This Post Helpful? 0
  • +
  • -

#10 gmbalaa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07

Re: disable the exit button in MDI form

Posted 11 August 2007 - 12:51 AM

View PostTophCoder, on 10 Aug, 2007 - 07:18 AM, said:





thanks for your reply..

but error occured on that code

Public Sub DisableX(frm As Form)
Dim hMenu As Long, nCount As Long

'Get handle to system menu
hMenu = GetSystemMenu(frm.hwnd, 0) [color=#FF0000]' error occured here (hwnd is not a member of form)

'Get number of items in menu
nCount = GetMenuItemCount(hMenu)

'Remove last item from system menu (last item is 'Close')
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)

'Redraw menu
DrawMenuBar frm.hwnd

End Sub

View Postgmbalaa, on 11 Aug, 2007 - 12:50 AM, said:

View PostTophCoder, on 10 Aug, 2007 - 07:18 AM, said:





thanks for your reply..

but error occured on that code

Public Sub DisableX(frm As Form)
Dim hMenu As Long, nCount As Long

'Get handle to system menu
hMenu = GetSystemMenu(frm.hwnd, 0) error occured here (hwnd is not a member of form)

'Get number of items in menu
nCount = GetMenuItemCount(hMenu)

'Remove last item from system menu (last item is 'Close')
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)

'Redraw menu
DrawMenuBar frm.hwnd

End Sub

Was This Post Helpful? 0
  • +
  • -

#11 gmbalaa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07

Re: disable the exit button in MDI form

Posted 11 August 2007 - 05:55 AM

Ya i found one solution.

When the user hit the exit button,It shows the message box like "Are you sure to exit" with yes/no option.

Code here,
==================
Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim a As Integer
a = MessageBox.Show("Are you sure to close", Text, MessageBoxButtons.YesNo)
If a = 6 Then
'Application.Run()
e.Cancel = False
Else
'Application.Exit()
e.Cancel = True
End If
End Sub
======================


all of them,Thanks for your valuable replies...
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: disable the exit button in MDI form

Posted 11 August 2007 - 09:19 AM

Disabling the close button really doesnt help you, as in all title bars the user can right-click and view the system menu, which has the option to close the application, also the user can do an Alt + F4 to close the application.

You can write code to prevent both of these, but it does require some API work.

The API Code

Friend Declare Function RemoveMenu Lib "user32" _
	(ByVal systemMenu As IntPtr, ByVal itemPosition As Integer, _
	ByVal flag As Integer) As IntPtr

	Friend Declare Function GetSystemMenu Lib "user32" _
	(ByVal systemMenu As IntPtr, ByVal revert As Boolean) As IntPtr

	Friend Declare Function GetMenuItemCount Lib "user32" _
	(ByVal systemMenu As IntPtr) As Integer

	Friend Declare Function DrawMenuBar Lib "user32" _
	(ByVal currentWindow As IntPtr) As Boolean 



Then you will need a way to remove all the menu items in the applications sytem menu to prevent closing the application this way as well.

Private Sub RemoveMenuItems(ByVal window As IntPtr)
		Dim remove As Integer = 1024
		Dim disable As Integer = 2
		Dim menu As IntPtr
		Dim itemCount As Integer

		'get the system menu of the application
		menu = GetSystemMenu(window, False)

		'get the count of menu items in the system menu
		itemCount = GetMenuItemCount(menu)

		'disable the "Close" command in the menu
		RemoveMenu(menu, itemCount - 1, disable Or remove)

		'now draw the menu bar on the application
		DrawMenuBar(window)
End Sub



Now we have disabled the close button (X in the upper right hand corner) and removed all the system menu commands (except the "Move" command). Theres one more thing to detect, and that is if the user presses Alt + F4, for that we put a small snippet of code in the Form_FormClosing event. For this just use the Form2_FormClosed event you already have.

As you can see simply disabling the close button does not stop the user from closing the application.

This is the code I use on a database application I created to prevent the user from closing the application before saving their changes.

Hope this helps :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#13 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: disable the exit button in MDI form

Posted 11 August 2007 - 11:24 AM

Another lame but working implementation would be to let the button be visible and prevent the App from shutting down.
Private Sub Form_Unload(Cancel As Integer)
Cancel = Not Cancel
End Sub


Was This Post Helpful? 0
  • +
  • -

#14 bgmacaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-July 07

Re: disable the exit button in MDI form

Posted 13 August 2007 - 06:57 AM

The correct answer is that you're trying to introduce a design flaw into your program by breaking Windows UI standards. I've most often seen your request tied to one of two things, (a) misuse of the MDI interface, or (B) placing exit code for an application in the wrong place.

For option (a), it would help to know what kind of application you're working on. I've seen this design mistake made most often in point-of-sale and kiosk apps. If you need to have that level of control, you'll find more success in either building user controls or using dynamic form building techniques than trying to force the square MDI peg into the round UI hole.

For option (B), the fix is simple. Don't put your exit code in a button or menu click event. Instead, put your exit code in the form's closing event. This insures that the code will be called correctly under all exit conditions, such as a system shutdown, with the exception of a process kill.

Note that users will quickly become frustrated when Windows UI standards are violated and will tend to dislike your application and, if they can, will use non-standard ways to exit it, such as using Task Manager to kill it.

This post has been edited by bgmacaw: 13 August 2007 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#15 Alkis  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 04-April 06

Re: disable the exit button in MDI form

Posted 22 August 2007 - 10:37 PM

<edit>forgot to quote PsychoCoder's post, anyway im refering to his post</edit>

Very nice man!

really helpfull when u need to make sure ur application saved everything

one little question, although i dont need any prevent closing code at the moment i might need later so my question is, instead of having the application not able to be closed since the very begining, u can make it unable to be closed when saving process starts and then able again right?

-Giannis

This post has been edited by Alkis: 22 August 2007 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1