0

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

代码/首选项界面如下:

这

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

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

不过,我遇到的问题是,当我尝试添加“日期戳”时,出现 System.NullReferenceException {"Object reference not set to an instance of an object."} 错误“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;
    }

问题是它不保存按钮的“已检查”状态。一旦 Dialog 关闭,它就会返回到之前的状态。

4

1 回答 1

1

我认为通过理解这个例子,你可以做你正在做的事情:

我有一个名为Form1I want to show User Choices in this form 的主表单。我还添加了一个名为的新表单,Preferences以便用户可以选择日期格式和键盘布局:

我的单选按钮命名如下:

RB_D_1
RB_D_2
.
.
.

用户点击后,Submit Changes我们将检查选择了哪个单选按钮并将其Text属性(例如RB_D_1.text"dd/MM/yyyy" )存储到Public String Variable调用DateFormat这里我们去:

    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在关闭 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;
    } 
于 2013-05-12T12:38:49.877 回答