简体   繁体   English

C# 保存带有所有控件的表单的 state

[英]C# saving state of the form with all controls

I have a form and I have some buttons doing stuff.我有一个表格,我有一些按钮可以做一些事情。

When I press buttons the windows form controls, like textboxes or group-boxes, buttons appear and disappear and change place on my form, for it is a dynamic form:)当我按下 windows 表单控件的按钮时,例如文本框或组框,按钮会出现和消失并改变我表单上的位置,因为它是一个动态表单:)

However, what I'd like to do is have a button ( BACK ) that will get my form to the state it was before an action of a button, putting back the controls in the place and state they were before action.但是,我想做的是有一个按钮( BACK ),它将使我的表单返回到按钮操作之前的 state ,将控件放回原处, state 它们在操作之前。

I thought of a C class MyState() that will have something like an array of Form1.我想到了一个C class MyState() ,它将具有类似于 Form1 数组的内容。 I will be saving the form state in that array and when I'll press the back button to get from array that "copy" of the Form state and maybe an index for indexing states.我将在该数组中保存表格 state,当我按下back按钮从数组中获取表格 state 的“副本”时,可能还有一个用于索引状态的索引。

I have no idea how to implement this, unfortunately.不幸的是,我不知道如何实现这一点。 :| :|

Can anyone show me the right way to do this?谁能告诉我正确的方法吗?

class Mystate
{
    private Form1 [] state;

    public Mystate(int n)
    {
        this.state = new Form1[n];
    }

    public Form1 this[int index]
    {
        get
        {
            return state[index];
        }
        set
        {
            this.state[index] = value;
        }
    }
}

Sounds like you want an high level undo/redo feature for your forms.听起来您想要 forms 的高级撤消/重做功能。

Here is a framework for such things: http://www.codeproject.com/Articles/10576/An-Undo-Redo-Buffer-Framework这是此类事情的框架: http://www.codeproject.com/Articles/10576/An-Undo-Redo-Buffer-Framework

Here is an answer that is close but not exactly the same as your question (The pattern implimented is the same though): How to implement good and efficient undo/redo functionality for a TextBox这是一个接近但与您的问题不完全相同的答案(虽然实现的模式是相同的): How to implement good and efficient undo/redo functionality for a TextBox

MementoPattern: http://www.codeproject.com/Articles/18025/Generic-Memento-Pattern-for-Undo-Redo-in-C MementoPattern: http://www.codeproject.com/Articles/18025/Generic-Memento-Pattern-for-Undo-Redo-in-C

Nothing like this is built-in.没有像这样的东西是内置的。 You have to do this on your own.你必须自己做这件事。

I'd do it like this: First, define precisely what state you want to save.我会这样做:首先,准确定义您要保存的 state。 Example:例子:

Control.Bounds
Control.Text
Checkbox.IsChecked
NumericUpDown.Value
...

Now we know exactly what needs to be saved.现在我们确切地知道需要保存什么。

Seconds, we need a way to create a snapshot of the current state of the form and recursively for all controls.秒,我们需要一种方法来创建当前 state 窗体的快照,并递归地为所有控件创建快照。 You can implement this using reflection so that everything will be automatic no matter how many controls you have.您可以使用反射来实现这一点,这样无论您有多少控件,一切都会自动进行。

Third, you need to be able to apply a snapshot to an instance of Form.第三,您需要能够将快照应用于 Form 的实例。 This is the opposite process of (2).这是(2)的相反过程。 This also can be done using reflection.这也可以使用反射来完成。

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

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