Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中,将控件和变量的状态从一种形式复制到另一种形式_C#_Winforms_Reflection - Fatal编程技术网

C# 在c中,将控件和变量的状态从一种形式复制到另一种形式

C# 在c中,将控件和变量的状态从一种形式复制到另一种形式,c#,winforms,reflection,C#,Winforms,Reflection,我有一张windows窗体。如果用户未选中复选框,则稍后按“下一步”时会打开一个新表单,但如果用户选择了该复选框,则会使用相同的表单。 如果选中复选框,我希望当前表单的克隆显示为具有相同变量和控制值,以便以后可以更改值,而无需取消选中复选框,再次按“下一步”,并手动键入其他值。 表格副本=此;只是引用了相同的形式,没有newthis这样的东西。 我无法尝试Form duplicate=new Form=此操作,因为我的表单从早期表单获取构造函数 有人知道怎么做吗?提前感谢以下是我要做的: 让我们

我有一张windows窗体。如果用户未选中复选框,则稍后按“下一步”时会打开一个新表单,但如果用户选择了该复选框,则会使用相同的表单。 如果选中复选框,我希望当前表单的克隆显示为具有相同变量和控制值,以便以后可以更改值,而无需取消选中复选框,再次按“下一步”,并手动键入其他值。 表格副本=此;只是引用了相同的形式,没有newthis这样的东西。 我无法尝试Form duplicate=new Form=此操作,因为我的表单从早期表单获取构造函数 有人知道怎么做吗?提前感谢

以下是我要做的: 让我们假设您想用按钮打开克隆。 以“复制到克隆”的形式:

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

}

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

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

}

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

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

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

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

您是否有许多需要克隆的状态变量?克隆整个表单层次结构控件、子控件和事件对我来说是一个大麻烦,我建议您不要这样做。如果有5-10个控件要克隆其状态,对于这些特定的控件,最好使用这样的方法。什么方法克隆控件及其值?例如,我只希望复制.text值并选中复选框?您可以在表单中添加第二个构造函数,以便传递所需的值。我如何创建包含所有变量和所有文本框“.文本值是否有许多需要克隆的状态变量?克隆整个表单层次结构控件、子控件和事件对我来说是一个大麻烦,我建议您不要这样做。如果有5-10个控件要克隆其状态,对于这些特定的控件,最好使用这样的方法。什么方法克隆控件及其值?例如,我只希望复制.text值并选中复选框?您可以在表单中添加第二个构造函数,以便传递所需的值。我如何创建包含所有变量和所有文本框。文本值它们就像15个文本框,我已经有了用于另一个目的的构造函数。这有点复杂,但是你可以用属性TextBox1 TextBox2等编码一个对象,并给它们值。之后,你将其保存为xml,当你再次调用表单时,用show加载xml文件并为每个文本框分配值。大约有15个文本框,我已经有了其他用途的构造函数。这有点复杂,但你可以用属性TextBox1 TextBox2等对对象进行编码,并给它们赋值。之后,将其保存为xml,当您再次使用show调用表单时,加载xml文件并为每个文本框分配值。是,将控件名称和值保存在字典中,如text或bool。加载表单时,您可以检查字典是否为空。如果不是空,您可以用这些值填充控件。是的在字典中保存控件名称和值,如text或bool。加载表单时,可以检查字典是否为null。如果不是null,则可以用这些值填充控件。