【发布时间】:2013-05-06 17:45:07
【问题描述】:
我有两组单选按钮(2 组 4 个按钮),我想在程序/主窗体加载后立即保存检查状态并加载检查状态。单选按钮NOT在主窗体上。
如何使用 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。
我创建了两个“设置”。日期偏好和键盘偏好。我也不知道他们应该是什么“类型”。如果有人可以指导我,我将非常感激。我是编程新手,非常感谢您的帮助。
单选按钮被命名为:
ddmmyyy 呸呸呸 yyyyddmm yyyymmdd
qwerty qwertz 艾泽蒂 德沃夏克
感谢您的帮助。
--编辑--
我忘了说这是一个 WinForms 应用程序。
【问题讨论】:
-
如果没有更好的办法,可以给控件命名,然后保存选中控件的名称,然后启用选中控件。 (注意:不是显示的文字)
-
我该怎么做?
-
我很抱歉,因为我不在带有 Visual Studio 的计算机上,所以不能给出一个实际的例子(因此评论不回答)。但是您创建了一种将设置保存到文件的方法(不确定如何使用 property.settings 抱歉),然后您找到所选名称的单选按钮(radioButton.checked 是所选名称),然后保存该名称( radioButton.name)
-
我没有收到你的
UpdateUserChoice。是在您加载偏好表单时吗? -
@AdrienLacroix 是的。它加载了用户在会话期间选择的选项。但是当程序关闭时它不会记住这些。
标签: c# winforms settings preferences properties.settings