Windows Form Dynamic Controls Access ....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgavathe
    New Member
    • Sep 2008
    • 5

    #1

    Windows Form Dynamic Controls Access ....

    How would i access dynamically created windows form control.


    I have Labels and TextBox generated dynamically from the database.

    I want to create an XML from both controls.


    something like

    <Sample>
    <Labes1.Textl > TextBox1.Text </Labels1.Text>
    <Labes2.Textl > TextBox2.Text </Labels2.Text>
    <Sample>

    I am stuck in building the Loop while i am accessing form controls.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you tried XML Serialization?

    -Frinny

    Comment

    • sgavathe
      New Member
      • Sep 2008
      • 5

      #3
      Thanks for pointing out the serializer.

      Nope, I haven't tried that.


      The way i was trying to build XML was with string concatenation.


      string Sample = "<Sample>";

      sample += "<Label1.Te x>"

      sample += "<Text1.Tex t>";

      sample += "</Label1.Text>";


      so it could build at the end like

      <Sample>
      <Labes1.Textl > TextBox1.Text </Labels1.Text>
      <Labes2.Textl > TextBox2.Text </Labels2.Text>
      <Sample>




      This is how i was trying to do,

      Code:
      if (ctrl.GetType() == typeof(Label))
                  {
                       
                      string LabelText1 = ctrl.Text;
                      teststring += "<" + LabelText1.ToString() + ">";
      
                      // Ignore the control unless it's a text box.
                      if (ctrl.GetType() == typeof(TextBox))
                      {
                   
      
                          string TextBoxText = ctrl.Text;
                          teststring += "<" + TextBoxText.ToString() + ">";
                   
      
                      }
      
                          teststring += "</" + LabelText1.ToString() + ">";   
      
                  }

      I am doing something wrong in the Loop.



      Thanks,
      Last edited by Curtis Rutland; Sep 26 '08, 03:47 PM. Reason: Added Code Tags - Please surround your code with [CODE] and [/CODE]

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        There is also the System.Xml namespace. It has all kinds of handy XML utilities.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by sgavathe
          Thanks for pointing out the serializer.

          Nope, I haven't tried that.


          The way i was trying to build XML was with string concatenation.


          string Sample = "<Sample>";

          sample += "<Label1.Te x>"

          sample += "<Text1.Tex t>";

          sample += "</Label1.Text>";


          so it could build at the end like

          <Sample>
          <Labes1.Textl > TextBox1.Text </Labels1.Text>
          <Labes2.Textl > TextBox2.Text </Labels2.Text>
          <Sample>




          This is how i was trying to do,

          Code:
          if (ctrl.GetType() == typeof(Label))
                      {
                           
                          string LabelText1 = ctrl.Text;
                          teststring += "<" + LabelText1.ToString() + ">";
          
                          // Ignore the control unless it's a text box.
                          if (ctrl.GetType() == typeof(TextBox))
                          {
                       
          
                              string TextBoxText = ctrl.Text;
                              teststring += "<" + TextBoxText.ToString() + ">";
                       
          
                          }
          
                              teststring += "</" + LabelText1.ToString() + ">";   
          
                      }

          I am doing something wrong in the Loop.



          Thanks,
          I don't see a loop in the above posted code....

          I definitely recommend checking out XML Serialization.. .and also consider using StringBuilders instead of adding to strings...this saves on memory, time, and CPU resources.

          Comment

          • sgavathe
            New Member
            • Sep 2008
            • 5

            #6
            Thanks for the help.

            I am still not being able to get that working?

            I am populating the form controls dynamically with label and texts, which is coming from another XML.

            Then i am asking user to add value in the textbox and i want to build the XML again with those values in

            <Label1> Text.value</Lable1>

            Any help would be appreciated.


            Thanks

            Comment

            • sgavathe
              New Member
              • Sep 2008
              • 5

              #7
              I have got it working with string builder .... Thanks for pointing that out.

              Code:
              StringBuilder MyStringBuilder = new StringBuilder("<data>"); 
                                   
                              foreach (Control childControl in this.Controls)
                              {
                                  // Find a Particular Control by its Type
                                  if (childControl.GetType().ToString() == "System.Windows.Forms.FlowLayoutPanel")
                                  {
                                      foreach (Control cc in childControl.Controls)
                                      {
              
                                          //ProcessControls(cc, xmlData);
                                          if(cc is Control || cc is Label || cc is TextBox) 
                                          {
                                          
                                          //if (cc is TextBox || cc is Label)
                                          //{
                                              if (cc is Label)
                                              {
                                                  string LabelText1 = cc.Text;
                                                  //xmlData += "<" + LabelText1.ToString() + ">";
              
                                                  MyStringBuilder.Append("<" + LabelText1.ToLower().ToString() + ">X</" + LabelText1.ToLower().ToString() + ">");
                                              }
              
                                              if (cc is TextBox)
                                              {
                                                  string TextBoxText = cc.Text;
                                                  MyStringBuilder.Replace("X", TextBoxText.ToString());
              
                                              }
                                          
                                          }
              
              
                                      }
                                      
                                  }
              
                                 
                              }
                              MyStringBuilder.Append("</data>");

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                We are glad that you have gotten it to work.

                Please use [CODE] tags when you post code. It makes it much easier to read.

                I've fixed your post.

                MODERATOR

                Comment

                Working...