在c#中将控件和变量的状态从一个Form复制到另一个Form

时间:2016-07-21 05:48:19

标签: c# winforms reflection

我有一个Windows窗体。如果用户没有选中复选框,则在下一步按下时会打开新表单,但如果他们选择了新表单,则会使用相同的表单。
如果他们选中复选框,我想要显示当前表单的克隆(具有相同的变量和控件值),以便他们可以在以后更改值而不取消选中复选框,再按下一步,然后手动键入其他值。

Form duplicate = this;只引用相同的表单,而new(this)没有。{ 我无法尝试Form duplicate = new Form() = this,因为我的形式从早期形式中获取构造函数 谁知道怎么做?提前致谢

3 个答案:

答案 0 :(得分:1)

这是我要做的: 让我们假装你想打开"克隆"用一个按钮。 在克隆形式:

    public Form1()
    {
        InitializeComponent();
    }

    public Form1(string YourValue, int AnotherValue)     //This basically works like a constructor when the form is called
    {
        InitializeComponent();
        ValueLabel1.Text = YourValue;
        ValueLabel2.Text = Convert.ToString(AnotherValue);
    }

    private void DuplicateButton_Click(object sender, EventArgs e)
    {
        int a = 3;
        Form1 Window = new Form1(TextBox1.Text, a);
        Window.Show;
    }

我希望这对你有用

答案 1 :(得分:1)

您可以在表单中添加以下方法:

public void RestoreState(Dictionary<string, object> controlStates, 
                         Dictionary<string, object> membersStates)
{
    InternalRestoreControls(controlStates);
    InternalRestoreMembers(membersStates);
}

private void InternalRestoreControls(Dictionary<string, object> states)
{
    foreach (var state in states)
    {
        Control c = this.Controls.Find(state.Key, true).FirstOrDefault();

        if (c is TextBox)
        {
            (c as TextBox).Text = state.Value == null ? null : state.Value.ToString();
        }
        else if (c is CheckBox)
        {
            (c as CheckBox).Checked = Convert.ToBoolean(state.Value);
        }
    }
}

private void InternalRestoreMembers(Dictionary<string, object> membersStates)
{
    // you might need to tweek this a little bit based on public/instance/static/private
    // but this is not the point of your question

    BindingFlags flags = BindingFlags.Instance | BindingFlags.Static
                       | BindingFlags.Public | BindingFlags.NonPublic;

    var props = this.GetType().GetProperties(flags);
    var fields = this.GetType().GetFields(flags);

    foreach(var variable in membersStates)
    {
        var prop = props.FirstOrDefault(x => x.Name == variable.Key);

        if(prop != null)
        {
            prop.SetValue(this, variable.Value);
            continue;
        }

        var field = fields.FirstOrDefault(x => x.Name == variable.Key);

        if(field != null)
        {
            field.SetValue(this, variable.Value);
            continue;
        }
    }
}

private Dictionary<string, object> GetControlsState()
{
    return new Dictionary<string, object>()
    {
        { txtBox1.Name, txtBox1.Text },
        // continue to the rest
    };
}

private Dictionary<string, object> GetMembersState()
{
    return new Dictionary<string, object>()
    {
        { nameof(variable1), variable1 },
        // continue to the rest
    };
}

用法:

Form duplicate = new Form();
duplicate.RestoreState(this.GetControlsState(), this.GetMembersState());

答案 2 :(得分:0)

您可以使用控件名称为键的字典,并将值控制为值,并将其作为可选参数传递

public form1(string para1, int para2, Dictionary<string,object>yourDic=null)
{

}