Thread: textBox, comboBox

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    textBox, comboBox

    I wonder something.
    If you for example use a textBox and a comboBox in a Form and want to save something like this:

    Code:
    comboBox1->Items->Add(textBox1->Text);
    This will work if you write something in the textBox1 and press a button this text will appear in the comboBox but when I close the application and open it again, the text will have dissapear from the comboBox.

    What is needed here, are we talking about databases to save such things ?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes.

    How else would you expect the data to save?

    You don't need to use a database, you can use text files, xml, or whatever you can think of to load the data from an external source.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes, that was what I thought. I had text files in mind to use.

  4. #4
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Forget text files! Use XML files. This is after all 2008!

    You can do this easy by :

    //this code isn't tested but u'll get the idéa
    Code:
    try{
    XmlDocument MyXML = new XmlDocument();
    XmlElement MyROOT;
    XmlElement MyElement;
    
    //creates a root for you
    ROOT = MyXML.CreateElement("mydata");
    MyXML.AppendChild(ROOT);
    
    //adds your data inside the root
    foreach(string sItem in comboBox1.Items){
    MyElement = MyXML.CreateElement("DATA");//creates a node "DATA"
    MyElement.InnerText = sItem;//puts your item text|data inside the "DATA" node
    ROOT.InsertAfter(MyElement, ROOT.LastChild);
    }
    
    //save your XML
    MyXML.Save("c:\somefile.xml");
    }
    catch(Exception e){
    Console.Write( e.ToString() );
    }
    Once saving XML, open your file with notepad or IE, it should look like this inside :

    <mydata>
    <DATA>text of item in combo box</DATA>
    <DATA>text of item in combo box</DATA>
    <DATA>text of item in combo box</DATA>
    </mydata>


  5. #5
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    You can get your data back with MyXML.Load("c:\somefile.xml");

    and do a loop, something like

    Code:
    foreach(XmlNode XMLNODE in MyXML.SelectSingleNode("mydata").ChildNodes){
    Console.Write( XMLNODE.InnerText );//This will get your data back for you from the XML
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2009, 04:40 AM
  2. Replies: 5
    Last Post: 03-02-2009, 08:33 AM
  3. Problem with a multiline textbox
    By Zeokat in forum C# Programming
    Replies: 4
    Last Post: 10-24-2008, 01:14 PM
  4. Trouble Adding to ComboBox (*not* an LB_ issue..)
    By roblarky in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2007, 11:27 AM
  5. inserting text in a textbox
    By Rune Hunter in forum C# Programming
    Replies: 1
    Last Post: 01-07-2006, 05:32 PM