Tech Off Thread

9 posts

Dynamically adding the text box value to the xml file

Back to Forum: Tech Off
  • User profile image
    Varunkrishna

    Hi all

    I am creating few textboxes and labels dynamically by using this code

    using System.Drawing;
    
    private int minValue = 0, constantValue = 1;
    ....
    private void dynamicGenerateFields ()
    {
    
    TextBox country= new TextBox();
                country.Name = "Country Name";
                country.Location = new Point(20, 18 + (20 * constantValue ));
                country.Size = new System.Drawing.Size(200, 25);
                tabControl1.Controls.Add(country);
    //similarly  generating  other controls like  country code,State, Pincode etc.
    
    minValue++;
    }
    
    private void dynamicGeneratedFieldsSave ()
    {
    foreach (TextBox cName in tabControl1.Controls.OfType<TextBox>())
                {
                   
                    String countryName = cName.Text;
                    MessageBox.Show("The Country details is: " + countryName );   
    /*how do I save all the text boxes values into the xml ? */            
                }
    }
    

    Guys I would like to save all the values entered at the text box in to the xml file, and could  someone please suggest me a way for doing this ?

    Thanks

    Varun Krishna. P

  • User profile image
    Dr Herbie

    Is there a reason you're not using a data structure and binding the contents of the data structure to the controls? 

    For example, I would create a class called Address, with properties for all the details like CountryName, CountryCode, etc.  On the form I would create an instance of this structure and bind it's content to the relevant controls.  Then when it needs to save to XML you can simply serialize the data structure.

    For more complex scenarios you end up with a full Controller/ViewModel containing all the code and the logic, while the form simply uses data binding to display the data to the user.

     

     

  • User profile image
    Varunkrishna

    @Dr Herbie:

    Thanks for the reply, Actually I am creating a smart device application using c#. Sorry I didn't know about that data structure.  But it dynamically generates controls during the run time/when the user clicks add new button. Now I have a problem here please take a look at the line number 20 ( the foreach) it fetches all the textboxes' values . But I would like to get a specific text box value, is that possible to do that ?

     

    Thanks

    Varun Krishna. P

  • User profile image
    JohnAskew

    @Varunkrishna:

     Use the ID you give to the controls...

    tabControl1.Controls.OfType.Where(c => c.ID == "myTextBoxID").FirstOrDefault();

  • User profile image
    Varunkrishna

    @JohnAskew:

    Thanks for your reply. But When I use ID for my text box I am getting the following error

    'System.Windows.Forms.TextBox' does not contain a definition for ID and no extension method 'ID' accepting a first argument of type 'Sytstem.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference ?)

     

    Thanks,

    Varun Krishna. P

  • User profile image
    figuerres

    , Varunkrishna wrote

    @JohnAskew:

    Thanks for your reply. But When I use ID for my text box I am getting the following error

    'System.Windows.Forms.TextBox' does not contain a definition for ID and no extension method 'ID' accepting a first argument of type 'Sytstem.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference ?)

     

    Thanks,

    Varun Krishna. P

    John was thinking of a web page, html has ID not windows forms... WinForms has .Name

  • User profile image
    figuerres

    , Varunkrishna wrote

    @Dr Herbie:

    Thanks for the reply, Actually I am creating a smart device application using c#. Sorry I didn't know about that data structure.  But it dynamically generates controls during the run time/when the user clicks add new button. Now I have a problem here please take a look at the line number 20 ( the foreach) it fetches all the textboxes' values . But I would like to get a specific text box value, is that possible to do that ?

     

    Thanks

    Varun Krishna. P

     

    I think first you may want to study this http://msdn.microsoft.com/en-us/library/bb387061.aspx

    Linq to XML -   you can use linq with xml to do some things...

    also take a look at this:

    http://stackoverflow.com/questions/4562320/how-to-do-databinding-between-xml-file-using-c-sharp-in-winform

     

     

  • User profile image
    Varunkrishna

    @figuerres:

    Do you mean with that the following syntax I could not achieve what I am thinking to do ? or it is not the best way to do ? and thanks for pointing out that Winform contains .Name, now I could use the following code to retrieve the value of the text box named "myTextBoxID".

    tabControl1.Controls.OfType.Where(c => c.Name == "myTextBoxID");

    Thanks

    Varun Krishna. P

  • User profile image
    JohnAskew

    @figuerres:You are correct, thanks.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.