Reputation: 31323
After hours of Googling on this subject matter, I found the following code snippet which effectively does disable only the close button of a form.
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
Now instead of just writing this same snippet in every form, I'm trying to figure out a way to package it into possibly a static method of a class so that I can class it by just one line from anywhere.
But I have faced a problem doing that because this snippet is also an overridden method therefore I don't know how I can put it in to another static method.
I tried the following
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Common
{
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
}
}
But it throws this error : 'WindowsFormsApplication1.Common.CreateParams': no suitable method found to override
My question is how can I make this snippet reusable?
Upvotes: 1
Views: 5326
Reputation: 6079
Enable/Disable/Hide Close button in C# windows forms
You can check this
Upvotes: 0
Reputation: 223402
Create a base class which inherits from the Form
class, and then make all your forms inherited from that class.
public class BaseForm : Form
{
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
}
public partial class Form1 : BaseForm
{
//your form code
}
Upvotes: 5