Question

I have two groups of radio buttons (2 groups of 4 buttons), which I would like to save the checked status of and load the checked status as soon as the program/main form loads up. The radio buttons are NOT on the main form.

How can I do this using Properties.Settings?

The code on the "Preference" form is as follows:

public string DataFormat, KeyboardFormat;

public void UpdateUserChoice(string date, string keyboard)
    {
        if (date == ddmmyyyy.Text)
            ddmmyyyy.Checked = true;
        else if (date == mmddyyyy.Text)
            mmddyyyy.Checked = true;
        else if (date == yyyyddmm.Text)
            yyyyddmm.Checked = true;
        else if (date == yyyymmdd.Text)
            yyyymmdd.Checked = true;
        //----------------------------------------------------------
        if (keyboard == qwerty.Text)
            qwerty.Checked = true;
        else if (keyboard == qwertz.Text)
            qwertz.Checked = true;
        else if (keyboard == azerty.Text)
            azerty.Checked = true;
        else if (keyboard == dvorak.Text)
            dvorak.Checked = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (ddmmyyyy.Checked)
            DataFormat = ddmmyyyy.Text;
        else if (mmddyyyy.Checked)
            DataFormat = mmddyyyy.Text;
        else if (yyyyddmm.Checked)
            DataFormat = yyyyddmm.Text;
        else if (yyyymmdd.Checked)
            DataFormat = yyyymmdd.Text;
        //--------------------------------------------------
        if (qwerty.Checked)
            KeyboardFormat = qwerty.Text;
        else if (qwertz.Checked)
            KeyboardFormat = qwertz.Text;
        else if (azerty.Checked)
            KeyboardFormat = azerty.Text;
        else if (dvorak.Checked)
            KeyboardFormat = dvorak.Text;
        this.Close();
    }

And the code on the MainForm is:

private void DateStamp()
    {
        if (dateFormat.ToUpper() == "DD/MM/YYYY")
        {
            int CaretPosition = richTextBoxPrintCtrl1.SelectionStart;
            string TextBefore = richTextBoxPrintCtrl1.Text.Substring(0, CaretPosition);
            string textAfter = richTextBoxPrintCtrl1.Text.Substring(CaretPosition);
            string currentDate = DateTime.Now.ToString("dd-MM-yyyy");
            richTextBoxPrintCtrl1.SelectedText = currentDate;
        }
        else if (dateFormat.ToUpper() == "MM/DD/YYYY")
        {
            int CaretPosition = richTextBoxPrintCtrl1.SelectionStart;
            string TextBefore = richTextBoxPrintCtrl1.Text.Substring(0, CaretPosition);
            string textAfter = richTextBoxPrintCtrl1.Text.Substring(CaretPosition);
            string currentDate = DateTime.Now.ToString("MM-dd-yyyy");
            richTextBoxPrintCtrl1.SelectedText = currentDate;
        }
        else if (dateFormat.ToUpper() == "YYYY/DD/MM")
        {
            int CaretPosition = richTextBoxPrintCtrl1.SelectionStart;
            string TextBefore = richTextBoxPrintCtrl1.Text.Substring(0, CaretPosition);
            string textAfter = richTextBoxPrintCtrl1.Text.Substring(CaretPosition);
            string currentDate = DateTime.Now.ToString("yyyy-dd-MM");
            richTextBoxPrintCtrl1.SelectedText = currentDate;
        }
        else if (dateFormat.ToUpper() == "YYYY/MM/DD")
        {
            int CaretPosition = richTextBoxPrintCtrl1.SelectionStart;
            string TextBefore = richTextBoxPrintCtrl1.Text.Substring(0, CaretPosition);
            string textAfter = richTextBoxPrintCtrl1.Text.Substring(CaretPosition);
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
            richTextBoxPrintCtrl1.SelectedText = currentDate;

private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UserPreferences pref = new UserPreferences();
        pref.UpdateUserChoice(dateFormat, keyboardFormat);
        pref.ShowDialog();
        dateFormat = pref.DataFormat;
        keyboardFormat = pref.KeyboardFormat;
    }


    private void virtualKeyboardToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        if (keyboardFormat.ToUpper() == "QWERTY")
        {
            Virtual_Keyboard vKeyboard = new Virtual_Keyboard();
            vKeyboard.Show();
        }
        else if (keyboardFormat.ToUpper() == "QWERTZ")
        {
            QWERTZ qwertz = new QWERTZ();
            qwertz.Show();
        }
        else if (keyboardFormat.ToUpper() == "AZERTY")
        {
            AZERTY azerty = new AZERTY();
            azerty.Show();
        }
        else if (keyboardFormat.ToUpper() == "DVORAK")
        {
            DVORAK dvorak = new DVORAK();
            dvorak.Show();
        }
        }

I would like to save the checked status of the radio buttons (as seen in the picture attached), so that when the user reopens the program, these "settings" are also loaded. How would I achieve this? Using Properties.Settings if it's possible.

I've created two "Settings". DatePreference and KeyboardPreference. I don't know what "type" they should be, either. If somebody could guide me, I'd really appreciate it. I'm new to programming so thank you for your help.

The RadioButtons are named:

ddmmyyyy mmddyyyy yyyyddmm yyyymmdd

qwerty qwertz azerty dvorak

Thanks for your help.

--EDIT--

I forgot to mention that this is a WinForms application.

Settings

Preferences Dialog

Was it helpful?

Solution

Example for the date (you can do the same for keyboard) :

Maybe you can create an enum like this :

public enum DatePreference { dd_mm_yyyy, mm_dd_yyyy, yyyy_dd_mm, yyyy_mm_dd };

Set in the Settings DatePreference as Integer

For your Preference form code :

UpdateUserChoice :

if (Properties.Settings.Default.DatePreference == (int)DatePreference.dd_mm_yyyy)
    ddmmyyyy.Checked = true;

button1_Click :

if (ddmmyyyy.Checked)
{
    DataFormat = ddmmyyyy.Text;
    Properties.Settings.Default.DatePreference = (int)DatePreference.dd_mm_yyyy;
}

Think to save the changes with Properties.Settings.Default.Save(); !

For your Main form code :

    if (Properties.Settings.Default.DatePreference == (int)DatePreference.dd_mm_yyyy)
    {
        int CaretPosition = richTextBoxPrintCtrl1.SelectionStart;
        string TextBefore = richTextBoxPrintCtrl1.Text.Substring(0, CaretPosition);
        string textAfter = richTextBoxPrintCtrl1.Text.Substring(CaretPosition);
        string currentDate = DateTime.Now.ToString("dd-MM-yyyy");
        richTextBoxPrintCtrl1.SelectedText = currentDate;
    }
    [...]

OTHER TIPS

I would represent the values in an enum.

    public enum AvailableKeyboardLayouts
    {
        DVORAK = 0,
        QWERTY = 1,
        QWERTZ = 2
    }

Using the Settings file you can save the type as string or int. Use Enum.Parse to transform the object

Saving and loading from the Settings files are easy:

My settings file is Settings.settings

Settings.Default.KeyboardPreference = AvailableKeyboardLayouts.DVORAK;
Settings.Default.Save();

How about using the Resources.resx file (depends on the project) Also you can use an xml file to save the values. If you have many settings then you can use a Datatable (named for example "Settings") , place it inside a Dataset and use SaveToXml and LoadFromXml for faster results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top