繁体   English   中英

如何在一个表单上使用单选按钮指示对另一个表单的操作?

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

我有一个具有“首选项”表单的应用程序,以便允许用户选择他们的首选项。 该表单具有一些单选按钮,这些按钮将允许用户选择其首选项。 在另一种形式上,我有一些代码会根据选中的按钮而做出不同的反应。

代码/首选项界面如下:

“首选项”界面。

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

单选按钮后面没有代码。 修饰符是公共的。

但是,我遇到的问题是,当我尝试添加“ Datestamp”时,出现了System.NullReferenceException {“对象引用未设置为对象的实例。”}错误在“ if(UserPreferences.Instance.ddmmyyyy 。选中)”。 我不确定现在该怎么办。

因此,当用户去添加时间戳发生什么,是它应检查单选按钮的选中状态,并添加对应于检查单选按钮的邮戳。

在此先感谢您的帮助。

- -编辑 - -

现在,在“首选项”窗体上,“保存”按钮后面的代码如下:

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

和主要形式的字符串:

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

和MainForm的“ ShowDialog”代码:

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

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

问题是它不保存按钮的“已检查”状态。 对话框关闭后,它将返回到先前的状态。

我认为通过理解此示例,您可以做自己正在做的事情:

我有一个名为Form1的主表单,我想以此表单显示“用户选择”。 并且我还添加了一个名为Preferences的新表单,因此用户可以选择日期格式和键盘布局:

我的单选按钮的命名如下:

RB_D_1
RB_D_2
.
.
.

用户单击“ Submit Changes我们将检查选中了哪个radioButton并将其Text属性(例如RB_D_1.text为“ dd / MM / yyyy”)存储到名为DateFormatPublic String Variable如下所示:

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

现在我们已经将用户选择保存在两个字符串变量中,因此我们可以从Form1它们

Form1每当用户单击“ Setting我们都会从Preferences表单中创建一个对象,并在关闭“首选项”表单后将其显示给用户,我们将检查我们已经讨论过的两个字符串变量,并决定如何处理这些结果:

例如,我将这些结果存储到另外两个字符串变量中,并在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;
    }

更新2:

像这样更改DateStamp()

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

}

更新3:

为了记住用户的选择,请执行以下操作:

将此功能添加到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; 
    }

并像这样更改向用户显示的旧方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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