开发者

Resize a ListView depending on how many items?

开发者 https://www.devze.com 2022-12-24 08:11 出处:网络
How can I resize the height of a ListView depending on how many items are in that ListView? I\'m trying to get the text of an item which is clicked, however whenever the user clicks on a space which h

How can I resize the height of a ListView depending on how many items are in that ListView? I'm trying to get the text of an item which is clicked, however whenever the user clicks on a space which has no item, there's an error. The exact error is:

InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index.

I'm using the code:

label14.Text = m开发者_如何学运维yListView1.SelectedItems[0].Text.ToString();

I figured that removing the space below the items will solve this problem. Thanks!


What you should do to fix your error is check to see whether the user actually clicked on an item:

if (myListView1.SelectedItems.Count > 0) {
    label14.Text = myListView1.SelectedItems[0].Text.ToString();
}


The height of each item is what, around 5-7 pixels depending on the font/font size used. So you can simply do myListView1.Height = myListView1.Items.Count * itemHeight;

To remove empty list items you can iterate through and remove them.


Aha. I've got it working. I used:

if (myListView1.SelectedItems.Count > 0)
        {
            label14.Text = myListView1.SelectedItems[0].Text.ToString();
        }

That seemed to do the trick. Thanks again for the help!

0

精彩评论

暂无评论...
验证码 换一张
取 消