How to persist the user information in drop down in a windows form app for different user login using c#

Dineshkumar.S 451 Reputation points
2023-04-13T05:41:48.52+00:00

I am currently looking at a better approach to remember/persist previous state of the controls on a .NET Windows Form using c# For example, there are 8 drop down list menu controls on a windows form. And user previously selected some items in these drop down menu. What I'd like to do here is: when this WinForm is loaded again, user's previous selections shall be recovered and remain the same. for this i have tried using saving user settings method in settings,settings config file but now i want to do the same for the different user login to the application in the same system. can anyone suggest me the method to do that ? Thanks in advance

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,648 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2023-04-13T14:27:01.12+00:00

    Hi @Dineshkumar.S ,Welcome to Microsoft Q&A.

    There are several methods to choose from. You can continue to choose built-in, or you can choose to use a database, or you can use the registry.

    Since you used the built-in before, I also show you the usage of the built-in:

    You can create different config files for different users.

    When a user logs in, just call different config files according to the user.

    Just for reference:

    You also need to consider encryption, and create config for each user, you can put the config address in the property, etc.

    using System.Configuration;
    
    namespace WinFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string aConfigPath = @"C:\Users\Administrator\source\repos\WindowsFormsApp8\WinFormsApp1\Config\a.config";
            string bConfigPath = @"C:\Users\Administrator\source\repos\WindowsFormsApp8\WinFormsApp1\Config\b.config";
    
            private void UpdateComboBoxSelection(string configPath, string key, ComboBox comboBox)
            {
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                comboBox.SelectedIndex = Convert.ToInt32(config.AppSettings.Settings[key].Value);
            }
    
            private void SubmitComboBoxSelection(string configPath, string key, ComboBox comboBox)
            {
                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                config.AppSettings.Settings[key].Value = comboBox.SelectedIndex.ToString();
                config.Save();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string textBoxValue = textBox1.Text.Trim();
                if (textBoxValue == "a")
                {
                    UpdateComboBoxSelection(aConfigPath, "C1SelectedItem", comboBox1);
                    UpdateComboBoxSelection(aConfigPath, "C2SelectedItem", comboBox2);
                }
                else if (textBoxValue == "b")
                {
                    UpdateComboBoxSelection(bConfigPath, "C1SelectedItem", comboBox1);
                    UpdateComboBoxSelection(bConfigPath, "C2SelectedItem", comboBox2);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string textBoxValue = textBox1.Text.Trim();
                if (textBoxValue == "a")
                {
                    SubmitComboBoxSelection(aConfigPath, "C1SelectedItem", comboBox1);
                    SubmitComboBoxSelection(aConfigPath, "C2SelectedItem", comboBox2);
                }
                else if (textBoxValue == "b")
                {
                    SubmitComboBoxSelection(bConfigPath, "C1SelectedItem", comboBox1);
                    SubmitComboBoxSelection(bConfigPath, "C2SelectedItem", comboBox2);
                }
            }
        }
    }
    
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful