If you really want to do such change, the best way is creating a UserControl
that hosts a ListView
and implement such properties and methods for your UserControl
.
For example:
//Just for sample, you should add some checks and raise some events and throw some exceptions.
public int SelectedIndex
{
get
{
var selectedIndex = -1;
if (this.listView1.SelectedItems.Count == 1)
selectedIndex = this.listView1.SelectedItems[0].Index;
return selectedIndex;
}
set
{
this.listView1.SelectedItems.Cast<ListViewItem>()
.ToList().ForEach(item =>
{
item.Selected = false;
});
this.listView1.Items[value].Selected = true;
}
}
Implementing user control is for being independent from ListView
and not to expose ListView
members. But if it is not a concern for you, you can inherit from ListView
and add properties and methods that you want to it, for example:
public int IndexFromPoint(Point p)
{
return this.IndexFromPoint(p.X, p.Y);
}
public int IndexFromPoint(int x, int y)
{
var item = this.HitTest(x, y).Item;
if (item != null)
return item.Index;
return -1;
}
private string displayMember;
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember
{
get
{
return displayMember;
}
set
{
displayMember = value;
}
}
private string valueMember;
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string ValueMember
{
get
{
return valueMember;
}
set
{
valueMember = value;
}
}
Edit
As another alternative that sounds good; you can create a WPF UserControl
that hosts a WPF ListBox
that seems doesn't suffer from that scroll problem. This way you can host your usercontrol in windows forms using an ElementHost
control.
As I tested when you add new items to it, the scroll remains where it is now.