在C#windows应用程序

时间:2016-06-29 21:18:22

标签: c# winforms

我的GUI如下所示。 GUI Image

单击生成按钮我将从GUI获取所有输入并为其创建和xml文件。 我的代码生成Xml文件

namespace ActionSample
{
    [Serializable()]
    public class TestCase
    {
        [XmlElement(ElementName = "TestCaseID")]
        public string TestCaseID { get; set; }
        [XmlElement(ElementName = "TestCaseDescription")]
        public string TestCaseDescription { get; set; }
        // public Action Action;
        [XmlElement(ElementName = "TestSetup")]
        public TestSetup TestSetup { get; set; }
        [XmlElement(ElementName = "TestExecution")]
        public TestExecution TestExecution { get; set; }
    }

    public class TestSetup
    {
        [XmlElement(ElementName = "Action")]
        public Action Action { get; set; }
    }

    [XmlRoot(ElementName = "TestStep")]
    public class TestStep
    {
        [XmlElement(ElementName = "IsExecutable")]
        public bool IsExecutable { get; set; }
        [XmlElement(ElementName = "RequestEndpoint")]
        public string RequestEndpoint { get; set; }
        [XmlElement(ElementName = "Action")]
        public Action Action { get; set; }
    }

    [XmlRoot(ElementName = "TestExecution")]
    public class TestExecution
    {
        [XmlElement(ElementName = "TestStep")]
        public TestStep TestStep { get; set; }
    }

    public class Action
    {
        public string ActionCommand;
        public string TimeOut;
        public string BamSymbol;
        public string Side;
        public string PrimeBroker;
        public string Size;
        public string PMCode;
        public string Strategy;
        public string SubStrategy;
        public string ActionLogEndPoint;
        public string ParameterName;
        public string ParameterValue;
        public bool IsActionResultLogged;
        [XmlElement(ElementName = "ValidationStep")]
        public ValidationStep ValidationStep { get; set; }
    }

    [XmlRoot(ElementName = "ValidationStep")]
    public class ValidationStep
    {
        [XmlElement(ElementName = "IsValidated")]
        public bool IsValidated { get; set; }
        [XmlElement(ElementName = "ValidationFormat")]
        public string ValidationFormat { get; set; }
        [XmlElement(ElementName = "ResponseEndpoint")]
        public string ResponseEndpoint { get; set; }
        [XmlElement(ElementName = "ResponseParameterName")]
        public string ResponseParameterName { get; set; }
        [XmlElement(ElementName = "ResponseParameterValue")]
        public string ResponseParameterValue { get; set; }
        [XmlElement(ElementName = "ExpectedValue")]
        public string ExpectedValue { get; set; }
        [XmlElement(ElementName = "IsValidationResultLogged")]
        public bool IsValidationResultLogged { get; set; }
        [XmlElement(ElementName = "ValidationLogEndpoint")]
        public string ValidationLogEndpoint { get; set; }
    }
}

Form1.cs的

public void button1_Click(object sender, EventArgs e)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(TestCase));            
        StringWriter sw = new StringWriter();
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add(string.Empty, string.Empty);
        XmlTextWriter writer = new XmlTextWriter("Action.xml", System.Text.Encoding.UTF8);
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 2;

        TestCase tc = new TestCase();
        Action actionObj = new Action();
        TestSetup testsetupObj = new TestSetup();
        ValidationStep vsObj = new ValidationStep();
        TestStep teststepObj = new TestStep();
        TestExecution teObj = new TestExecution();

        actionObj.ActionCommand = comboBox1.Text.ToString();
        actionObj.TimeOut = textBox1.Text.ToString();
        actionObj.BamSymbol = textBox2.Text.ToString();
        actionObj.Side = comboBox4.Text.ToString();
        actionObj.PrimeBroker = comboBox5.Text.ToString();
        actionObj.Size = textBox5.Text.ToString();
        actionObj.PMCode = comboBox2.Text.ToString();
        actionObj.Strategy = textBox7.Text.ToString();
        actionObj.SubStrategy = comboBox3.Text.ToString();
        actionObj.ActionLogEndPoint = textBox9.Text.ToString();

        tc.TestCaseID = textBox4.Text.ToString();
        tc.TestCaseDescription = textBox6.Text.ToString();
        //tc.Action = actionObj;

        teststepObj.IsExecutable = Convert.ToBoolean(textBox17.Text.ToString());
        teststepObj.RequestEndpoint = textBox18.Text.ToString();

        vsObj.IsValidated = Convert.ToBoolean(textBox8.Text.ToString());
        vsObj.ValidationFormat = textBox10.Text.ToString();
        vsObj.ResponseEndpoint = textBox11.Text.ToString();
        vsObj.ResponseParameterName = textBox12.Text.ToString();
        vsObj.ResponseParameterValue = textBox13.Text.ToString();
        vsObj.ExpectedValue = textBox14.Text.ToString();
        vsObj.IsValidationResultLogged = Convert.ToBoolean(textBox15.Text.ToString());
        vsObj.ValidationLogEndpoint = textBox16.Text.ToString();
        actionObj.ValidationStep = vsObj;
        testsetupObj.Action = actionObj;
        teststepObj.Action = actionObj;
        teObj.TestStep = teststepObj;
        tc.TestSetup = testsetupObj;
        tc.TestExecution = teObj;
        serializer.Serialize(writer, tc);
        writer.Close();          
        MessageBox.Show("XML File created ! ");
    }

现在我有一个要求。 案例一 1.单击“克隆”按钮,应动态生成在Testsetup下分组的所有控件。 为此,我试图动态生成两个控件,并在下面粘贴代码。

int inputNumber = 1;

        //Initialize list of input text boxes
        inputTextBoxes = new List<TextBox>();
        inputComboBoxes = new List<ComboBox>();
        string dd;

        //Generate labels and text boxes
        for (int i = 1; i <= inputNumber; i++)
        {
            #region
            //Create a new label and text box
            Label labelInput = new Label();

            ComboBox comboBoxNewInput = new ComboBox();

            //Initialize label's property
            labelInput.Text = "Action";
            labelInput.Location = new Point(30,  groupBox2.Bottom+ (i * 30));
            labelInput.AutoSize = true;

            //Initialize comboBoxes Property
            comboBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);

            //Add the newly created text box to the list of input text boxes
            inputComboBoxes.Add(comboBoxNewInput);                   

            //Add the labels and text box to the form
            this.Controls.Add(labelInput);

            this.Controls.Add(comboBoxNewInput);

            Label labelInput2 = new Label();
            TextBox textBoxNewInput = new TextBox();

            labelInput2.Text = "TimeOut";
            labelInput2.Location = new Point(comboBoxNewInput.Width + 120, groupBox2.Bottom + (i * 30));
            labelInput2.AutoSize = true;

            //Initialize textBoxes Property
            textBoxNewInput.Location = new Point(labelInput2.Width + 200, labelInput2.Top - 3);

            ////Add the newly created text box to the list of input text boxes
            inputTextBoxes.Add(textBoxNewInput);


            //Add the labels and text box to the form
            this.Controls.Add(labelInput2);
            this.Controls.Add(textBoxNewInput);

我采取了正确的方法,因为我发现这种方法有点单调乏味。有没有更好的方法来完成我的任务?

案例二 2.正如您从TestCase类上面的代码中看到的,我创建了类并从类的对象生成xml。 如果控制是动态的,那么我应该如何修改上面的XmlSerializer代码呢?

我想在寡妇应用程序中搜索很多动态生成的控件。没有找到好运的好运

请帮我提出建议。任何帮助,将不胜感激。在此先感谢。

0 个答案:

没有答案