Link to home
Create AccountLog in
System Programming

System Programming

--

Questions

--

Followers

Top Experts

Avatar of ilyayudkevich
ilyayudkevich

How Set FOCUS on any ITEM in MFC, CListCtrl
About working with MFC, CListCtrl .
From answers I understood how Select/Deselect Item. But I need to set Focus on Selected Item.
If I immitate rounding, I mean: selection on Top Item and User press Up Key. It should Select most down Item and focus on it and scroll if necessary. But after using
      m_List.SetItemState(Item,
            LVIS_FOCUSED, LVIS_FOCUSED);
(Of course, ITEM = SIZE - 1)

It ALWAYS focus on (ITEM - 1) - next up item

Please advise.

Forgot to ask: How to set selection without user clicking? Because no selection at all until user clicked.
Ilya

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of mahesh1402mahesh1402🇮🇳

You may try with LVIS_FOCUSED

Try this :

SetItemState(nItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

-MAHESH

Avatar of mahesh1402mahesh1402🇮🇳

>>How to set selection without user clicking

To select an item you may use:

m_ListCtrl.SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);

To unselect an item you must use:

m_ListCtrl.SetItemState(nItem, 0, LVIS_SELECTED);

Try to use LVIS_SELECTED | LVIS_FOCUSED for selection focus
SetItemState(nItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

-MAHESH

Avatar of AndyAinscowAndyAinscow🇨🇭

You talk about focus and selection and mix the two in the question - you do understand that they are two different things I hope.  (Focus is NOT the same as selection).


>>     m_List.SetItemState(Item,  LVIS_FOCUSED, LVIS_FOCUSED);
>> (Of course, ITEM = SIZE - 1)It ALWAYS focus on (ITEM - 1) - next up item

Have you already allowed for the zero based counting when you calculate SIZE ?  That would give the behaviour you describe.

To 'scroll' to an item you need the EnsureVisible function - m_List.EnsureVisible(item, FALSE);

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of ilyayudkevichilyayudkevich

ASKER

Thank you all very much!
To get what I need all three lines are necessary:
 m_List.SetItemState(Item, LVIS_SELECTED, LVIS_SELECTED | LVIS_FOCUSED);
 m_List.EnsureVisible(Item, FALSE);
 m_List.SetFocus();


ASKER CERTIFIED SOLUTION
Avatar of AndyAinscowAndyAinscow🇨🇭

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of mahesh1402mahesh1402🇮🇳

>>Thank you all very much!
>>To get what I need all three lines are necessary:

ilyayudkevich,
 Are you sure you have accepted right solution ? That should not be split as I have also helped you for getting correct solution ???

-MAHESH



System Programming

System Programming

--

Questions

--

Followers

Top Experts

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.