Question

I am developing a Windows mobile 6.0 application using Visual Studio 2008 (.NET Framework 3.0) with C#, I want to save the selected value from a comboBox in Form2 for example and retrieve it later after I close and re-open the application in Form1.

Was it helpful?

Solution

There are a bunch of way that this can be made possible. I prefer saving this value (along wiht any other value you would like to save) inside of a .xml file. When you proess the "OK" button on your form, it will save the value to the value. Once you open the form, it will open and read the .xml file assinging the values as need be. Check out this link on how to read and write to .xml files.

OTHER TIPS

First, there is NO Compact Framework version 3.0 for Windows Mobile. Are you talling about Windows Phone 7 or 8?

For Windows Mobile (Compact Framework 2.0 or 3.5): If you want to store / retrieve only one value, you can simply use the registry to save and restore the value.

using System;
using System.Text;
using Microsoft.Win32;

namespace StoreString
{
    class RegStoreClass:IDisposable
    {
        RegistryKey m_regKey = null;
        String m_AppName="";
        String m_Value = "";
        public String sValue{
            get{
                readReg();
                return m_Value;
            }
            set{
                m_Value=value;
                this.writeReg();
            }
        }
        String AppName
        {
            get
            {
                if (m_AppName == "")
                {
                    //assign AppName
                    string aname;
                    //get the executable name
                    m_AppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                    //remove extra infos

                }
                return m_AppName;
            }
        }
        public RegStoreClass()
        {

            try
            {
                //open or create sub key for read/write
                m_regKey = Registry.LocalMachine.CreateSubKey(AppName);
            }
            catch (Exception)
            {
            }
            //try to read value from sub key
            if (!readReg())
            {
                //something failed, write actual value to reg
                writeReg();
            }
        }
        public void Dispose(){
            m_regKey.Flush();
            m_regKey.Close();
        }
        bool readReg(){
            bool bRet=false;
            try 
            {
                string s = (string)m_regKey.GetValue("Value", "n/a");
                m_Value = s;
                bRet=true;
            }
            catch (Exception)
            {
            }
            return bRet;
        }
        bool writeReg(){
            bool bRet = false;
            try
            {
                m_regKey.SetValue("Value", m_Value, RegistryValueKind.String);
                m_regKey.Flush();
                bRet = true;
            }
            catch (Exception)
            {

            }
            return bRet;
        }
    }
}

Use the above class (for example as regClass) in your Form2 code. Then, when you need to store or retrieve the stored value:

Save new value:

regClass.sValue = comboBox1.SelectedItem.ToString();

Read saved value:

string s = regClass.sValue

The above class exmines the application name itself and uses that as subkey for storing the value in the registry.

========================================== If you come across the need to store more and more values, it is better to use a class that will do that for you. The storage can be an external file or the registry. External files can be organized like ini files or have a structure as xml files.

Compact Framework implementation of app.settings: http://www.codeproject.com/Articles/6308/AppSettings-Implementation-for-Compact-Framework and http://www.codeproject.com/Articles/51848/Compact-Framework-Configuration-XML-File-Read-Writ

Ini or xml file location as program executable: Find INI File From application path and Read INI File in Compact framework C# Read / write ini files http://www.codeproject.com/Articles/21896/INI-Reader-Writer-Class-for-C-VB-NET-and-VBScript

The easiest way is to use xmlserializer. This way you dont need to specify how to write nor read every values. Just pass them the stream object and the object to be serialized and xmlserializer will take care of writing of the values. Same with getting back the values, use deserialize and get the object and cast it to the target type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top