如何使这些简单?

时间:2017-03-05 01:43:44

标签: c#

我尝试将一个表单对象复制到另一个表单对象。以下是每个textBox和其他cotrols的重复设置相等。我想知道是否有任何简单的方法来重复逐个控制副本。

class CopyLadderAndPlatformWin
        {
            public CopyLadderAndPlatformWin(LadderAndPlatformWin tgLadderAndPlatformWin, LadderAndPlatformWin srLadderAndPlatformWin)
            {
                tgLadderAndPlatformWin.chTopPlatform.Checked = srLadderAndPlatformWin.chTopPlatform.Checked;
                tgLadderAndPlatformWin.txtIdentifier.Text = srLadderAndPlatformWin.txtIdentifier.Text;
                tgLadderAndPlatformWin.txtStartAngle.Text = srLadderAndPlatformWin.txtStartAngle.Text;
                tgLadderAndPlatformWin.txtSizeAngle.Text = srLadderAndPlatformWin.txtSizeAngle.Text;
                tgLadderAndPlatformWin.txtLadderAngle.Text = srLadderAndPlatformWin.txtLadderAngle.Text;
                tgLadderAndPlatformWin.txtPltWidth.Text = srLadderAndPlatformWin.txtPltWidth.Text;
                tgLadderAndPlatformWin.txtPltDatum.Text = srLadderAndPlatformWin.txtPltDatum.Text;
                tgLadderAndPlatformWin.txtShellClearence.Text = srLadderAndPlatformWin.txtShellClearence.Text;
                tgLadderAndPlatformWin.txtGratingWeight.Text = srLadderAndPlatformWin.txtGratingWeight.Text;
                tgLadderAndPlatformWin.txtLdToDtm.Text = srLadderAndPlatformWin.txtLdToDtm.Text;
                tgLadderAndPlatformWin.txtHndHgt.Text = srLadderAndPlatformWin.txtHndHgt.Text;
                tgLadderAndPlatformWin.txtRailWgt.Text = srLadderAndPlatformWin.txtRailWgt.Text;
                tgLadderAndPlatformWin.txtWnFcCof.Text = srLadderAndPlatformWin.txtWnFcCof.Text;
                tgLadderAndPlatformWin.txtPltDpt.Text = srLadderAndPlatformWin.txtPltDpt.Text;
                tgLadderAndPlatformWin.txtRailHgt.Text = srLadderAndPlatformWin.txtRailHgt.Text;
                tgLadderAndPlatformWin.txtHeadStart.Text = srLadderAndPlatformWin.txtHeadStart.Text;
                tgLadderAndPlatformWin.txtHeadEnd.Text = srLadderAndPlatformWin.txtHeadEnd.Text;
                tgLadderAndPlatformWin.txtPltLength.Text = srLadderAndPlatformWin.txtPltLength.Text;
                tgLadderAndPlatformWin.txtStrgtWidth.Text = srLadderAndPlatformWin.txtStrgtWidth.Text;
                tgLadderAndPlatformWin.txtPltAngle.Text = srLadderAndPlatformWin.txtPltAngle.Text;
                tgLadderAndPlatformWin.txtPltOffset.Text = srLadderAndPlatformWin.txtPltOffset.Text;
                tgLadderAndPlatformWin.txtTopDatum.Text = srLadderAndPlatformWin.txtTopDatum.Text;

                tgLadderAndPlatformWin.rdApplyWind.Checked = srLadderAndPlatformWin.rdApplyWind.Checked;
                tgLadderAndPlatformWin.rdNoWind.Checked = srLadderAndPlatformWin.rdNoWind.Checked;
                tgLadderAndPlatformWin.chEmpty.Checked = srLadderAndPlatformWin.chEmpty.Checked;
                tgLadderAndPlatformWin.chTest.Checked = srLadderAndPlatformWin.chTest.Checked;
                tgLadderAndPlatformWin.chLiftWeight.Checked = srLadderAndPlatformWin.chLiftWeight.Checked;
            }
        }

3 个答案:

答案 0 :(得分:0)

在一个循环中迭代源表单上的所有控件。

在这个循环中编写另一个循环来迭代目标表单上的控件。

按名称比较控件。

当名称根据控件类型匹配从源到目标的复制值时。

以下是展示这个想法的代码:

Invite::where('code', $value)

答案 1 :(得分:0)

在你的评论中你说:

  

我需要将保存的控件值传输到另一个要显示的表单(重新播放)。

执行此操作的正确方法是使用一个类来表示您正在操作或以第一种形式显示的内容。例如,假设您正在操纵或显示名为Platform的内容(通过查看您的代码)。

您需要为此Platform创建一个类:

public class Platform
{
    public double LadderAngle { get; set; }
    public int LadderWidth { get; set; }
    // Other properties
}

然后在第一个类中创建一个实例并使用该实例,如下所示:

var platform = new Platform();

// This can fail so use TryParse but I will just use Parse for brevity
platform.LadderAngle = int.Parse(tgLadderAndPlatformWin.txtLadderAngle.Text);

然后,不要将值从控件复制到其他控件,只需将该对象引用传递给第二个表单:

var form2 = new Form2();
form2.Platform = platform;

要实现这一点,您需要在第二种形式中创建一个属性,如下所示:

public Platform Platform { get; set; }

答案 2 :(得分:0)

如果您只想复制TextChecked属性, 您可以使用反射来迭代LadderAndPlatformWin之外的所有属性,并检查它们是否具有带有选中或文本值的属性,然后复制值。

代码将是这样的:

 [TestMethod]
    public void DoCopy()
    {
        var source = new SomeObject();

        var Destination = new SomeObject();
        Destination.A = new AnotherObject();
        Destination.C = new LastOne();
        Destination.B = new AnotherObject();

        var type = source.GetType();
        BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
        PropertyInfo[] properties = type.GetProperties(flags);

        foreach (PropertyInfo property in properties)
        {
            var insideSource = property.GetValue(source);
            var insideType = insideSource.GetType();

            var insideDestination = property.GetValue(Destination);


            insideType.GetProperties(flags).Where(s => s.Name == "Text" || s.Name == "Checked").ToList().ForEach(p =>
            {
                p.SetValue(insideDestination, p.GetValue(insideSource)); 
            });

        }

        Trace.WriteLine(JsonConvert.SerializeObject(Destination));
    }

    public class SomeObject
    {
        public AnotherObject A { get; set; } = new AnotherObject() {Text = "Text"};
        public AnotherObject B { get; set; } = new AnotherObject() {Text = "Text2"};

        public LastOne C { get; set; } = new LastOne() {Checked = true};
    }

    public class AnotherObject
    {
        public string Text { get; set; }
    }

    public class LastOne
    {
        public bool Checked { get; set; }
    }