简体   繁体   中英

How can I use a radio button on one form to dictate an action on another form?

I have an application where I have a "preferences" form in order to allow the user to select their preferences. The form has some radio buttons that will allow the user to choose their preferences. On another form, I have some code which will react differently depending on which button is checked.

The code/preferences interface is as follows:

“首选项”界面。

private void DateStamp()
    {
        if (UserPreferences.Instance.ddmmyyyy.Checked)
        {
            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 (UserPreferences.Instance.mmddyyyy.Checked)
        {
            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 (UserPreferences.Instance.yyyyddmm.Checked)
        {
            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 (UserPreferences.Instance.yyyymmdd.Checked)
        {
            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;
        }

There is no code behind the radio buttons. The modifiers are public.

The issue I have, though is that when I try to add a "Datestamp", I get a System.NullReferenceException {"Object reference not set to an instance of an object."} error on line "if (UserPreferences.Instance.ddmmyyyy.Checked)". I'm unsure of what to do now.

So what should happen when the user goes to add a datestamp, is it should check the checked state of the radio buttons and add the datestamp that corresponds to the checked radio button.

Thanks in advance for your help.

---EDIT---

On the "Preferences" form, the code behind the "Save" button is now as follows:

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 Main Form strings:

public partial class Basic_Word_Processor : Form
{
    public string keyboardFormat;
    public string dataFormat;

And the MainForm "ShowDialog" code:

private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UserPreferences pref = new UserPreferences();
        pref.ShowDialog();

        dataFormat = pref.DataFormat;
        keyboardFormat = pref.KeyboardFormat;
    }

The issue is that it doesn't save the "checked" status of the buttons. It returns to the previous state as soon as the Dialog is closed.

I think by understanding this example you can do what you're attending to do :

I have a main form called Form1 I want to show User Choices in this form . and also I added a new form called Preferences so user will be able to choose date Format and Keyboard Layout :

my radio buttons are named like this :

RB_D_1
RB_D_2
.
.
.

after user clicks on Submit Changes we'll check which radioButton is selected and store it's Text property (ex RB_D_1.text is "dd/MM/yyyy" ) to a Public String Variable called DateFormat here we go :

    public string DataFormat, KeyboardFormat;

    private void CMDSubmit_Click(object sender, EventArgs e)
    {
        if (RB_D_1.Checked)
            DataFormat = RB_D_1.Text;
        else if (RB_D_2.Checked)
            DataFormat = RB_D_2.Text;
        else if (RB_D_3.Checked)
            DataFormat = RB_D_3.Text;
        else if (RB_D_4.Checked)
            DataFormat = RB_D_4.Text;
        else
            DataFormat = "MM/DD/YYYY"; // default format

        //--------------------------------

        if (RB_L_1.Checked)
            KeyboardFormat = RB_L_1.Text;
        else if (RB_L_2.Checked)
            KeyboardFormat = RB_L_2.Text;
        else if (RB_L_3.Checked)
            KeyboardFormat = RB_L_3.Text;
        else if (RB_L_4.Checked)
            KeyboardFormat = RB_L_4.Text;
        else
            KeyboardFormat = "QWERTY"; // default format


        this.Close();
    }

now we have saved user choices in two string variables so we can reach them from our Form1

in Form1 whenever user clicks on Setting we'll make an object from our Preferences form and Show it to user after closing the Preferences form , we'll check those two string Variables which we already talked about and decide what to do with those results :

for example I stored those results into another two string variables and Showed Them in TextBoxes

    private void CMDSettings_Click(object sender, EventArgs e)
    {
        // showing the Preferences form to user 
        Preferences pref = new Preferences();
        pref.ShowDialog();

        // getting results from Preferences Form
        dateFormat = pref.DataFormat;
        keyboardFormat = pref.KeyboardFormat;

        // Showing the Result in TextBoxes
        textBox1.Text = dateFormat ;
        textBox2.Text = keyboardFormat;
    }

UPDATE 2 :

change DateStamp() like this :

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;
    }

}

UPDATE 3 :

for remembering user choices do this :

add this function to Preferences

    public void UpdateUserChoice(string date,string keyboard)
    {
        if (date == RB_D_1.Text)
            RB_D_1.Checked = true;
        else if (date == RB_D_2.Text)
            RB_D_2.Checked = true;
        else if (date == RB_D_3.Text)
            RB_D_3.Checked = true;
        else if (date == RB_D_4.Text)
            RB_D_4.Checked = true; 

        //---------------------------

        if (keyboard == RB_L_1.Text)
            RB_L_1.Checked = true;
        else if (keyboard == RB_L_2.Text)
            RB_L_2.Checked = true;
        else if (keyboard == RB_L_3.Text)
            RB_L_3.Checked = true;
        else if (keyboard == RB_L_4.Text)
            RB_L_4.Checked = true; 
    }

and change the old way of showing to user like this :

    private void CMDSettings_Click(object sender, EventArgs e)
    {
        Preferences pref = new Preferences();
        pref.UpdateUserChoice(dateFormat, keyboardFormat);
        pref.ShowDialog();
        dateFormat = pref.DataFormat;
        keyboardFormat = pref.KeyboardFormat;
        textBox2.Text = keyboardFormat;
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM