Solved

How do I create texboxes programmatically on Pocket PC?

Posted on 2014-11-04
4
78 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

816 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

11 Experts available now in Live!

Get 1:1 Help Now