此代码有效,但是当我选中然后在第一个复选框中再次取消选中时会显示结果。我只想检查一次。有人有意见吗?谢谢!
private void clb_grup_MouseClick(object sender, MouseEventArgs e)
{
Liste.Items.Clear();
string s = "";
foreach (var item in clb_kisiler.CheckedItems)
{
s = s + item.ToString() + " ";
}
string[] secilenler = s.Split(' ');
if (clb_grup.GetItemChecked(0) == true)
{
for (int x = 0; x < clb_kisiler.Items.Count; x++)
{
clb_kisiler.SetItemChecked(x, true);
}
for (int i = 0; i < clb_kisiler.Items.Count; i++)
{
Liste.Items.Add(clb_kisiler.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(1) == true)
{
for (int x = 0; x < clb_idari.Items.Count; x++)
{
clb_idari.SetItemChecked(x, true);
}
for (int i = 0; i < clb_idari.Items.Count; i++)
{
Liste.Items.Add(clb_idari.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(2) == true)
{
for (int x = 0; x < clb_teknik.Items.Count; x++)
{
clb_teknik.SetItemChecked(x, true);
}
for (int i = 0; i < clb_teknik.Items.Count; i++)
{
Liste.Items.Add(clb_teknik.Items[i].ToString());
}
}
foreach (object i in clb_kisiler.CheckedItems)
{
Liste.Items.Add(i.ToString());
}
}
新代码
我的朋友使用字符串数组而不是组合框。她也遇到了和我一样的问题,她还尝试编写一个代码来防止重复项出现在列表框中。她有一个相同的选中列表框和我一样,她的第二个选中的列表框是空的,还有一个列表框。代码如下。
private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e) { //第一个检查列表框的事件
string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia", "Katherine" };
string[] idari = { "Julie", "Amelia", "Katherine" };
string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };
if (chklstbx_bolum.GetItemChecked(0) == true)
{
//if the first box in the first check list box is checked then do this
//this part of the code works fine but it doesnt check if there are //duplicates of the same name in the list box
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < 5;i++ )
{
chklstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(teknik[i]);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == true){
//do this if the second box in the first check box list is checked
//Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.
chklstbx_sonuc.Items.Clear();
int x=0;
do
{
for (int i = 0; i < 3; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(idari[i]);
}
} while ( x== 0);
x++;
for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
{
for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
{
if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())
lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);
}
}
}
}
所以现在第二个问题是为什么第二个 else if 语句会产生问题并使程序没有响应?而且我们仍然想知道我们如何能够转移字符串数组中的项目而无需检查和取消检查,而是在检查时直接转移?