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.