Save Combobox items to file (not .txt)

I have a program which has 46 diffrent combo boxes. The items in these boxes change according to what the user selects (ie Box35 items changes according to what item is selected in box 30). What I would like is for the user to be able to set all the combo box items the way they want, click save, and the program save them to a file (pref not a text file, XML maybe ). I can sort of conceptualize the saving of each "item" of every combo box and writing it to a text file, but I would much rather save it to a custom file or something else (ie a file with my programs unique file extension). I would also eventualy need to be able to populate the combo boxes from this same file.

I know it is a lot to ask for and I thank you in advance for any help you can offer. Thanks!


Answer this question

Save Combobox items to file (not .txt)

  • KyleT

    u want to save each entry from the comboboxes or all entries from each combobox (all entries meaning also non selected items)

    To make your unique file format, encrypt the combo values and place them in a file ;)


  • mduffin

    There are so many ways to tackle this problem.

    To keeps things simple, I suggest you create an in-memory DataTable and populate the values of each combo box as rows in the DataTable and then save the data using the WriteXml method on the DataTable, which will save the information to a file using Xml. You can then of course re-load the data using ReadXml method of the DataTable.

    You could abstract it all like:

    public class UserData
    {

    private DataTable _dt;

    public UserData()
    {
    _dt = new DataTable();
    DataColumn key = new DataColumn("Key",typeof(string));
    DataColumn value = new DataColumn("Value",typeof(string));
    _dt.Columns.Add(key);
    _dt.Columns.Add(value);
    }

    public void Add(string key, string value)
    {
    DataRow dr = _dt.NewRow();
    ...
    _dt.AddRow(dr);
    }

    public string Get(string key)
    {
    DataRows[] rows = table.Select(...);
    ....
    return rows[0]["Key"].ToString();
    }

    public void Save()
    {
    _dt.SaveXml(...);
    }

    public void Load()
    {
    _dt.LoadXml(...);
    }

    }

    You get the idea.

    You could also create a custom class and deal with XmlSerializer, but you get reading and saving as Xml free in the DataTable. You could do all this with a typed DataSet as well, which has some really cool benefits.

    Hope this helps,

    Dave



  • Pradeep K

    GoDaddy wrote:
    why not use an Access file. It would be easier for you and the user to populate the tables. and easier for you to bind those comboBox to it using DataSet. just an idea

    And if you don't want the user to play in that file. well you could create an Admin Interface that let's the administrator play with the data inside that mdb file. And presenting the data in a windows form with datagridview and BindingNavigator. It's really easy to do with .NET 2.0 nowadays.

    Its easy to bypass the admin protection of access databases. but might be good idee to just bind the data into the comboboxes. When using custom data format, u are still able to do it. When reading a record (data for the combobox) put it in a dataset ... the bind the dataset to your comboboxes


  • Kenn Roland

    Well, I finally broke down and wrote the code for the read and write routines. I thought about using an Access file, but I couldn't get it to work right. Anyway, I just used the stream writer/reader to write text to a file and gave that file an extension like *.lyt so that the user would not be able access the file just by double clicking it and not picking the specific program. Thanks for all your help though.

  • Dr. NO

    Thank you both for your help. I actually solved the problem sort of by saveing the information to a file in text format using stream writer and reader and giving it a off the wall extension. My only problem now (besides being lazy in this) is that I don't want to write out 46 lines of read and 46 lines of write code. I was trying to figure out a way to make a loop so that the counter would read the combobox text, save it, and then automatically advance on to the next one. I can't figure out how to make it advance to the next combobox though. Any ideas

    PS. I am only wanting to save the currently selected item in the combobox. I don't care about the rest of the items. Thanks!

  • blaflen

    why not use an Access file. It would be easier for you and the user to populate the tables. and easier for you to bind those comboBox to it using DataSet. just an idea

    And if you don't want the user to play in that file. well you could create an Admin Interface that let's the administrator play with the data inside that mdb file. And presenting the data in a windows form with datagridview and BindingNavigator. It's really easy to do with .NET 2.0 nowadays.

  • Steve001

    Not sure of the loop.

    I mostly use VB.NET instead of C#.NET

    but try this.

    foreach (Control ctl in this.controls)
    {
    if(TypeOf(ctl) == GetType(ComboBox))
    {
    ... u just found your next combobox ;)
    }
    }

    I dont know if the typeof command works on c#

    on vb it goes like this

    if typeof control is combobox then
    ...
    endif


  • Save Combobox items to file (not .txt)