简体   繁体   中英

C# How do I create code to set a form back to default properties, with a button click event?

使用Visual C#2008快速版,我试图在我的表单上创建一个按钮,将表单设置回默认属性,如大小,背景颜色等等......任何人都有关于我如何做这个的任何例子?

对于每个属性信息,您可以获取DefaultValueAttribute并将所需的Property设置为其值。

You cannot do this without saving the original state somewhere.

Just create some class that holds the default info:

class DefaultFormInfo
{
    int Width { get; set; }
    int Height { get; set; }
}

Then use some reflection:

static DefaultFormInfo FormInfo = new DefaultFormInfo();

void FillDefaults()
{
            foreach (PropertyInfo pinf in FormInfo.GetType().GetProperties())
            {
                pinf.SetValue(FormInfo, this.GetType().GetProperty(pinf.Name).GetValue(this, null), null);
            }
}

void Restore()
{
    foreach (PropertyInfo pinf in FormInfo.GetType().GetProperties())
    {
        this.GetType().GetProperty(pinf.Name).SetValue(this, pinf.GetValue(FormInfo, null), null);
    }
}

By far the simplest way is to just create a new instance of the form and close the old one. That requires a little bit of surgery if this is the main form of your app, closing it would terminate the program. Start by opening Program.cs and edit it so it looks like this:

static class Program {
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        AppContext = new ApplicationContext();
        AppContext.MainForm = new Form1();
        Application.Run(AppContext);
    }
    public static ApplicationContext AppContext;
}

The ApplicationContext variable now controls the lifetime of the app, instead of the Form1 instance. You can recreate the form with code like this in Form1:

    private void button1_Click(object sender, EventArgs e) {
        Form1 frm = new Form1();
        frm.StartPosition = FormStartPosition.Manual;
        frm.Location = this.Location;
        frm.Size = this.Size;
        Program.AppContext.MainForm = frm;
        frm.Show();
        this.Close();
    }

The simplest solution might be to define some Form level variables, and record the default values in an Event like the Form Load event :

// form scoped variables
private Color defaultBackColor;
private Rectangle defaultBounds;
private FormWindowState defaultFormWindowState;

// save the Form default Color, Bounds, FormWindowState
private void Form1_Load(object sender, EventArgs e)
{
    defaultBackColor = this.BackColor;
    defaultBounds = this.Bounds;
    defaultFormWindowState = this.WindowState;
}

Then in the Click Event of your button : reset the defaults :

// restore the defaults on Button click
private void btn_FormReset_Click(object sender, EventArgs e)
{
    this.WindowState = defaultFormWindowState;
    this.Bounds = defaultBounds;
    this.BackColor = defaultBackColor;
}

There are some more powerful ways of doing this involving using the 'Settings feature of Visual Studio (at Design Time and Run-Time) : check them out at :

How to: Create Application Settings Using the Designer

Application Settings Overview

How To: Write User Settings at Run Time with C#

How To: Read Settings at Run Time With C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM