views:

152

answers:

3

Hello, 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.

A: 

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.

Lucas Jones
What I want is to set the focus on other control when the user is "Focused" on the first item and pulses the "Up Arrow" Key
Jhonny D. Cano -Leftware-
A: 

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.

Austin Hyde
I didn't find a blur event on the ListBox control, where is it?
Jhonny D. Cano -Leftware-
+1  A: 

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

Jhonny D. Cano -Leftware-
+1 Nice workaround
Rashmi Pandit