Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I create texboxes programmatically on Pocket PC?

Posted on 2014-11-04
4
Medium Priority
?
85 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

722 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