Esempio n. 1
0
        private void SaveControlProperties(Control control, ref StringBuilder appSettings)
        {
            // Write the control's properties to our config file

            if ((control is Label) && !StoreLabelInfo)
            {
                return;
            }
            if ((control is Button) && !StoreButtonInfo)
            {
                return;
            }
            if (ExcludedControls.Contains(control))
            {
                return;
            }
            if (control.Tag != null)
            {
                if (control.Tag.Equals("NoConfigSave"))
                {
                    return;  // This control isn't being stored
                }
            }

            if (control.Tag != null)
            {
                if (!control.Tag.Equals("NoTextSave"))
                {
                    appSettings.AppendLine(control.Name + ":Text:" + Encode(control.Text));
                }
            }
            else
            {
                appSettings.AppendLine(control.Name + ":Text:" + Encode(control.Text));
            }

            PropertyInfo prop = control.GetType().GetProperty("SelectedIndex", BindingFlags.Public | BindingFlags.Instance);

            if (prop != null && prop.CanWrite)
            {
                appSettings.AppendLine(control.Name + ":SelectedIndex:" + prop.GetValue(control));
            }

            prop = control.GetType().GetProperty("Checked", BindingFlags.Public | BindingFlags.Instance);
            if (prop != null && prop.CanWrite)
            {
                appSettings.AppendLine(control.Name + ":Checked:" + prop.GetValue(control));
            }
        }
 private bool ControlIsExcluded(Control control)
 {
     if (control is TabPage)
     {
         return(true);
     }
     if ((control is Label) && !StoreLabelInfo)
     {
         return(true);
     }
     if ((control is Button) && !StoreButtonInfo)
     {
         return(true);
     }
     if ((control is GroupBox) && !StoreGroupboxInfo)
     {
         return(true);
     }
     if (ExcludedControls.Contains(control))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 3
0
        private void RestoreFormValues()
        {
            // Read our saved control values from the file, and restore

            String appSettings = String.Empty;

            if (_formsConfig.ContainsKey(_form.Name))
            {
                appSettings = Decode(_formsConfig[_form.Name]);
            }
            if (String.IsNullOrEmpty(appSettings))
            {
                return;
            }

            if (!appSettings.StartsWith("FormConfig:"))
            {
                return;
            }


            using (StringReader reader = new StringReader(appSettings))
            {
                string sLine = "";
                while (sLine != null)
                {
                    sLine = reader.ReadLine();

                    if (!String.IsNullOrEmpty(sLine))
                    {
                        string[] controlSetting = sLine.Split(':');
                        if (controlSetting.Length > 3)
                        {
                            for (int i = 3; i < controlSetting.Length; i++)
                            {
                                controlSetting[2] = controlSetting[2] + ":" + controlSetting[i];
                            }
                        }

                        Control control = null;
                        try
                        {
                            if (!String.IsNullOrEmpty(controlSetting[0]))
                            {
                                Control[] matchingControls = _form.Controls.Find(controlSetting[0].Trim(), true);
                                if (matchingControls.Length > 1)
                                {
                                    foreach (Control matchingControl in matchingControls)
                                    {
                                        if (matchingControl.Name == controlSetting[0].Trim())
                                        {
                                            control = matchingControl;
                                            break;
                                        }
                                    }
                                }
                                else if (matchingControls.Length == 1)
                                {
                                    control = matchingControls[0];
                                }
                            }
                        }
                        catch { }
                        if (control != null)
                        {
                            bool bRestore = true;
                            if (ExcludedControls.Contains(control))
                            {
                                bRestore = false;
                            }
                            else
                            {
                                if (control.Tag != null)
                                {
                                    bRestore = !control.Tag.Equals("NoConfigSave");
                                }
                            }
                            if (bRestore)
                            {
                                PropertyInfo prop = control.GetType().GetProperty(controlSetting[1].Trim(), BindingFlags.Public | BindingFlags.Instance);
                                if (controlSetting[1].Trim().Equals("Text"))
                                {
                                    controlSetting[2] = Decode(controlSetting[2]);
                                }
                                if (prop != null && prop.CanWrite)
                                {
                                    try
                                    {
                                        switch (prop.PropertyType.Name.ToString())
                                        {
                                        case "Int32":
                                            prop.SetValue(control, Convert.ToInt32(controlSetting[2]));
                                            break;

                                        case "Boolean":
                                            prop.SetValue(control, Convert.ToBoolean(controlSetting[2]));
                                            break;

                                        default:
                                            prop.SetValue(control, controlSetting[2]);
                                            break;
                                        }
                                    }
                                    catch { }
                                }
                            }
                        }
                    }
                }
            }
        }