我有两组单选按钮(两组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。
我创建了两个“设置”。 DatePreference和KeyboardPreference。我不知道他们应该是什么“类型”。如果有人能指导我,我会很感激。我是编程新手,非常感谢您的帮助。
的单选按钮被命名为:
DDMMYYYY MMDDYYYY yyyyddmm 年月日
QWERTY QWERTZ AZERTY 德沃夏克
感谢您的帮助。
- EDIT--
我忘了提及这是一个WinForms应用程序。
如果没有人有一个更好的办法,你能说出控件,然后保存所选控件的名称,然后启用选定的控件。 (注意:不是显示的文字) – Sayse 2013-05-12 16:23:02
我该如何去做这件事? – Toby 2013-05-12 16:26:22
我appologise,因为我不与计算机上的视觉工作室,所以不能给一个实际的例子(因此评论不答复)。但是你做了一个方法,将设置保存到一个文件(不知道如何使用property.settings对不起),然后你会发现单选按钮选定的名称(radioButton.checked是选择的那个),然后保存该名称( radioButton.name) – Sayse 2013-05-12 16:29:13