Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I create texboxes programmatically on Pocket PC?

Posted on 2014-11-04
4
Medium Priority
?
89 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
  • 3
4 Comments
 
LVL 16

Assisted Solution

by:hjgode
hjgode earned 750 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview

971 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