开发者

How to make the Close button disabled in a windows Form using C# coding

every one. I just want to make the Close button disabled on a button click event using C#.net. I am trying 开发者_JAVA百科for this but not correctly sure it's correct.

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


Add the following library

using System.Runtime.InteropServices;

Declare the following as class level variable

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);

In the Form_Load() event, write the following code

private void Form1_Load(object sender, EventArgs e)
{
        IntPtr hMenu = GetSystemMenu(this.Handle, false);
        int menuItemCount = GetMenuItemCount(hMenu);
        RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

}

Disable the Close 'X' button

Just modify it for your ClickEvent


private const int dis_close_button = 0x200;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams ObjCP = base.CreateParams;
            ObjCP.ClassStyle = ObjCP.ClassStyle | dis_close_button;
            return ObjCP;
        }
    }

Try, this...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜