Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use a radio button on one form to dictate an action on another form?
    primarykey
    data
    text
    <p>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.</p> <p>The code/preferences interface is as follows:</p> <p><img src="https://i.stack.imgur.com/qmldA.png" alt="The &quot;Preferences&quot; interface."></p> <pre><code>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; } </code></pre> <p>There is no code behind the radio buttons. The modifiers are public.</p> <p>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.</p> <p>So what <em>should</em> 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.</p> <p>Thanks in advance for your help.</p> <p>---EDIT---</p> <p>On the "Preferences" form, the code behind the "Save" button is now as follows:</p> <pre><code>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(); } </code></pre> <p>And the Main Form strings:</p> <pre><code>public partial class Basic_Word_Processor : Form { public string keyboardFormat; public string dataFormat; </code></pre> <p>And the MainForm "ShowDialog" code:</p> <pre><code>private void preferencesToolStripMenuItem_Click(object sender, EventArgs e) { UserPreferences pref = new UserPreferences(); pref.ShowDialog(); dataFormat = pref.DataFormat; keyboardFormat = pref.KeyboardFormat; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload