急求一个关于checkedlistbox的问题
private void btnupmove_Click(object sender, EventArgs e)
{
if (checkedListBox1.SelectedIndex > 0)
{
object selstr = checkedListBox1.Items[checkedListBox1.SelectedIndex - 1];
checkedListBox1.Items[checkedListBox1.SelectedIndex - 1] = checkedListBox1.SelectedItem;
checkedListBox1.Items[checkedListBox1.SelectedIndex] = selstr;
checkedListBox1.SelectedItem = checkedListBox1.Items[checkedListBox1.SelectedIndex - 1];
}
}
这段代码可以实现item的向上移动,但是前面的checked事件并没有随着项移动,怎么在移动的时候同时chencked也移动呢,谢谢了~~~~~~~
------解决方案--------------------if (checkedListBox1.SelectedIndex > 0)
{
object selstr = checkedListBox1.Items[checkedListBox1.SelectedIndex - 1];
bool b1 = checkedListBox1.CheckedItems.Cast<object>().Any(x => x == checkedListBox1.Items[checkedListBox1.SelectedIndex - 1]);
bool b2 = checkedListBox1.CheckedItems.Cast<object>().Any(x => x == checkedListBox1.Items[checkedListBox1.SelectedIndex]);
checkedListBox1.Items[checkedListBox1.SelectedIndex - 1] = checkedListBox1.SelectedItem;
checkedListBox1.Items[checkedListBox1.SelectedIndex] = selstr;
checkedListBox1.SelectedItem = checkedListBox1.Items[checkedListBox1.SelectedIndex - 1];
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, b2);
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex + 1, b1);
}
------解决方案--------------------int index = 0;
var query = (from x
in checkedListBox1.Items.Cast<object>()
let i = ++index
where checkedListBox1.CheckedItems.Cast<object>().Any(y => y == x)
select i).ToList();
for (int i = query.Min(); i < query.Max(); i++)
checkedListBox1.SetItemChecked(i, true);