我有一个应用程序,我有一个“首选项”表单,以便允许用户选择他们的首选项。表单有一些单选按钮,允许用户选择他们的首选项。在另一种形式上,我有一些代码会根据检查的按钮做出不同的反应。
代码/首选项界面如下:
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.Checked)”。我不确定现在该做什么。
那么当用户去添加日期戳时,应该发生什么,是否应该检查单选按钮的已检查状态并添加与选中的单选按钮对应的日期戳。
提前感谢您的帮助。
--- --- EDIT
在“首选项”表单中,“保存”按钮后面的代码现在如下:
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;
}
问题是它没有保存按钮的“已检查”状态。一旦对话框关闭,它就会返回到先前的状态。
答案 0 :(得分:1)
我认为通过理解这个例子,你可以做你正在做的事情:
我有一个名为Form1
的主要表单我想在此表单中显示用户选择。
我还添加了一个名为Preferences
的新表单,以便用户可以选择日期格式和键盘布局:
我的单选按钮的名称如下:
RB_D_1
RB_D_2
.
.
.
用户点击Submit Changes
后,我们会检查选择了哪个radioButton并将其Text
属性(ex RB_D_1.text
为“dd / MM / yyyy”)存储到{{1我们在这里调用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
TextBoxes
更新2:
像这样更改 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;
}
:
DateStamp()
更新3:
用于记住用户选择:
将此功能添加到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;
}
}
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;
}