Windows - Set ListBox focus ring in code?

I’m in the process of coding a ListBox with standard Windows behaviour (in terms of mouse and keyboard interaction) as the one included is far from it, and its bugging me.

If I call ListBox.Selected(5) I get the result below.

I have also tried setting ListBox.ListIndex = 5 with no change.

Any ideas?

Are you subclassing listbox or are you creating your own from scratch?

Subclassing at the moment, just testing things.

If its not possible, I might build my own.

Is it multiple selection? Try deselecting the current row and selecting another (like a SegmentedControl)
(ListBox.Selected(0) = false ListBox.Selected(5) = true)

Well eventually I want it as a multi (so the focus should be on the last interaction), but its single at the moment, and I’m just selecting that row in code, there is no selection when the program starts so I cant deselect it.

I guess its not possible by the lack of responses?

Just a quick bump before I go to feedback a feature request.


LB.UseFocusRing = False

[quote=307679:@Axel Schneider]try

LB.UseFocusRing = False

Thanks Axel, I’ve tried that already, it just turns the ring off. If I then turn it back on again, its in the same position as the picture above :frowning:

So you want this Focus Ring?
As a cell focus ring for the selected row?

I want the focus ring, but I want to be able to specify the cell/row it is on. When I select a row in code, I want it to move the focus ring to that entry, so for example, if someone then presses F2 to edit, it will edit the correct entry, and if they press DOWN_ARROW it will move onto entry 5 in the above example, and not one entry 1.

This is usually done in other languages by setting some kind of index on the control, however ListBox.ListIndex = 5 doesnt move the focus ring.

There is no cell focus ring that I am aware of… the only time I’ve seen a cell focus ring is when you were in EDIT MODE on that cell
as such you need to draw it yourself in the cellbackgroundpaint event

look at this ->

Its there on the first picture :slight_smile: The dotties around entry 0, its standard Windows behaviour for keyboard entry. Every control should have it or something similar to show the user that the control has keyboard focus.

Thanks :slight_smile:

I know I can paint my own, but why reinvent the wheel if its already there. I guess from the lack of an obvious solution that its not currently a feature, when I have exhausted all other avenues I will re-evaluate my approach :slight_smile:

The ListBox is not a native Windows control. So it could be that it does not behave exactly like other Win32 controls.

I was able to reproduce what you show, but I think that one is a bug. As soon as you move the selection with the keyboard, the selection box is clearly visible around the blue selection bar.

So unless I am mistaken, it would seem that the selection bar is de facto indicating where the keyboard focus is, unless you get it to manifest by setting the selection in code.

Note that also, the selection bar indicates a row, and not a cell. I tried with several columns.

I am inclined to believe you must create your own system that behaves according to your specs.