2012-01-12 83 views
1

我正在努力保存上次使用的設置,然後程序開始將其恢復。一切工作正常,但我只是不知道如何處理ListBox項目。如何將ListBox項目存儲到文件中並將其恢復回去?

指出一件事是我已經使用一個分隔符來存儲我的設置。處理這個問題時我感到很困惑。

這是我如何保存我的設置:

private void btnStart_Click(object sender, EventArgs e) 
{ 
    int interval = 0; 
    int plusMinus = 0; 
    int pause = 0; 
    int delay = 0; 
    int randomLine = 0; 

    if (cbPause.Checked == true) pause = 1; 
    if (cbDelay.Checked == true) delay = 1; 
    if (cbRandomLine.Checked == true) randomLine = 1; 
    interval = int.Parse(nudInterval.Value.ToString()); 
    plusMinus = int.Parse(nudPlusMinus.Value.ToString()); 

    lastUsed.Text = 
     interval + splitString + 
     plusMinus + splitString + 
     pause + splitString + 
     delay + splitString + 
     randomLine; 

    if (nudPlusMinus.Value == 0) 
    { 
     tmrInterval.Interval = int.Parse(nudInterval.Value.ToString()); 
    } 
    else 
    { 
     Random random = new Random(); 
     tmrInterval.Interval = random.Next(int.Parse(nudInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString())); 
    } 

    WhenStarted(); 

    tmrInterval.Start(); 
} 

這是我找回它們在程序啓動:

public AutoTyper() 
{ 
    InitializeComponent(); 

    tmrInterval.Tick += new EventHandler(Interval); 
    tmrDelay.Tick += new EventHandler(Delay); 
    tmrSpace.Tick += new EventHandler(Space); 

    lbMessage.SelectedIndexChanged += new EventHandler(lbMessage_SelectedIndexChanged); 
    txtMessage.TextChanged += new EventHandler(txtMessage_TextChanged); 

    SetInterval(); 

    if (!lastUsed.EmptyFile()) 
    { 
     string[] allSettings = lastUsed.Text.Split(splitChar, StringSplitOptions.None); 
     int settingCount = 0; 
     int settingNumber = 0; 

     foreach (string setting in allSettings) settingNumber++; 

     if (settingNumber == 5) 
     { 
      foreach (string setting in allSettings) 
      { 
       settingCount++; 

       if (settingCount == 1) nudInterval.Value = int.Parse(setting); 
       else if (settingCount == 2) nudPlusMinus.Value = int.Parse(setting); 
       else if (settingCount == 3) { if (setting == "1") cbPause.Checked = true; } 
       else if (settingCount == 4) { if (setting == "1") cbDelay.Checked = true; } 
       else if (settingCount == 5) { if (setting == "1") cbRandomLine.Checked = true; } 
      } 
     } 
    } 
} 
+0

這是你在看什麼http://stackoverflow.com/questions/1700779/c-sharp-way-to-serialize-liststring-in-settings-containing-any-character-rege – Lloyd 2012-01-12 11:04:19

+0

@Lloyd:I關於使用XML是空白的。 – HelpNeeder 2012-01-12 11:15:05

+2

@HelpNeeder也許有其他方式,但這是最好的。 – Burimi 2012-01-12 12:05:28

回答

3

只是取回/添加所有值後設置SelectedIndex(除非在設計時已經發生)。

但總的來說,我會重寫那個設置處理。您應該使用鍵和值存儲您的設置。否則,如果您想添加,刪除或更改某些設置的順序,則會遇到噸問題。

+0

嗯,這是我能想出的最簡單的解決方案,它看起來確實能完成這項工作。 – HelpNeeder 2012-01-12 11:05:45

+1

這完全好,如果這就是你必須保存的。但是延伸很難(因爲你可能不得不處理不同的版本)。如果你願意,可以看看序列化等內容。將允許您簡單地序列化/反序列化包含您的值的對象(例如結構或映射)。 – Mario 2012-01-12 11:08:38

+0

我不知道如何開始你的想法。你介意給我看一些關於相關話題的東西嗎? – HelpNeeder 2012-01-12 11:14:20

相關問題