简体   繁体   English

没有在C#中以编程方式选择listview项

[英]listview item is not selected programmatically in C#

I have seen all the Questions about this issue, but solutions seem to not apply in my case. 我已经看到了有关此问题的所有问题,但是解决方案似乎不适用于我的情况。

i am trying to select an item in a listview and i have the following: 我试图在列表视图中选择一个项目,我有以下内容:

ListView Matlab_Hst_Props;
...
//Matlab_Hst_Props.HideSelection == false;
//Matlab_Hst_Props.Items.Count == 4;

Matlab_Hst_Props.Invoke(new Action(() => Matlab_Hst_Props.Focus()));
Matlab_Hst_Props.Invoke(new Action(() => Matlab_Hst_Props.Select()));
Matlab_Hst_Props.Invoke(new Action(() => Matlab_Hst_Props.Items[0].Selected = true));

//Matlab_Hst_Props.Items[0].Selected == true

Matlab_Hst_Props.Invoke(new Action(() => Matlab_Hst_Props.Items[0].Focused = true));

//Matlab_Hst_Props.Items[0].Focused == false;!!!
//Matlab_Hst_Props.SelectedItems.Count == 0; !!!

Yes, i do this from a thread, but even if i dont use the thread and remove the invoke , i still have the same behaviour. 是的,我从线程执行此操作,但是即使我不使用线程并删除invoke ,我仍然具有相同的行为。

please help. 请帮忙。 What could i be missing? 我可能会缺少什么?


I would like to add here an update. 我想在这里添加更新。 In fact my GUI is made up of tabs. 实际上,我的GUI由选项卡组成。 This ListView is in the third Tab. 该ListView在第三个选项卡中。 I get the error if i DON'T click on the third Tab and the ListView is Not drawn. 如果我不单击第三个选项卡并且未绘制ListView,则会收到错误消息。 iF i select the tab, and the ListView is Drawn before This piece of code is run, then the selection is done correctly. 如果选择了该选项卡,并且在运行这段代码之前绘制了ListView,则选择正确。 What could it change drawing or not the Listview? 它可以改变图形或不改变Listview? and how can i somehow force such an operation? 我如何以某种方式强制执行此操作?

I am posting here the workaround i use. 我在这里发布我使用的解决方法。 I know this is just a silly workaround, but i still did not found out the true reason behind this behaviour. 我知道这只是一个愚蠢的解决方法,但是我仍然没有找出这种行为背后的真正原因。

In initialization process of the GUI, i change the tab programmatically to the Tab where the Listview resides: 在GUI的初始化过程中,我以编程方式将选项卡更改为Listview所在的选项卡:

Top_Tabs.Invoke(new Action(() => Top_Tabs.SelectedIndex = 2));

and then as initialization ends, i put it back on the first Tab 然后在初始化结束时,我将其放回第一个Tab

Top_Tabs.Invoke(new Action(() => Top_Tabs.SelectedIndex = 0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM