Reputation: 18013
this is a WinForms question.
In a ListBox with SelectionMode = MultiSimple, how can I get the currently focused item?
Note, I don't want to get the SelectedItem or SelectedItems, but the item which currently have the dash lines around, something like ListView.FocusedItem.
Upvotes: 0
Views: 1133
Reputation: 18013
This is kinda hacky, but i haven't found a better solution.
Capture the DrawItem Event and save the focus index on a field
if (e.State == DrawItemState.Focus) {
myfocus = e.Index;
}
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
if (brochas.Count != colores.Count) {
ProcesarBrochas();
}
// Draw the current item text based on the current Font
// and the custom brush settings.
if (Items.Count > e.Index) {
e.Graphics.DrawString(Items[e.Index].ToString(),
e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
}
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
Use the myFocus variable
Upvotes: 1
Reputation: 27436
This isn't the perfect solution, but a workaround might be to store the selectedItem
into a "focusedItem
" when the blur event fires, then simply retrieve it when you need to.
Upvotes: 0
Reputation: 20193
I don't think there's one in there by default - a user control may be your only option here.
You may want to rethink what you're doing - why do you need the focussed ones instead of the selected ones? There may be a different way of doing it.
Upvotes: 0