简体   繁体   English

为什么默认情况下我的winform列表视图项不显示?

[英]Why don't my winform listview items show up selected by default?

I have a winforms app and i have listview. 我有一个winforms应用程序,并且有listview。 Through the visual designer, I add a bunch of items and set the "checked" property to true on all of the items. 通过可视化设计器,我添加了一堆项目并将所有项目的“ checked”属性设置为true。 When i start my app none of the items are selected which seems odd. 当我启动我的应用程序时,没有任何项目被选中,这似乎很奇怪。

Even after I tried adding this code: 即使我尝试添加此代码后:

         for (int i = 0; i < listView1.Items.Count; i++)
        {
            listView1.Items[i].Selected = true;
        }

when i startup my app (its a vsto app), none of the items are selected. 当我启动我的应用程序(它是一个vsto应用程序)时,没有选择任何项目。 I am choosing LargeIconView (not sure if that makes a difference) 我正在选择LargeIconView(不确定是否会有所作为)

How can i default a listview to have all items checked by default at startup? 我如何默认一个列表视图来在启动时默认检查所有项目?

The reason your code isn't working is because the ListView control doesn't have focus. 您的代码无法正常工作的原因是因为ListView控件没有焦点。 Two things you could do are to 您可以做的两件事是

1) Set the TabIndex property of the control to be the lowest on the form (likely 0) 1)将控件的TabIndex属性设置为TabIndex上的最低值(可能为0)

2) Select the ListView programmatically 2)以编程方式选择ListView

private void Form1_Load(object sender, EventArgs e)
{
   listView1.Select();
   for (int i = 0; i < listView1.Items.Count; i++)
   {
       listView1.Items[i].Selected = true;
   }
}

The checked property is relevant only if the ListView's property of CheckBoxes is set to True. 仅当CheckBoxes的ListView的属性设置为True时,选中的属性才相关。 Checked is not the same thing as Selected. Checked与Selected是不同的。

Your code to select all the items works for me. 您选择所有项目的代码对我有用。 But perhaps, as keyboardP suggests, your issue is related to Focus. 但是,也许正如keyboardP所建议的那样,您的问题与Focus有关。 Edited : Yes, it only works because I am testing it and this is the only control on my form. 编辑 :是的,它仅工作,因为我正在测试它,这是我窗体上的唯一控件。

"How can i default a listview to have all items checked by default at startup?" “如何默认设置一个列表视图以在启动时默认检查所有项目?”

Change: 更改:

        listView1.Items[i].Selected = true;

To: 至:

        listView1.Items[i].Checked = true;

Not sure why the setting isn't "sticking" if you already set them all to checked thru the IDE. 如果已经将所有设置都设置为通过IDE进行检查,则不确定为什么该设置不会“停留”。 Are you modifying the contents of the ListView when the form is loading? 表单加载时是否要修改ListView的内容?

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

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