Board index » cppbuilder » Selected Item Losing Focus in ListView

Selected Item Losing Focus in ListView

Hello, Group:

I have a ListView which uses either a clickable button or a keyboard
shortcut to move items in the list up or down.

When I use the keyboard shortcut, the selected item and the focused item get
separated. When moving up, the focus is one item above the selected item.
When moving down, the focus is one item below the selected item. When using
the buttons, the focus and selection stay on the same item. Of course, by
"focus", I mean the dotted box outline, and by "selection", I mean the
colour-highlighting.

When using the de{*word*81}, I have found that the lines:
ListView->Items->Item[i]->Selected = true;
and
ListView->Items->Item[i]->Focused = true;

are indeed referring to the same item. (The ith item).

The keyboard event handler is an OnKeyDown() with a switch / case statement,
and the clickable buttons are ButtonClick() Events. Both Event Handlers call
the same generic method, either MoveItemsUp() or MoveItemsDown(). Thus, in
theory, they should do the same thing and have the same errors.

This has been driving me nuts. Does anyone know how or why the focus and
selection are being seperated, and what can be done to fix the problem?

Thanks in advance for your help.

Magnus McElroy

 

Re:Selected Item Losing Focus in ListView


Quote
"Magnus McElroy" <mag...@microcommander.com> wrote:
> [...] The keyboard event handler is an OnKeyDown() [...]

To which object is the OnKeyDown event assigned? You might fix it
by setting the Key value to NULL after you have trapped it.

~ JD

Re:Selected Item Losing Focus in ListView


Quote
"JD" <nos...@nospam.com> wrote:
> To which object is the OnKeyDown event assigned?

That was a stupid question. Here's a good one: If you use the
keyboard to cause the misalignment, does it realign if you then
use the OnClick()?

~ JD

Re:Selected Item Losing Focus in ListView


JD:

Yes, it does. Here's what happens. The focused item is noted by F:, and the
Selected Item is noted with S:

1. I start off with this, and click the focused and selected job with the
mouse.

        Job One
F:S:  Job Two
        Job Three
        Job Four
        Job Five

2. I use the Keyboard, pressing CTRL-DOWN, and I get this:

        Job One
        Job Three
S:     Job Two
F:     Job Four
        Job Five

Ideally, I would have Job Two as both Selected and Focused. The selection
never wavers from Job Two.

3. Then, after clicking the down button, I get this:

        Job One
        Job Three
        Job Four
F: S: Job Two
        Job Five

In response to your other question, the OnKeyDown event is:

        //Key pressed = ( CTRL+UP )
        case VK_UP:
        {
            if( Shift.Contains( ssCtrl ) )  //checks if CTRL was held down
                MoveItemsUp();
            break;
        }
        //Key pressed = ( CTRL+DOWN )
        case VK_DOWN:
        {
            if( Shift.Contains( ssCtrl ) )  //checks if CTRL was held down
                MoveItemsDown();
            break;
        }

Thanks for your assistance, JD. I hope you can shed some light on this.

Quote
"JD" <nos...@nospam.com> wrote in message

news:3e39be5b$1@newsgroups.borland.com...
Quote

> "JD" <nos...@nospam.com> wrote:
> > To which object is the OnKeyDown event assigned?

> That was a stupid question. Here's a good one: If you use the
> keyboard to cause the misalignment, does it realign if you then
> use the OnClick()?

> ~ JD

Re:Selected Item Losing Focus in ListView


Quote
"Magnus McElroy" <mag...@microcommander.com> wrote:
>> "JD" <nos...@nospam.com> wrote:
>> If you use the keyboard to cause the misalignment, does it
>> realign if you then use the OnClick()?
> Yes, it does.

I thought that it would. The problem is in the drawing of the
object which is happening before the properties set to their
final values. I'm still learning (that's why I like to hang out
here when I can) so I don't exactly what to tell you.

This might be because the object is also getting the key press
and your getting a 'double' response. I believe that
Ctrl_Up/Down are valid key combinations for the object. In the
OnKeyDown event, try setting the Key value to NULL.

If that doesn't work, check to see if the object has a
BeginUpdate() method. That method disables redrawing until you
use the EndUpdate() method. So ... if it's there, place them
above and below the function call in the OnKeyDown event.

You might also try both possible solutions.

The only other thing that I would know to try is to do all of
the drawing yourself. It's not that difficult but there's no
guarantee that it will fix the problem.

If nothing that I suggested works for you be sure to post a
response so that others who look will know that your problem has
not been solved. TeamB members know infinately more than I.

HTH

~ JD

Other Threads