私はいくつかのことを行うタイマーイベントを持っています。私がそれを達成しようとしている1つのアイテムは、タイマーが私が実行している完了したアクションにヒットしたときにチェックされるCheckListBox
アイテムをプログラムで削除することです。CheckListBox CheckedItemsを削除する
これはタイマーのコードで、私が何をしようとしているのですか?
private void timer1_Tick(object sender, EventArgs e)
{
string s;
if (DbFirmwareUpdateComplete.WaitOne(1))
{
DbFirmwareUpdateComplete.Reset();
mnuLoadKeyFile.Enabled = true;
}
if (DbUpdateComplete.WaitOne(1))
{
DbUpdateComplete.Reset();
mnuLoadKeyFile.Enabled = true;
btnLoad.Enabled = true;
}
if (CacheComplete.WaitOne(1))
{
CacheComplete.Reset();
btnLoad.Enabled = true;
}
if (UpdateRunning)
{
bool UpdateDone = true;
int StillActive = 0;
// loop through all active jobs to check if all have completed
foreach (clsCnaPair cna in ActiveJobs)
{
if (cna.Job.JobComplete == false)
{
UpdateDone = false;
StillActive++;
}
else
{
if (cna.Job.UpdateSuccess)
{
// Update color of CLB.Items.Selected if success.
int count = CLB.Items.Count;
for (int index = count; index > 0; index--)
{
if(CLB.CheckedItems.Contains(CLB.Items[index-1]))
{
CLB.Items.RemoveAt(index - 1);
}
}
}
else
{
// Update color of CLB.Items.Selected if failed.
}
}
}
if (UpdateDone)
{
UpdateRunning = false;
log("All Update jobs have finished.");
}
if (ckTop.Checked == true)
{
ckTop.Checked = false;
}
else
{
ckTop.Checked = false;
}
私はこのプログラムを実行し、このピースにヒットします。
if (cna.Job.UpdateSuccess)
{
// Update color of CLB.Items.Selected if success.
int count = CLB.Items.Count;
for (int index = count; index > 0; index--)
{
if(CLB.CheckedItems.Contains(CLB.Items[index-1]))
{
CLB.Items.RemoveAt(index - 1);
}
}
}
私はエラーを取得する:
System.ArgumentOutOfRangeException: InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index
エラーコードのこの部分の後に発生します。
ラインでprivate void CLB_SelectedIndexChanged(object sender, EventArgs e)
{
// One of the CNA IPs was selected. sender is the CheckedListBox.
// Here we want to display its fingerprint in the text box, or if the push is running, the status.
// get the CnaPair class represented by this IP:
clsCnaPair CnaPair = (clsCnaPair)CLB.Items[CLB.SelectedIndex];
// Display the corresponding fingerprint string in the editBox:
if (CnaPair.Job != null) txtStatus.Text = CnaPair.Job.GetStatus();
else txtStatus.Text = CnaPair.GetInfo();
}
以上、特に:
clsCnaPair CnaPar = (clsCnaPair)CLB.Items[CLB.SelectedIndex];
私は何をしないのですか? Googleを検索して、私が削除をやっている方法を示して、そこに見つかった例と一致しています。
おかげで、
...エラーはそれ以上のものです。大声でそれを読んで、あなたのコードを問題の原因となった値に置き換えようとします。 'clsCnaPair CnaPair =(clsCnaPair)CLB.Items [-1]'あなたが何を得るかを今調べてみましょう。 –
m.rogalski、同じエラーメッセージが表示されます。 –
@ m.rogalskiがあなたに正しい方向を指していることを確認してください。CLB.SelectedIndex> = 0を確認してください。 – McNets