Click here to Skip to main content
15,887,485 members
Articles / Desktop Programming / Windows Forms

Application State Data using XML

Rate me:
Please Sign up or sign in to vote.
2.67/5 (3 votes)
26 Sep 20062 min read 22.7K   254   22  
A utility to capture/restore state data to a container via XML.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

/*
Author:      Philip McGahan 
Email:       pmcgahan@hteinc.com, McGahanFL@Earthlink.net
The author maintains no rights or restriction on this code, its reuse or modification.
Date:        09/15/2006
*/


namespace PFM_Wrap_Ctrl_Text_Property
{
    /// <summary>
    /// N:PFM_Wrap_Ctrl_Text_Property: Used as a standard way to get/set values of a control.
    /// </summary> 
    /// 

       /// <summary>
       /// Documentation tags based on http://www.codeproject.com/useritems/C__Documentation_Proess.asp
       /// by Jaiprakash M Bankolli
       /// N denotes the Namespace 
       ///T denotes Type,and includes class, Interface, struct, enum or delegates 
       ///F Denotes the fields of class 
       ///P Denotes the Property indexer or indexed property 
       ///M Denotes the Methods special methods constructor and operator(overloaded) 
       ///E Denotes Events 
       ///! Denotes error string that the C# was unable to resolve some references
       /// </summary>


    public class SetGetCtrlText
    {
        /// <summary>
        /// T:SetGetCtrlText: Used as a standard way to get/set values to text and text-like controls.
        /// Supports: Radio, checkbox and controls that use '.TEXT' property.
        /// ToDo:Enhance this class to support additional control types.
        /// </summary> 
        /// 

        public string getText(Control ctrl)
        {
            /// <summary>
            /// M:getText: Returns a value from a control.
            /// Would need modification to support non Text and Text-like controls
            /// </summary> 
            /// 

           string sValueReturn = null;
           Type ctrlType = ctrl.GetType();
           if (ctrlType.Name == "CheckBox")
           {
              CheckBox tmp = (CheckBox)ctrl;
              sValueReturn = "0";
              if (tmp.Checked == true)
                  sValueReturn = "1";
            }
            else if (ctrlType.Name == "RadioButton")
            {
               RadioButton tmp = (RadioButton)ctrl;
               sValueReturn = "0";
               if (tmp.Checked == true)
                   sValueReturn = "1";
            }
           else if(ctrlType.Name == "ProgressBar")
           {
             //TODO-PROGRESS-BAR
             //example of how to support more controls.
             // 1 of 2.  Save/Restore progress bar
             // I do not want to set default data on a progress bar but if you did you would need code
             // like this.
             //  ProgressBar tmp = (ProgressBar)ctrl;
             //  sValueReturn = tmp.Value.ToString();
           }
           else if (ctrlType.Name == "CheckedListBox")
           {//this shows the limitation of this process.  We can not save a controls state unless
            //it can be converted to a single string.
               StringBuilder sValues = new StringBuilder();
               CheckedListBox tmp = (CheckedListBox)ctrl;
               for (int iIndex = 0; iIndex < tmp.Items.Count; iIndex++)
               {
                   if (tmp.GetItemChecked(iIndex) == false)
                       sValues.Append("0");
                   else
                       sValues.Append("1");
               }
               sValueReturn = sValues.ToString();
           }
           else
           {//for control types that support the Text attribute
                sValueReturn = ctrl.Text;
           }
            return sValueReturn;
        }//getText

        public void setText(Control ctrl, string strValue)
        {
            /// <summary>
            /// M:setText: Sets a value to a control.
            /// Would need modification to support non Text and Text-like controls
            /// </summary> 
            /// 

            Type ctrlType = ctrl.GetType();
            if (ctrlType.Name == "CheckBox")
            {
                bool bState = false;
                if (strValue == "1")
                    bState = true;
                CheckBox tmp = (CheckBox)ctrl;
                tmp.Checked = bState;
            }
            else if (ctrlType.Name == "RadioButton")
            {
                bool bState = false;
                if (strValue == "1")
                    bState = true;
                RadioButton tmp = (RadioButton)ctrl;
                tmp.Checked = bState;
            }
           else if(ctrlType.Name == "ProgressBar")
           {
               //TODO-PROGRESS-BAR
               //example of how to support more controls.
               // 1 of 2.  Save/Restore progress bar
               // I do not want to set default data on a progress bar but if you did you would need code
               // like this.
               //ProgressBar tmp = (ProgressBar)ctrl;
               //tmp.Value = Convert.ToInt16(strValue);
           }
           else if (ctrlType.Name == "CheckedListBox")
           {//this shows the limitation of this process.  We can not save a controls state unless
               //it can be converted to a single string.
               CheckedListBox tmp = (CheckedListBox)ctrl;
               for (int iIndex = 0; iIndex < tmp.Items.Count; iIndex++)
               {
                   if(strValue.Substring(iIndex, 1) == "0")
                       tmp.SetItemCheckState(iIndex, CheckState.Unchecked);
                   else
                       tmp.SetItemCheckState(iIndex, CheckState.Checked);

               }
           }
           else
           {
               ctrl.Text = strValue;
           }
        }//setText
    }//SetGetCtrlText
}//Wrap_ControlValue

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Just a good guy.

Comments and Discussions