how to save the state of radio buttons on application exit.how to get or set check state of radio buttons of a form,from another form.pleasssssssssssssssssssssssssssss help me with dis..its very urgent.i am new to c sharp.i will be really very grateful. :) thank you

Dani AI

Generated

This is a WinForms desktop scenario (as clarified by and prompted by ). pointed toward the Settings system — the simplest, most robust pattern is to store a single identifier for the checked radio (a name or index) in a user-scoped setting, persist it on exit, and restore it when the form that owns those radios loads.

Add a user-scoped setting (Project → Properties → Settings) called SelectedRadioName (string). Save the checked radio on form close with a small recursive helper:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    var checkedRb = FindCheckedRadio(this);
    Properties.Settings.Default.SelectedRadioName = checkedRb?.Name ?? "";
    Properties.Settings.Default.Save();
}

private RadioButton FindCheckedRadio(Control parent)
{
    foreach (Control c in parent.Controls)
    {
        if (c is RadioButton rb && rb.Checked) return rb;
        var child = FindCheckedRadio(c);
        if (child != null) return child;
    }
    return null;
}

Restore that state in the form Load using a recursive finder:

private void MyForm_Load(object sender, EventArgs e)
{
    string name = Properties.Settings.Default.SelectedRadioName;
    if (!string.IsNullOrEmpty(name))
    {
        var ctrl = FindControlRecursive(this, name) as RadioButton;
        if (ctrl != null) ctrl.Checked = true;
    }
}

private Control FindControlRecursive(Control parent, string name)
{
    if (parent.Name == name) return parent;
    foreach (Control c in parent.Controls)
    {
        var found = FindControlRecursive(c, name);
        if (found != null) return found;
    }
    return null;
}

To change a radio on one form from another, expose a small public method/property on the owning form and call it (pass the owner into the child or obtain the instance from Application.OpenForms):

public void SetSelectionByName(string radioName)
{
    var rb = FindControlRecursive(this, radioName) as RadioButton;
    if (rb != null) rb.Checked = true;
}

Notes and cautions: ensure the setting is User-scoped (application-scoped settings are read-only at runtime). Call Save() to persist changes. Prefer storing one identifier (name/index) rather than many booleans. If radios are created dynamically or in multiple groups, adapt the naming/lookup accordingly.

Recommended Answers

All 4 Replies

Is it a Web Application that you are using?

Is it a Web Application that you are using?

not web..desktop application... i need to save states of few radio buttons on exit..and check the states on staring again..radio buttons are not in the main startup form.

or i just need to know how to change checked state of radio button of 1 form,from another form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.