?
Solved

CListCtrl SetFocus

Posted on 2011-10-08
11
Medium Priority
?
1,833 Views
Last Modified: 2013-12-03
Hello,

I try to set the focus to my list (CListCTrl) after filling it, but it do not get the main focus on the dialog. (SetFocus). It seems the SetFocus do not work. Is there any trick to set the focus to a CListCtrl?

Ingo
0
Comment
Question by:Ocrana
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36938995
SetFocus should work - assuming the list control is enabled (disabled controls never get the focus).

Please show your code.
How do you test it and know that the ListCtrl doesn't have the focus ?
0
 

Author Comment

by:Ocrana
ID: 36939039
It seems the problem is because I calll this from an worker thread. The main dialog total ignroe the commands. If I add a button onto the dialog and send the focus messages from there it works well.
iam  screwed

Ingo
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 36939298
It does not matter that you try to set the focus from a thread.  That will not make a difference providing that you follow a simple rule: you can not pass MFC objects between threads and expect member functions to work.

So, if you pass your CListCtrl by pointer or reference to your thread function, then call any member functions on it, it will not work.  This is because, put simply, the underlying HWND of the list control is not in that thread's "map of known window objects".  The easy way to get around this is to pass the HWND of the CListCtrl to the thread instead (obtained via CListCtrl::GetSafeHWND() or similar), then call ::PostMessage (hWnd, WM_SETFOCUS) where hWnd is the HWND obtained.

HTH
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36940966
I agree with mrwad99 - your problem should have nothing to do with threads.  (You must NOT pass MFC objects across threads however I disagree slightly with one part - it will not work - typically it is a crash, not just the code doesn't work.)


SetFocus should work - assuming the list control is enabled (disabled controls never get the focus).

    Please show your code.


    How do you test it and know that the ListCtrl doesn't have the focus ?
0
 

Author Comment

by:Ocrana
ID: 36941037
Sorry, I do not agree. It sounds all to blank theory. Fact is, I uncouple the thread/message with a PostMessage function to the main HWND but it do not work.
A SetFocus() work from button click but not from message function received from thread.

Ingo
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36941325
According to you your current code does not work.
I've asked twice for you to show it - you haven't.

I'm sorry but my crystal ball is requiring an upgrade - I can't see your code in it.  Without that I can't help.

or, is this more useful ?  Check line 473, I think your variable ought to be initialised with a call to fooble().  (GIGO).
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 36941745
i recommend against sending a WM_SETFOCUS from a thread to hwnd of the main ui thread. instead the thread should send a user message to the main dialog which could be handled in the dialog's message map. the the dialog could m_listCtrl.SetFocus() what surely works.

i think the reason why the PostMessage doesn't work is that you would need the hwnd of the list control while you told you took the 'main HWND' what probably is the hwnd of the dialog or mainframe.

but a worker thread never should try to directly control a dialog. that is not the business of a thread.

Sara

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36941948
>>If I add a button onto the dialog and send the focus messages from there it works well.
later comment
>>A SetFocus() work from button click but not from message function received from thread.

If you aren't going to show us the code you use at least be consistent with telling us what you are doing.  Then we can guess a little better.


ps.  sara - a WM_SETFOCUS is sent after the window receives the focus according to my help files.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 36942555
according to my docs the SetFocus sends a WM_KILLFOCUS to the window which currently has the focus and a WM_SETFOCUS to the window gaining the focus. that means, sending (only) the (notification) messages from thread by PostMessage would not cause any change on focus.  

in the docs there also is a warning:
Do not use the SetFocus function to set the keyboard focus to a window associated with another thread's message queue

by the way, also the filling of the list control shouldn't be done by the thread. instead the data to fill should be provided in a shared container (normally created on the heap by new) and a pointer to that container passed with the user message from worker thread to main thread. then, the main thread can handle the message and do the filling from container and delete the container when done.

Sara
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36942676
@ sara
http://msdn.microsoft.com/en-us/library/ms646283%28VS.85%29.aspx

Anyway - without the real code that is in use anything from us is just pie in the sky guesswork as to what is going wrong and how to cure it.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 36946861
Andy, i don't disagree to the docs you mentioned (they are the same i was using :) )

the SetFocus is a winapi function which is not message based, that means it cannot be replaced by a SendMessage or PostMessage (what i meant mistakenly in my first post). so, the SetFocus changes the active window (sets the focus to the window identified by hwnd) and then sends the notification messages.

Sara
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question