Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

ListBox Focused Item

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

Answers (3)

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

This is kinda hacky, but i haven't found a better solution.

  1. Put ListBox.DrawMode on OwnerDrawFixed
  2. 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();
    
  3. Use the myFocus variable

Upvotes: 1

Austin Hyde
Austin Hyde

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

Lucas Jones
Lucas Jones

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

Related Questions