Solved

How do I create texboxes programmatically on Pocket PC?

Posted on 2014-11-04
4
82 Views
Last Modified: 2016-02-16
I have an xml file with questions. I want to the system to programmatically create the fields for the items in the xml file. I know this is possible with LINQ.

Info I need to save (also to an xml file):
Personal Details:
Name
Surname
Tel No
Assessment Details:
Section 1
Question 1 - Passed
Question 2 - Failed
Section 2
Question 1 - Passed
Question 2 - Failed

Some xml files may have different amount of questions.

The xml file looks something like this:

<Assessment name="Test Assessment">
  <OperatorInfo>
    <Name />
    <Surname />
    <Tel No />
  </OperatorInfo>
  <AssessmentInfo>
    <QuestionSection name="Section 1" id="0">
      <Question name="Question 1" id="0">Is the sky blue</Question 1>
      <Answer =id"0">
        <Option 1>True</Option 1>
        <Option 2>False</Option 2>
      </Answer>
      <Question name="Question 2" id="1">ALL birds can fly</Question>
        <Answer =id"1">
        <Option 1>True</Option 1>
        <Option 2>False</Option 2>
      </Answer>
    </QuestionSection>
  </AssessmentInfo>
<Assessment>


I started off with a public class:
 public class Operator
    {
        public string operatorName { get; set; }
        public string operatorSurname { get; set; }
        public string operatorTelnum { get; set; }
    }
  public class Assessment
    {
        public string Answer1{ get; set; }
        public string Answer2{ get; set; }
        public string Answer3{ get; set; }
    }

but this will not work, as the amount of sections & questions can differ. How do I go about? Any help?
0
Comment
Question by:Benny Theunis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Assisted Solution

by:hjgode
hjgode earned 250 total points
ID: 40423420
To have an ml and a class to serialize and de-serialize with an unknown amount off repeating values use an array or list<> of another class. An example:

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;

    namespace WindowsFormsApplication1
    {
    /*
    <Assessment name="Test Assessment">
      <OperatorInfo>
        <Name />
        <Surname />
        <Tel No />
      </OperatorInfo>
      <AssessmentInfo>
        <QuestionSection name="Section 1" id="0">
          <Question name="Question 1" id="0">Is the sky blue</Question 1>
          <Answer =id"0">
            <Option 1>True</Option 1>
            <Option 2>False</Option 2>
          </Answer>
          <Question name="Question 2" id="1">ALL birds can fly</Question>
            <Answer =id"1">
            <Option 1>True</Option 1>
            <Option 2>False</Option 2>
          </Answer>
        </QuestionSection>
      </AssessmentInfo>
    <Assessment>
    */
        [XmlRoot("Assessment")]
        class assesment
        {
            public static assesment deserialize(string sXMLfile)
            {
                XmlSerializer xs = new XmlSerializer(typeof(assesment));
                //StreamReader sr = new StreamReader("./SystemHealth.xml");
                StreamReader sr = new StreamReader(sXMLfile);
                assesment s = (assesment)xs.Deserialize(sr);
                sr.Close();
                return s;
            }
            public static void serialize(assesment _assesment, string sXMLfile)
            {
                XmlSerializer xs = new XmlSerializer(typeof(assesment));
                //omit xmlns:xsi from xml output
                //Create our own namespaces for the output
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                //Add an empty namespace and empty value
                ns.Add("", "");
                //StreamWriter sw = new StreamWriter("./SystemHealth.out.xml");
                StreamWriter sw = new StreamWriter(sXMLfile);
                xs.Serialize(sw, _assesment, ns);
            }

            [XmlAttribute("name")]
            string name { get; set; }

            [XmlElement("OperatorInfo")]
            OperatorInfo _operatorInfo;

            //inner class
            class OperatorInfo
            {
                [XmlAttribute("Name")]
                string Name { get; set; }
                [XmlAttribute("Surame")]
                string Surname { get; set; }
                [XmlAttribute("Tel No")]
                string Tel_No { get; set; }
            }

            [XmlElement("QuestionSection")]
            QuestionSection _questionScetion;

            //inner class
            class QuestionSection
            {
                [XmlAttribute("name")]
                string name { get; set; }
                [XmlAttribute("id")]
                string id { get; set; }
               
                [XmlText(typeof(string))]
                public string text { get; set; }

                [XmlElement("question")]
                question[] _question;
            }

            class question
            {
                [XmlAttribute("name")]
                string name { get; set; }
                [XmlAttribute("id")]
                string id { get; set; }

                [XmlElement("Answer")]
                Answer[] answers; //you can have multiple anwsers

                //      <Question name="Question 1" id="0">Is the sky blue</Question 1>
                //<Answer =id"0">
                //<Option 1>True</Option 1>

                class Answer
                {
                    [XmlAttribute("id")]
                    string id { get; set; }

                    [XmlElement("option")] // "Option 1" / "Option 2" will not fit this!,
                    //better go with <Option id="0" name="abc">... and <Option id="1" name="xyz">...
                    option[] _option;
                }

                class option
                {
                    Boolean _bool { get; set; } //this will only allow boolean options
                }
            }
           
        }
    }


I hope this give you an idea how to code your class.
0
 

Author Comment

by:Benny Theunis
ID: 40460383
Thank you for your inputs.

After brushing up on how to create classes and using linq, I now have an idea.

hjgode, I'm currently using the linq method. I have managed to get the Questions as buttons on my form dynamically. The only problem now is to go one level deeper... displaying the elements in my question when I click on the Question button. I have my questions buttons on panel1, and now want to display those question elements on panel2 when I click on the Question button.

I hope I make sense :)
0
 

Accepted Solution

by:
Benny Theunis earned 0 total points
ID: 40460644
I managed.

For the NOOBS like me, this is what I did:
            int top = 5;
            int left = 20;

            var secList = (from sL in doc.Descendants("AssessmentSections").Descendants("AssessmentSection")
                           select new
                           {
                               itemCollectionID = Convert.ToInt32(sL.Attribute("sectionGroupID").Value),
                               itemCollectionName = sL.Attribute("sectionNameID").Value,
                           }).ToArray();

            foreach (var section in secList)
            {
                Button button = new Button();
                button.Left = left;
                button.Top = top;
                button.Text = section.itemCollectionName;
                button.Height = 35;
                button.Width = 200;
                this.Controls.Add(button);
                top += button.Height + 2;
                button.Click += new EventHandler(button_Click);
            }

   protected void button_Click (object sender, EventArgs e)
        {
                //your code
        }

Open in new window


Thanks guys for pointing me in the right direction :)
0
 

Author Closing Comment

by:Benny Theunis
ID: 40470207
By doing some research I have figured it out.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question