disable X on form but leave -

how can disable the X button on the form's title bar but with leaving the minimuze button on it.. I just need the minimize button only on the bar (no maximum & no X)




Answer this question

disable X on form but leave -

  • RickReis

    C#



  • Les Russell

    I usually do it by creating a non-rectangular form. Although doing this requires alot of coding because you have to write the code to allow the form to be moved. Doing it this way you can just create your own Minimize Box and Close Box.


    What langauge are you working in



  • Gholson

    Hi there,

    In the way I do it (not saying it's a good way, but I've found that it works) is to take it in 2 steps:

    • Disable the Maximize button
    • Disable the Close button (the X)
    Of course, it's not the same as removing them completely but it accomplishes the same sort of goal (i.e. not allowing a user to make use of the functionality in that button)

    Maximize Button
    =============
    This one is the easier of the two. If you select your form, in the Properties window of your IDE it will have a property called "MaximizeBox". You need to set this to False and the maximize button will be disabled.

    You could also place code in your form's load handler to do it. Below is an example (NB. Form1 is the name of my form):

    private void Form1_Load(object sender, EventArgs e)
    {
        this.MaximizeBox = false;

    }


    Close Button
    ==========
    This one involves a little bit more work. OK, first you need to place a "using System.Runtime.InteropServices" statement along with all your other "using" statements. Depending on your code, after you do this you should have something like:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    OK, now that's been taken care of, I think it's best if I paste the code example now and then explain it:

    public partial class Form1 : Form

    {

        private const int SC_CLOSE = 0xF060;

        private const int MF_GRAYED = 0x1;

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("user32.dll")]
        private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
        }

    }


    OK, now the above code sample is the user-defined code for your form. First thing you should note are the two constants, SC_CLOSE and MF_GREYED. SC_CLOSE is a Windows constant that refers to the close button (the X) in a window and MF_GREYED means that something is greyed out. Basically what we have to do is use these constants as part of the effort to grey out the close button on a form.

    The next thing you will see is two functions that have been imported from user32.dll. By their naming, we can see that one of them is used to get a handle to the menu that is displayed at the top of the form (i.e. the one that has the maximize, minimize and close buttons) and the other one is used to set whether these are enabled or disabled.

    Now, you need to define a handler for the Paint event of your form. This is easily handled through the IDE as follows:

    1) Click on your form

    2) In the Properties window there will be a button that looks like a lightning bolt. Click on this button to bring up a list of applicable events for the form

    3) Scroll through the list of events and there should be one called "Paint". Double click in the empty cell next to the entry for the "Paint" event.

    4) Your code window will appear with a skeleton of a function for your Paint handler, which should be called something like <Form Name>_Paint (for example, in the above sample mine is called Form1_Paint as Form1 is the name of my form)

    OK, now in your handler you need to paste the following code:

    EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);

    After doing this, your form's paint handler should look something like the Paint handler (Form1_Paint) that was in my code sample.

    Basically, this call uses GetSystemMenu() to get a handle to the menu with the close, maximize and minimize buttons. Then, the EnableMenuItem() call comes along and sets it so that the close button is greyed out (i.e. disabled).

    NB. The code needs to go in the Paint handler because otherwise the changes to the close button will not be retained when the form is repainted (e.g. when you minimise and then restore).

    Hope that helps a bit


  • marcol74

    NateV,

    Your code works fine. You have explained it very clearly. Thanks for the same. But I found a small glitch with that. When the form is minimized (provided Maximize and close should be disabled according to the requierment) and restored back, the 'X' is also enabled.

    Instead of using EnableMenuItem, if the menu item (Close button - 'X') is removed using RemoveMenuItem from the form in Form_Load event, it is not re-appearing at any cost

    const int MF_BYPOSITION = 0x400;

    [DllImport("User32")]

    private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

    [DllImport("User32")]

    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("User32")]

    private static extern int GetMenuItemCount(IntPtr hWnd);

    private void Form_Load(object sender, EventArgs e)

    {

    .

    .

    .

    IntPtr hMenu = GetSystemMenu(this.Handle, false);

    int menuItemCount = GetMenuItemCount(hMenu);

    RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

    .

    .

    .}


  • disable X on form but leave -