private void Form1_Load(object sender, EventArgs e)
{
objectListView1.Items.Clear();
objectListView1.Items.Add("1");
objectListView1.Items.Add("2");
objectListView1.Items.Add("3");
objectListView1.Items.Add("4");
objectListView1.Items.Add("5");
}
private void button1_Click(object sender, EventArgs e)
{ Thread sd = new Thread(new ThreadStart(CountItems));
sd.Start();
} List<string> checkedselectedItems = new List<string>(); private void CountItems() {
ISynchronizeInvoke sync = objectListView1;
if (sync.InvokeRequired) {
//how to do the invoke method,get Checked Objects Items ?????
//if user checked some items in objectListView1
//I want to do nothing before objectListView1.SetObjects ,
// if do objectListView1.SetObjects and that objectListView1.CheckedObjects.Count > 0 ,
// //bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
// {
// hasChecked = true;// to Waiting
// }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
/*
我的意思就是在这里,如果要刷新、更新ListView界面的列表值。而界面已有用户选中项目,为了不让刷新、更新界面冲掉选中项(不使选中项失效)
所以让刷新、更新界面在循环中等待,直到用户没有选中
*/
} else { checkedselectedItems = objectListView1.CheckedObjects as List<string>;// objectListView1.SelectedObjects;
}
}
}
ListView么有CheckedObjects 属性啊。没看出来你这个线程开出来是干嘛的。
Lambda表达式或者匿名函数是最好的了.
没用过那个控件,写不了代码了,给个以前写的://跨线程添加listitem
//这里的一些操作生成了一个string[] list.
//习惯用a了- -MethodInvoker a = ()=>{
foreach (var s in list)
{
listbox.Items.Add (s);
}
};if (listbox.InvokeRequired)
listbox.Invoke (a);
else
a();a里的内容可以换成对属性的访问修改等等任何你希望在UI线程里完成的事.
到程序收到数据databuffer,将databuffer分析后变成objectListView1上的一行对象objectItem(它相当于ArrayList),
这时我就要更新objectListView1列表的数据,但是如果用户此时也选定了某些objectItem正准备其他操作,
如果更新objectListView1刷新界面,用户选定的那些objectItems会被冲掉,而没有选中了 所以我就想在
void Updata(IList<objectItems> ){
//bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
//objectListView1.SelectedObjects<----就是这里怎么在线程里取得属性返回值,它相当与ListView.Items属性
// {
// hasChecked = true;// 等待用户操作完成 // }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
//
}
//bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
//objectListView1.SelectedObjects<----就是这里怎么在线程里取得属性返回值,它相当与ListView.Items属性,
//问题在这里,注意我说取值,不是赋值,赋值我知道怎么Invoke或MethodInvoker
// {
// hasChecked = true;// 等待用户操作完成 // }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
//用户操作完成了,用委托更新节目数据
}//再举例说简单点:
//在线程里ArrayList objectItems=ListView.Items,这是我要做的事
//谁做的给谁分,不要告诉我
以下invoke形式我不坚决不给分
ListView.Items=。
ListView.Items.Add(....)