我有两组单选按钮(2 组 4 个按钮),我想保存检查状态并在程序/主窗体加载后立即加载检查状态。单选按钮不在主窗体上。
如何使用 Properties.Settings 执行此操作?
“偏好”表单上的代码如下:
public string DataFormat, KeyboardFormat;
public void UpdateUserChoice(string date, string keyboard)
{
if (date == ddmmyyyy.Text)
ddmmyyyy.Checked = true;
else if (date == mmddyyyy.Text)
mmddyyyy.Checked = true;
else if (date == yyyyddmm.Text)
yyyyddmm.Checked = true;
else if (date == yyyymmdd.Text)
yyyymmdd.Checked = true;
//----------------------------------------------------------
if (keyboard == qwerty.Text)
qwerty.Checked = true;
else if (keyboard == qwertz.Text)
qwertz.Checked = true;
else if (keyboard == azerty.Text)
azerty.Checked = true;
else if (keyboard == dvorak.Text)
dvorak.Checked = true;
}
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();
}
MainForm 上的代码是:
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;
private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
UserPreferences pref = new UserPreferences();
pref.UpdateUserChoice(dateFormat, keyboardFormat);
pref.ShowDialog();
dateFormat = pref.DataFormat;
keyboardFormat = pref.KeyboardFormat;
}
private void virtualKeyboardToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (keyboardFormat.ToUpper() == "QWERTY")
{
Virtual_Keyboard vKeyboard = new Virtual_Keyboard();
vKeyboard.Show();
}
else if (keyboardFormat.ToUpper() == "QWERTZ")
{
QWERTZ qwertz = new QWERTZ();
qwertz.Show();
}
else if (keyboardFormat.ToUpper() == "AZERTY")
{
AZERTY azerty = new AZERTY();
azerty.Show();
}
else if (keyboardFormat.ToUpper() == "DVORAK")
{
DVORAK dvorak = new DVORAK();
dvorak.Show();
}
}
我想保存单选按钮的选中状态(如附图所示),这样当用户重新打开程序时,这些“设置”也会被加载。我将如何实现这一目标?如果可能,请使用 Properties.Settings。
我创建了两个“设置”。日期偏好和键盘偏好。我也不知道他们应该是什么“类型”。如果有人可以指导我,我将非常感激。我是编程新手,所以谢谢你的帮助。
单选按钮被命名为:
ddmmyyyy mmddyyyy yyyyddmm yyyymmdd
qwerty qwertz azerty dvorak
谢谢你的帮助。
- 编辑 -
我忘了说这是一个 WinForms 应用程序。