Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to automatically resize a listView control in SmallIcon view, so that all items are visible
 

How to automatically resize a listView control in SmallIcon view, so that all items are visible

Is there an easy way to automatically resize a listView control, so that as more items are added to it, it's size grows up to an allowed max size?

I have a listview control in a form. I am using view=SmallIcon. I am using this listView to display the files selected by the user Similar to the control used by outlook when you add attachments, I want my listview control to automatically resize as I add more items in it. This should happen up to a certain height, and only after that a vertical scroll bar should be added.

I am thinking about getting the Bounds property of the last item in the listView, and calculate the height of the listView box based on that, but before I get into that I was wondering if anybody can recommend asimpler way to do this.

Thanks.
Cuneyt
Cuneyt Havlioglu - MSFT  Wednesday, April 15, 2009 10:29 PM
Hi Cuneyt Havlioglu,

Based on my experience, ListView doens't expose some event, such as itemAdded event for you to resize the ListView control. So I think the simplest way is to calculate the current height of all the items, and compare it with the height of the ListView. Then decide whether we need to resize the ListView control.

As you may already think about using the Bounds property of the ListViewItem, we can simple calculate the height needed to make all items visible by adding the single item height to the Bounds.Y of the last ListViewItem.

I am also writing some testing code for you to refer:

        public Form1()
        {
            InitializeComponent();
            this.listView1.View = View.SmallIcon;
            this.listView1.SmallImageList = this.imageList1;
            this.listView1.Items.Add("file1", 0);
            this.listView1.Items.Add("file2ddddddddddddd", 0);
            this.listView1.Items.Add("file3", 0);
            this.listView1.Items.Add("file4", 0);
            this.listView1.Scrollable = false;
        }
        /// <summary>
        /// Add new items.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem("file3333333333333", 0);
            // measure the height of the single item
            int itemHeight = this.listView1.Items[0].Bounds.Height;

            //add item to list view
            this.listView1.Items.Add(lvi);

            // if the maximumize height of the ListView is 100
            if (this.listView1.Height <= 100)
            {
                int count = this.listView1.Items.Count - 1;
                if (this.listView1.Items[count].Bounds.Y + itemHeight >= this.listView1.Height)
                {
                    this.listView1.Height += itemHeight;
                }
            }
            else
            {
                //make the listview scrollable again.
                this.listView1.Scrollable = true;
            }
        }
You may find we can achieve the goal with several lines of code.

If you have further problem, please feel free to contact me.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, April 17, 2009 3:34 AM
Hi Cuneyt Havlioglu,

Based on my experience, ListView doens't expose some event, such as itemAdded event for you to resize the ListView control. So I think the simplest way is to calculate the current height of all the items, and compare it with the height of the ListView. Then decide whether we need to resize the ListView control.

As you may already think about using the Bounds property of the ListViewItem, we can simple calculate the height needed to make all items visible by adding the single item height to the Bounds.Y of the last ListViewItem.

I am also writing some testing code for you to refer:

        public Form1()
        {
            InitializeComponent();
            this.listView1.View = View.SmallIcon;
            this.listView1.SmallImageList = this.imageList1;
            this.listView1.Items.Add("file1", 0);
            this.listView1.Items.Add("file2ddddddddddddd", 0);
            this.listView1.Items.Add("file3", 0);
            this.listView1.Items.Add("file4", 0);
            this.listView1.Scrollable = false;
        }
        /// <summary>
        /// Add new items.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem("file3333333333333", 0);
            // measure the height of the single item
            int itemHeight = this.listView1.Items[0].Bounds.Height;

            //add item to list view
            this.listView1.Items.Add(lvi);

            // if the maximumize height of the ListView is 100
            if (this.listView1.Height <= 100)
            {
                int count = this.listView1.Items.Count - 1;
                if (this.listView1.Items[count].Bounds.Y + itemHeight >= this.listView1.Height)
                {
                    this.listView1.Height += itemHeight;
                }
            }
            else
            {
                //make the listview scrollable again.
                this.listView1.Scrollable = true;
            }
        }
You may find we can achieve the goal with several lines of code.

If you have further problem, please feel free to contact me.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, April 17, 2009 3:34 AM

You can use google to search for other answers

Custom Search

More Threads

• Sort DataGrid View on the basis of CheckBox Column
• How do you disable the Up/Down arrow in PropertyGrid collection editor
• Resx files not available in designer when in a project subfolder
• Cell Size in a DataGrid.
• Does .NET has this component built in ?
• Splash Screen Controls becoming transparent at load time.
• Deserialization error caused by Location extended property added by ComponentTray
• Controls magically(?) change their size when opening the designer
• TypeDescriptor.GetProperties and extenderproviders
• Cannot set Anchor property in Designer