我正在处理存储上次使用的设置,然后在程序启动时检索它们。一切正常,但我不知道如何处理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 :(得分:3)
添加所有值后,只需检索/设置SelectedIndex
(除非在设计时已经发生)。
但总的来说,我会改写设置处理。您应该使用键和值存储设置。否则,如果您想要添加,删除或更改某些设置的顺序,则会遇到吨问题。