简体   繁体   English

C#如何使用按钮单击事件创建代码以将表单设置回默认属性?

[英]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: 首先打开Program.cs并编辑它,看起来像这样:

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. ApplicationContext变量现在控制应用程序的生命周期,而不是Form1实例。 You can recreate the form with code like this in Form1: 您可以使用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 Load事件:

// 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 : 然后在按钮的Click事件中:重置默认值:

// 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 : 有一些更强大的方法可以实现这一点,包括使用Visual Studio的“设置”功能(在设计时和运行时):在以下位置查看它们:

How to: Create Application Settings Using the Designer 如何:使用Designer创建应用程序设置

Application Settings Overview 应用程序设置概述

How To: Write User Settings at Run Time with C# 如何:使用C#在运行时编写用户设置

How To: Read Settings at Run Time With C# 如何:使用C#在运行时读取设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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