public JpegQualityDialog() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); conf = AppConfig.GetInstance(); this.trackBarJpegQuality.Value = conf.Output_File_JpegQuality; this.textBoxJpegQuality.Text = conf.Output_File_JpegQuality.ToString(); }
public PrintOptionsDialog() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); conf = AppConfig.GetInstance(); this.AllowPrintCenter = this.checkboxAllowCenter.Checked = (bool)conf.Output_Print_Center; this.AllowPrintEnlarge = this.checkboxAllowEnlarge.Checked = (bool)conf.Output_Print_AllowEnlarge; this.AllowPrintRotate = this.checkboxAllowRotate.Checked = (bool)conf.Output_Print_AllowRotate; this.AllowPrintShrink = this.checkboxAllowShrink.Checked = (bool)conf.Output_Print_AllowShrink; this.checkbox_dontaskagain.Checked = false; }
/// <summary> /// loads the configuration from the config file /// </summary> /// <returns>an instance of AppConfig with all values set from the config file</returns> private static AppConfig Load() { AppConfig conf; CheckConfigFile(); Stream s = null; try { s = File.Open(ConfigPath, FileMode.Open); BinaryFormatter b = new BinaryFormatter(); conf = (AppConfig)b.Deserialize(s); s.Close(); conf.SetDefaults(); return conf; } catch (Exception ex) { Debug.WriteLine(ex.ToString()); if (s != null) { s.Close(); } AppConfig config = new AppConfig(); config.Save(); return config; } }
/// <summary> /// when new fields are added to this class, they are instanced /// with null by default. this method iterates over all public /// fields and uses reflection to set them to the proper default value. /// </summary> public void SetDefaults() { Type type = this.GetType(); FieldInfo[] fieldInfos = type.GetFields(); foreach (FieldInfo fi in fieldInfos) { object o = fi.GetValue(this); int i; if (o == null || (int.TryParse(o.ToString(), out i) && i == 0)) { // found field with value null. setting to default. AppConfig tmpConf = new AppConfig(); Type tmpType = tmpConf.GetType(); FieldInfo defaultField = tmpType.GetField(fi.Name); fi.SetValue(this, defaultField.GetValue(tmpConf)); } } }
/// <summary> /// get an instance of AppConfig /// </summary> /// <returns></returns> public static AppConfig GetInstance() { if (instance == null) { instance = Load(); } return instance; }