-
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 ?
-
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.
-
Yes, that was what I thought. I had text files in mind to use.
-
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>
:)
-
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
}