2013-05-12 63 views
3

我有两组单选按钮(两组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应用程序。

Settings

Preferences Dialog

+0

如果没有人有一个更好的办法,你能说出控件,然后保存所选控件的名称,然后启用选定的控件。 (注意:不是显示的文字) – Sayse 2013-05-12 16:23:02

+0

我该如何去做这件事? – Toby 2013-05-12 16:26:22

+0

我appologise,因为我不与计算机上的视觉工作室,所以不能给一个实际的例子(因此评论不答复)。但是你做了一个方法,将设置保存到一个文件(不知道如何使用property.settings对不起),然后你会发现单选按钮选定的名称(radioButton.checked是选择的那个),然后保存该名称( radioButton.name) – Sayse 2013-05-12 16:29:13

回答

1

实例的日期(你可以为键盘做相同):

也许你可以创建一个这样的枚举:

public enum DatePreference { dd_mm_yyyy, mm_dd_yyyy, yyyy_dd_mm, yyyy_mm_dd }; 

设置在设置DatePreference为整数

对于您的首选项表单代码:

UpdateUserChoice:

if (Properties.Settings.Default.DatePreference == (int)DatePreference.dd_mm_yyyy) 
    ddmmyyyy.Checked = true; 

的button1_Click:

if (ddmmyyyy.Checked) 
{ 
    DataFormat = ddmmyyyy.Text; 
    Properties.Settings.Default.DatePreference = (int)DatePreference.dd_mm_yyyy; 
} 

想保存更改与Properties.Settings.Default.Save();

为了您主要表单代码:

if (Properties.Settings.Default.DatePreference == (int)DatePreference.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; 
    } 
    [...] 
+0

我会试试这个。 – Toby 2013-05-12 16:50:10

+1

对不起。正如我所说的,我是编程新手,所以这对我来说有点困惑。你能再解释一下吗? – Toby 2013-05-12 16:52:59

+0

当然,你不明白什么? – 2013-05-12 16:53:52

0

如何使用Resources.resx文件(取决于项目) 您也可以使用XML文件来保存值。如果你有很多设置,那么你可以使用Datatable(例如命名为“Settings”),将它放在Dataset中并使用SaveToXml和LoadFromXml来获得更快的结果。

+0

我该怎么做? – Toby 2013-05-12 16:54:02

+0

这个文章将帮助你:[链接](http://www.codeproject.com/Tips/350010/Saving-User-Settings-in-Winform-Application) 也可以为单选按钮创建每个组的Enum并以整数形式将其保存到设置文件中,因此您将知道 0是gor QWERTY,1代表QWERTZ等。 您将保存在窗体的关闭事件并在加载的事件中加载。希望能帮助到你。 – 2013-05-12 17:00:38

1

我将代表一个枚举值。

public enum AvailableKeyboardLayouts 
    { 
     DVORAK = 0, 
     QWERTY = 1, 
     QWERTZ = 2 
    } 

使用设置文件,您可以将类型保存为字符串或整型。使用Enum.Parse从设置文件转换对象

保存和加载很简单:

我的设置文件是Settings.settings

Settings.Default.KeyboardPreference = AvailableKeyboardLayouts.DVORAK; 
Settings.Default.Save(); 
+0

你有同样的想法;) – 2013-05-12 16:38:26