Solved

multiple types of user profiles/preferences

Posted on 2009-07-05
8
300 Views
Last Modified: 2013-12-17
is there a way to save multiple profiles for the user to select they're app settings from
ex: app is installed and multiple people use it on one windows log in
user A likes his colors, and forms in one place with one background
user B has something else ect.

also the users have to option to save a variety of color profiles only and use them with whatever main profile they want.

side note is there a way to import an old INI file so users don't have to reconfigure everything on when and if they choose to migrate to the new app?
0
Comment
Question by:maveric191
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 24782799
Sure!
Create a mechanism in your aplication that allows users ( of your app ) to save & load their own preferences.
In the most simple example, save user settings to xml file(s), and provide a way for your app user to load their own settings.
For your color settings, save them separate from the main settings and then allow the users to choose which color profile(s) they want to use.
In terms of the INI file question, File.Copy should do it
 
 
0
 

Author Comment

by:maveric191
ID: 24811869
fantastic!!! could you be so kind as to point me in the right direction plz.  I've never worked with XML that i'm aware of at least
0
 

Author Comment

by:maveric191
ID: 24943951
still at a loss as to how to do this.  help plz
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Expert Comment

by:topdog770
ID: 24948033
Sorry mav, I'm not sure how i missed your first comment.  When i get to work tomorrow, I'll add some code snippets here.
0
 

Author Comment

by:maveric191
ID: 24948039
no worries :-) it happens to the best of us.  thanks again
0
 
LVL 12

Accepted Solution

by:
topdog770 earned 500 total points
ID: 25006596
Sorry mav, here, finally is some code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
 
namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        // simple and crude approach to storing user settings        
        SerializeConfiguration _sc = new SerializeConfiguration();
        UserSettings _userSettings = new UserSettings();
 
        public Form1()
        {
            InitializeComponent();
 
 
            this.Load += new EventHandler(OnFormLoad);
 
        }
 
        void OnFormLoad(object sender, EventArgs e)
        {
            //LoadSettings(null, null);
 
            UserSettings.OnPropertyChanged += new PropertyValueChangedEventHandler(_userSettings_OnPropertyChanged);
        }
 
        void _userSettings_OnPropertyChanged(object s, PropertyValueChangedEventArgs e)
        {
            ApplyUserSettings();
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_userSettings.Dirty)
            {
                DialogResult dr = MessageBox.Show("You have un-saved settings, would you like to save them now?", "Unsaved Settings", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
                else if (dr == DialogResult.Yes)
                {
                    SaveSettings(null, null);
                }
            }
        }
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        void SaveSettings(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".config";
            sfd.Filter = "*.config|*.config";
            sfd.FilterIndex = 0;
 
            sfd.FileName = System.Environment.UserName;
 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                _sc._userSettings = _userSettings;
                _sc.SaveSettings( sfd.FileName);
 
                UserSettings.Dirty = false;
            }
        }
 
        void LoadSettings(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = ".config";
            ofd.FileName = System.Environment.UserName;
 
            ofd.Filter = "*.config|*.config";
            ofd.FilterIndex = 0;
 
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                UserSettings = _sc.LoadSettings( ofd.FileName );
                ApplyUserSettings();
                UserSettings.Dirty = false;
            }
        }
 
        
        public UserSettings UserSettings
        {
            get { return _userSettings; }
            set { _userSettings = value; }
        }
 
        private void SetBackColor_Red(object sender, EventArgs e)
        {
            _userSettings.ColorOfBackground = Color.Red;
        }
 
        private void SetBackColor_Blue(object sender, EventArgs e)
        {
            _userSettings.ColorOfBackground = Color.Blue;
        }
 
        private void SetBackColor_Orange(object sender, EventArgs e)
        {
            _userSettings.ColorOfBackground = Color.Orange;
        }
 
        private void SetBackColor_White(object sender, EventArgs e)
        {
            _userSettings.ColorOfBackground = Color.White;
        }
 
        private void SetFontSize_Bigger(object sender, EventArgs e)
        {
            float fSize = this.Font.Size;
 
            if (fSize < 32)
            {
                fSize += 1;
            }
 
            _userSettings.SizeOfFont = fSize;
 
        }
 
        private void SetFontSize_Smaller(object sender, EventArgs e)
        {
            float fSize = this.Font.Size;
 
            if (fSize > 6)
            {
                fSize -= 1;
            }
 
            this.Font = new Font(this.Font.OriginalFontName, fSize, this.Font.Style);
        }
 
        private void SetWindowSize_Small(object sender, EventArgs e)
        {
            _userSettings.SizeOfWindow = UserSettings.WindowSize.small;  
        }
 
        private void SetWindowSize_Medium(object sender, EventArgs e)
        {
            _userSettings.SizeOfWindow = UserSettings.WindowSize.medium;  
        }
 
        private void SetWindowSize_Large(object sender, EventArgs e)
        {
            _userSettings.SizeOfWindow = UserSettings.WindowSize.large;            
        }
 
        void ApplyUserSettings()
        {
            this.Font = new Font(this.Font.OriginalFontName, _userSettings.SizeOfFont, this.Font.Style);
            this.BackColor = _userSettings.ColorOfBackground;
            
            int winsize = 200 * (int)_userSettings.SizeOfWindow;
            this.Size = new Size( winsize, winsize);
        }
 
 
        #region Designer code
 
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.loadSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.backgroundColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.redToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.blueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.orangeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.whiteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.arialToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.comicSansToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.windowSizeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.smallToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mediumToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.largeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.lblSampleText = new System.Windows.Forms.Label();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fiToolStripMenuItem,
            this.settingsToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(271, 24);
            this.menuStrip1.TabIndex = 2;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fiToolStripMenuItem
            // 
            this.fiToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.loadSettingsToolStripMenuItem,
            this.saveSettingsToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fiToolStripMenuItem.Name = "fiToolStripMenuItem";
            this.fiToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
            this.fiToolStripMenuItem.Text = "File";
            // 
            // loadSettingsToolStripMenuItem
            // 
            this.loadSettingsToolStripMenuItem.Name = "loadSettingsToolStripMenuItem";
            this.loadSettingsToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.loadSettingsToolStripMenuItem.Text = "Load Settings";
            this.loadSettingsToolStripMenuItem.Click += new System.EventHandler(this.LoadSettings);
            // 
            // saveSettingsToolStripMenuItem
            // 
            this.saveSettingsToolStripMenuItem.Name = "saveSettingsToolStripMenuItem";
            this.saveSettingsToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.saveSettingsToolStripMenuItem.Text = "Save Settings";
            this.saveSettingsToolStripMenuItem.Click += new System.EventHandler(this.SaveSettings);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // settingsToolStripMenuItem
            // 
            this.settingsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.backgroundColorToolStripMenuItem,
            this.fontToolStripMenuItem,
            this.windowSizeToolStripMenuItem});
            this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
            this.settingsToolStripMenuItem.Size = new System.Drawing.Size(58, 20);
            this.settingsToolStripMenuItem.Text = "Settings";
            // 
            // backgroundColorToolStripMenuItem
            // 
            this.backgroundColorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.redToolStripMenuItem,
            this.blueToolStripMenuItem,
            this.orangeToolStripMenuItem,
            this.whiteToolStripMenuItem});
            this.backgroundColorToolStripMenuItem.Name = "backgroundColorToolStripMenuItem";
            this.backgroundColorToolStripMenuItem.Size = new System.Drawing.Size(169, 22);
            this.backgroundColorToolStripMenuItem.Text = "Background Color";
            // 
            // redToolStripMenuItem
            // 
            this.redToolStripMenuItem.Name = "redToolStripMenuItem";
            this.redToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.redToolStripMenuItem.Text = "Red";
            this.redToolStripMenuItem.Click += new System.EventHandler(this.SetBackColor_Red);
            // 
            // blueToolStripMenuItem
            // 
            this.blueToolStripMenuItem.Name = "blueToolStripMenuItem";
            this.blueToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.blueToolStripMenuItem.Text = "Blue";
            this.blueToolStripMenuItem.Click += new System.EventHandler(this.SetBackColor_Blue);
            // 
            // orangeToolStripMenuItem
            // 
            this.orangeToolStripMenuItem.Name = "orangeToolStripMenuItem";
            this.orangeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.orangeToolStripMenuItem.Text = "Orange";
            this.orangeToolStripMenuItem.Click += new System.EventHandler(this.SetBackColor_Orange);
            // 
            // whiteToolStripMenuItem
            // 
            this.whiteToolStripMenuItem.Name = "whiteToolStripMenuItem";
            this.whiteToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.whiteToolStripMenuItem.Text = "White";
            this.whiteToolStripMenuItem.Click += new System.EventHandler(this.SetBackColor_White);
            // 
            // fontToolStripMenuItem
            // 
            this.fontToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.arialToolStripMenuItem,
            this.comicSansToolStripMenuItem});
            this.fontToolStripMenuItem.Name = "fontToolStripMenuItem";
            this.fontToolStripMenuItem.Size = new System.Drawing.Size(169, 22);
            this.fontToolStripMenuItem.Text = "Font Size";
            // 
            // arialToolStripMenuItem
            // 
            this.arialToolStripMenuItem.Name = "arialToolStripMenuItem";
            this.arialToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.arialToolStripMenuItem.Text = "Bigger";
            this.arialToolStripMenuItem.Click += new System.EventHandler(this.SetFontSize_Bigger);
            // 
            // comicSansToolStripMenuItem
            // 
            this.comicSansToolStripMenuItem.Name = "comicSansToolStripMenuItem";
            this.comicSansToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.comicSansToolStripMenuItem.Text = "Smaller";
            this.comicSansToolStripMenuItem.Click += new System.EventHandler(this.SetFontSize_Smaller);
            // 
            // windowSizeToolStripMenuItem
            // 
            this.windowSizeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.smallToolStripMenuItem,
            this.mediumToolStripMenuItem,
            this.largeToolStripMenuItem});
            this.windowSizeToolStripMenuItem.Name = "windowSizeToolStripMenuItem";
            this.windowSizeToolStripMenuItem.Size = new System.Drawing.Size(169, 22);
            this.windowSizeToolStripMenuItem.Text = "Window Size";
            // 
            // smallToolStripMenuItem
            // 
            this.smallToolStripMenuItem.Name = "smallToolStripMenuItem";
            this.smallToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.smallToolStripMenuItem.Text = "Small";
            this.smallToolStripMenuItem.Click += new System.EventHandler(this.SetWindowSize_Small);
            // 
            // mediumToolStripMenuItem
            // 
            this.mediumToolStripMenuItem.Name = "mediumToolStripMenuItem";
            this.mediumToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.mediumToolStripMenuItem.Text = "Medium";
            this.mediumToolStripMenuItem.Click += new System.EventHandler(this.SetWindowSize_Medium);
            // 
            // largeToolStripMenuItem
            // 
            this.largeToolStripMenuItem.Name = "largeToolStripMenuItem";
            this.largeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.largeToolStripMenuItem.Text = "Large";
            this.largeToolStripMenuItem.Click += new System.EventHandler(this.SetWindowSize_Large);
            // 
            // lblSampleText
            // 
            this.lblSampleText.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblSampleText.Location = new System.Drawing.Point(0, 24);
            this.lblSampleText.Name = "lblSampleText";
            this.lblSampleText.Size = new System.Drawing.Size(271, 186);
            this.lblSampleText.TabIndex = 3;
            this.lblSampleText.Text = "label1";
            this.lblSampleText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(271, 210);
            this.Controls.Add(this.lblSampleText);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fiToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem loadSettingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem saveSettingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem backgroundColorToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem redToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem blueToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem orangeToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem whiteToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem fontToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem arialToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem comicSansToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem windowSizeToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem smallToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem mediumToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem largeToolStripMenuItem;
        private System.Windows.Forms.Label lblSampleText;
 
 
        #endregion
    }
 
    [Serializable()]
    public class UserSettings
    {
        public enum WindowSize
        {
            small = 1,
            medium = 2,
            large = 3
        }
        private Color colorOfBackground = Control.DefaultBackColor;
 
        public Color ColorOfBackground
        {
            get { return colorOfBackground; }
            set
            {
                if (value != colorOfBackground)
                {
                    colorOfBackground = value;
                    Dirty = true;
 
                    FireChangeEvent();
                }
            }
        }
        private float sizeOfFont = 10f;
 
        public float SizeOfFont
        {
            get { return sizeOfFont; }
            set
            {
                if (value != sizeOfFont)
                {
                    sizeOfFont = value;
                    Dirty = true;
 
                    FireChangeEvent();
                }
            }
        }
        private WindowSize sizeOfWindow = WindowSize.small;
 
        public WindowSize SizeOfWindow
        {
            get { return sizeOfWindow; }
            set
            {
                if (value != sizeOfWindow)
                {
                    sizeOfWindow = value;
                    Dirty = true;
 
                    FireChangeEvent();
                }
            }
        }
 
        private void FireChangeEvent()
        {
            if (OnPropertyChanged != null)
            {
                // could use this event better by indicating what really changed..
                OnPropertyChanged(SizeOfWindow, null);
            }
        }
 
        bool _dirty = false;
 
        [field:NonSerialized]
        public bool Dirty
        {
            get { return _dirty; }
            set
            {
                if (value != _dirty)
                {
                    _dirty = value;
                }
            }
        }
 
        [field:NonSerialized]
        public static event PropertyValueChangedEventHandler OnPropertyChanged;
    }
 
    public class SerializeConfiguration
    {
        public SerializeConfiguration()
        {
        }
 
        public UserSettings _userSettings = new UserSettings();
 
        private String GetConfigFilePath(string fileName)
        {
            return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), fileName);
        }
 
        public void SaveSettings(string fileName)
        {
            Stream Write = null;
 
            try
            {
                FileInfo FI = new FileInfo(GetConfigFilePath(fileName));
 
                Write = FI.OpenWrite();
 
                BinaryFormatter BF = new BinaryFormatter();
 
                BF.Serialize(Write, _userSettings);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (Write != null)
                {
                    Write.Close();
                }
            }
        }
 
        public UserSettings LoadSettings(string fileName)
        {
            UserSettings result = new UserSettings();
 
            try
            {               
                using (Stream stream = File.Open(fileName, FileMode.Open))
                {
                    BinaryFormatter bFormatter = new BinaryFormatter();
                    result = (UserSettings)bFormatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
            }
 
            return result;
        }
 
    }
 
}

Open in new window

0
 

Author Comment

by:maveric191
ID: 25010088
Thanks :-) i've figured out how to get my colors to save using the settings and binding in VS.  I haven't figured out how to allow the user to save multiple color profiles using various names or how to get those names to show up in the initial form that allows users to add modify copy or delete the profile, or to show and allow for a quick swap of color profiles via the menu bar in the main form.  screenshots below are roughly what i'm trying to do.
opening.jpg
colorprofile.jpg
0
 

Author Comment

by:maveric191
ID: 25018520
here's a sample config file that a friend sent me.
sample-config.txt
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question