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

本文关键字:窗体 一个 复制 另一个 控件 状态 变量 | 更新日期: 2023-09-27 17:58:02

我有一个windows窗体。如果用户没有选中复选框,稍后按next时会打开一个新窗体,但如果用户选中了它,则会使用相同的窗体
如果他们选中复选框,我希望显示当前表单的克隆(具有相同的变量和控制值),这样他们以后就可以更改值,而无需取消选中复选框、再次按"下一步"并手动键入其他值

CCD_ 1只是引用了相同的形式,不存在CCD_
我无法尝试Form duplicate = new Form() = this,因为我的表单采用了早期表单中的构造函数

有人知道怎么做吗?提前感谢

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

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

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());

以下是我要做的:让我们假设你想用一个按钮打开"克隆"。以到克隆的形式:

    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;
    }

我希望这对你有效

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

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