我有一个选中的列表框,根据所选项目,我想将一个字符串数组转移到第二个选中的列表框,也转移到一个列表框。所以基本上我可以说我选择了#34; Tumu"从第一个选中的列表框中,我希望将字符串tumu [i]写入第二个选中的列表框和列表框中。同样适用于" Teknik"使用teknik [i]和Idari使用idari [i]数组。问题是当我将字符串分配给相应的检查列表框时,运行程序时会显示teknik [i]当我点击" Tumu"但是我想要显示tumu [i]数组。在我取消选中并检查后,结果会变成另一个字符串数组......基本上程序正在运行。我也希望结果在当用户从第一个选中的列表框中取消选中项目时,要删除的列表框和要在第二个列表框中取消选中的复选框。如果有人对问题可能是什么有任何想法,将非常感激:)我有附上了一张照片和代码。
private void chklstbx_bolum_ItemCheck(object sender,ItemCheckEventArgs e) {//第一个复选框列表
string[] tumu = { "Jane", "Tom", "Danny", "John", "Jacyln", "Lily", "Lale" };
string[] idari = { "Jane", "Tom", "Danny" };
string[] teknik = { "John", "Jacyln", "Lily", "Lale"};
if (chklstbx_bolum.GetItemChecked(0) == false)
{ //if the first box is checked then do this
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < teknik.Length; i++)
{
chklstbx_sonuc.Items.Add(teknik[i]);
lstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
}
}
}
if (chklstbx_bolum.GetItemChecked(0) == true)
{//when the first item is unchecked then items in the list box will be cleared and the items in the second list box will be all unchecked.
lstbx_sonuc.Items.Clear();
for (int i = 0; i < chklstbx_sonuc.Items.Count; i++)
{
chklstbx_sonuc.SetItemChecked(i, false);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == false)
{
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < idari.Length; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
lstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
}
}
if (chklstbx_bolum.GetItemChecked(1) == true)
{//when the second item in the first checked list box is unchecked then items in the list box will be cleared and the items in the second list box will be all unchecked.
lstbx_sonuc.Items.Clear();
for (int i = 0; i < chklstbx_sonuc.Items.Count; i++)
{
chklstbx_sonuc.SetItemChecked(i, false);
}
}
else if (chklstbx_bolum.GetItemChecked(2) == false)
{
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < tumu.Length; i++)
{
chklstbx_sonuc.Items.Add(tumu[i]);
lstbx_sonuc.Items.Add(tumu[i]);
chklstbx_sonuc.SetItemChecked(i, true);
}
}
if (chklstbx_bolum.GetItemChecked(2) == true)
{ //when the third item in the first checked list box is unchecked then items in the list box will be cleared and the items in the second list box will be all unchecked.
lstbx_sonuc.Items.Clear();
for (int i = 0; i < chklstbx_sonuc.Items.Count; i++)
{
chklstbx_sonuc.SetItemChecked(i, false);
}
}
}
答案 0 :(得分:0)
检查这是否有帮助。使用字典以避免重复类似的代码。
public partial class FormCheckList : Form
{
Dictionary<string, string[]> myDictionary;
public FormCheckList()
{
InitializeComponent();
InitializeArrays();
}
private void InitializeArrays()
{
myDictionary = new Dictionary<string, string[]>();
myDictionary.Add("Tumu", new string[] { "Jane", "Tom", "Danny", "John", "Jacyln", "Lily", "Lale" });
myDictionary.Add("Idari", new string[] { "Jane", "Tom", "Danny" });
myDictionary.Add("Teknik", new string[] { "John", "Jacyln", "Lily", "Lale" });
}
private void checkedListBoxLevelOne_ItemCheck(object sender, ItemCheckEventArgs e)
{
string selectedItem = checkedListBoxLevelOne.SelectedItem.ToString();
string[] items = myDictionary[selectedItem];
checkedListBoxLevelTwo.Items.Clear();
listBox.Items.Clear();
for (int i = 0; i < items.Length; i++)
{
checkedListBoxLevelTwo.Items.Add(items[i]);
checkedListBoxLevelTwo.SetItemChecked(i, true);
listBox.Items.Add(items[i]);
}
}
}