Solved

How do I create texboxes programmatically on Pocket PC?

Posted on 2014-11-04
4
71 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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
By doing some research I have figured it out.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now