4

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

Toby
  • 377
  • 1
  • 10
  • 23
  • if noone has a better way, you could name the controls, and then save the name of the selected control, and then enable the selected control. (Note: not the text displayed) – Sayse May 12 '13 at 16:23
  • How would I go about doing this? – Toby May 12 '13 at 16:26
  • I appologise as I'm not on a computer with visual studio so can't give an actual example (hence comment not answer). But you make a method that will save settings to a file (not sure about how to use property.settings sorry) and then you find the radio button thats selected's name (radioButton.checked is the one thats selected) and then save that name (radioButton.name) – Sayse May 12 '13 at 16:29
  • I don't get your `UpdateUserChoice`. Is it when you load your Preference form ? – Adrien Lacroix May 12 '13 at 16:46
  • @AdrienLacroix Yes. It loads up the user's choice that they've selected during the session. But it doesn't memorize these when the program has closed. – Toby May 12 '13 at 16:49

3 Answers3

1

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;
    }
    [...]
Adrien Lacroix
  • 3,502
  • 1
  • 20
  • 23
  • 1
    I'm sorry. As I said, I'm new to programming, so this is kind of confusing to me. Could you please explain a bit more? – Toby May 12 '13 at 16:52
  • Sure, what do you don't understand ? – Adrien Lacroix May 12 '13 at 16:53
  • Well, in general I am struggling to understand how I would implement this correctly. I've changed the two settings to "int" and added the "enum DatePreference", but I don't understand how I do the rest. – Toby May 12 '13 at 16:57
  • I'm sorry. I'm new to programming, so this is all a learning experience. – Toby May 12 '13 at 16:57
  • Not a problem. Glad I can help :) I've just changed your `if` condition. For example, in your **Main form** : `if (dateFormat.ToUpper() == "DD/MM/YYYY")` => `if (Properties.Settings.Default.DatePreference == (int)DatePreference.dd_mm_yyyy)` Same for others dates – Adrien Lacroix May 12 '13 at 16:59
  • Do I replace 'if (keyboard == qwerty.Text) qwerty.Checked = true;' with 'if (Properties.Settings.Default.DatePreference == (int)DatePreference.dd_mm_yyyy) ddmmyyyy.Checked = true;'? – Toby May 12 '13 at 17:16
  • Adapt my code which is for your date with your keyboard. The enum would be like this : `public enum KeyboardPreference { QWERTY, QWERTZ, AZERTY, DVORAK };` And for `if (keyboard == qwerty.Text)`, replace with : `if (Properties.Settings.Default.KeyboardPreference == (int)KeyboardPreference.qwerty)` My code was just a sample for your date. Adapt it with the keyboard. – Adrien Lacroix May 12 '13 at 17:19
  • This code isn't playing nice. :/ I have eight errors. All with the DatePreference part. "The name "DatePreference" does not exist in the current context" Properties.Settings.Default.DatePreference = (int) //DatePreference// .yyyy_mm_dd; Any ideas? (I have "uncommented" the part which throws up an error. There are eight of these. All the same.) – Toby May 12 '13 at 17:26
  • Add the `public` keyword in front of the declaration of the enum like this : `public enum DatePreference { dd_mm_yyyy, mm_dd_yyyy, yyyy_dd_mm, yyyy_mm_dd };` – Adrien Lacroix May 12 '13 at 17:28
  • Where did you put the enum declaration ? It should be at the top of the document, just above the `using ...` – Adrien Lacroix May 12 '13 at 17:31
  • ...Oh I'll try that now. I wasn't aware of that. – Toby May 12 '13 at 17:32
1

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();
Jras
  • 518
  • 3
  • 12
0

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.

  • THis aricle will help you : [link](http://www.codeproject.com/Tips/350010/Saving-User-Settings-in-Winform-Application) Also for the radiobuttons you can create an Enum for each group and save it to the settings file as an integer so you will know that 0 is gor QWERTY , 1 for QWERTZ etc. You will save at the closing event of the form and load at the loaded event. Hope it helps. – Emmanouil Chountasis May 12 '13 at 17:00