Home > other >  How can I make my form in visual studio to remember the last checked ReadioButtom among 5 buttoms in
How can I make my form in visual studio to remember the last checked ReadioButtom among 5 buttoms in

Time:02-23

So I have what I think is a very basic question but I do not know how to do it. So I am making launcher for a game in visual studio using C# where the user has to press one RadioButtom among 5 to select a language.

The buttoms are just placing some characters to the string varable x.

It is on default on English but if someone selects Spanish I want the RadioButtom in the form to be on Spanish when the form gets open the second time since Spanish was the last one selected, but everytime the form gets open it goes back to the default English buttom.

        if (rbEnglish.IsChecked == true)
            x = "eng";
        if (rbSpanish.IsChecked == true)
            x = "spa";
        if (rbItalian.IsChecked == true)
            x = "ita";
        if (rbGerman.IsChecked == true)
            x = "ger";
        if (rbFrench.IsChecked == true)
            x = "fre";

After this is just a buttom that reads the string in x to start the game in the selected RadioButtom language. I want to make it so people dont have to select their prefered language everytime so they can just press the buttom.

CodePudding user response:

If you want the radio buttons in the form to be on the last checked button, you can refer to the following steps: First create an enum called Language:

public enum Language
    {
        eng,
        spa,
        ita,
        ger,
        fre
    }

Second double-click on Setting.settings file and set Language as int.

enter image description here enter image description here

Finally you can refer to this code:

public partial class Form1 : Form
{
    public string x;
    public Form1()
    {
        InitializeComponent();
        if (Settings.Default.Language == (int)Language.eng)
            rbEnglish.Checked = true;
        if (Settings.Default.Language == (int)Language.spa)
            rbSpanish.Checked = true;
        if (Settings.Default.Language == (int)Language.ita)
            rbItalian.Checked = true;
        if (Settings.Default.Language == (int)Language.ger)
            rbGerman.Checked = true;
        if (Settings.Default.Language == (int)Language.fre)
            rbFrench.Checked = true;
    }
    public enum Language
    {
        eng,
        spa,
        ita,
        ger,
        fre
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (rbEnglish.Checked)
        {
            Settings.Default.Language = (int)Language.eng;
            x = Language.eng.ToString();
        }
        if (rbSpanish.Checked)
        {
            Settings.Default.Language = (int)Language.spa;
            x = Language.spa.ToString();
        }
        if (rbItalian.Checked)
        {
            Settings.Default.Language = (int)Language.ita;
            x = Language.ita.ToString();
        }
        if (rbGerman.Checked)
        {
            Settings.Default.Language = (int)Language.ger;
            x = Language.ger.ToString();
        }
        if (rbFrench.Checked)
        {
            Settings.Default.Language = (int)Language.fre;
            x = Language.fre.ToString();
        }
        textBox1.Text = x;
        Settings.Default.Save();
    }
}

Here is the test result

enter image description here

  • Related