I think I've experienced an epiphany. In case anybody else is interested, here's how:
I will have to override the default property "SelectedIndex" using the SendMessage() function of the olden days of VB 6.0:
public class ListBoxEx : ListBox
{
[DllImport("USER32.DLL")]
private static extern int SendMessage(IntPtr hWnd, uint message, int wParam, string lParam);
[DllImport("USER32.DLL")]
private static extern int SendMessage(IntPtr hWnd, uint message, int wParam, int lParam);
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint message, int wParam, StringBuilder lParam);
private const int LB_FINDSTRING = 0x018F;
private const int LB_FINDSTRINGEXACT = 0x1A2;
private const int LB_SETCURSEL = 0x0186;
private const int LB_GETCURSEL = 0x0188;
private const int LB_GETTEXT = 0x0189;
...
new public int FindString(string s)
{
return SendMessage(this.Handle, LB_FINDSTRING, -1, s);
}
new public int FindStringExact(string s)
{
return SendMessage(this.Handle, LB_FINDSTRINGEXACT, -1, s);
}
new public int SelectedIndex
{
get
{
return SendMessage(this.Handle, LB_GETCURSEL, 0, 0);
}
set
{
SendMessage(this.Handle, LB_SETCURSEL, value, 0);
}
}
new public object SelectedItem
{
get
{
StringBuilder s = new StringBuilder ( 256 );
SendMessage(this.Handle, LB_GETTEXT, this.SelectedIndex, s);
return s;
}
set
{
this.SelectedIndex = this.FindStringExact(value.ToString());
}
}
}
Yes, the code is kludgy, esp. considering that you'd also have to define the SelectedValue, SelectedIndices, etc. properties, but those I don't feel like tackling those yet. It will just have to do for now, unless someone here presents us with a better, simpler solution.
Ahh, I feel like Archimedes =P