Thread: WM_SETFOCUS Problem

  1. #1
    Registered User
    Join Date
    Aug 2006

    WM_SETFOCUS Problem

    Hey I'm having a problem wondering if any of you guys know why.

    I have an edit control where I validate it for proper text length but need it to set focus if the text entered is not valid.

    I used the code below:
                                        case FRAME_START + 47: // TABLE NAME
                                             v = GetWindowTextLength((HWND)lParam);
                                             if(v < 8 || v > 20){
                                                  MessageBox(0, "Table name must be 8 to 20 characters long.", "Invalid Table Name", 0);
                                                  SetWindowText((HWND)lParam, "");
                                                  SendMessage((HWND)lParam, WM_SETFOCUS, 0, 0);
    But whenever I use it, it will set focus to the text box but then the textbox cannot be typed in any longer. Really weird problem.

    I sending the message EM_SETREADONLY w/ a false wParam but that didn't seem to be the problem. What's weird is I can right click in the text box and copy/cut/paste/delete text from it but can't add text by typing it after sending the WM_SETFOCUS message.

    I have the textbox contained as a child within a subclassed static if that makes any difference. The case statement provided above is just a portion of the procedure I have for the subclassed container.

    Edit: Actually what's really strange is I tried using the WM_SETFOCUS message on another edit control and it acts really strange. I'll click on that edit control click off and click on an entirely different control and type something then click off of it and if I click back onto the original edit control again it will set focus to the previous one before it. It's kindof hard to explain unless you saw the results for yourself.

    Help is greatly appreciated. Thanks.
    Last edited by pdesigns; 08-04-2006 at 03:23 PM.

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    I believe the problem is in using a notification as a message:
    The WM_SETFOCUS message is sent to a window after it has gained the keyboard focus.
    WM_SETFOCUS is a notification that is sent by the control *after* it has gained focus, and does not necessarily *cause* it to gain focus if sent explicitily.
    To set keyboard focus, use the function SetFocus(hwnd) instead.

  3. #3
    Registered User
    Join Date
    Aug 2006

    Thumbs up

    @nthony, thanks for your help.

    Using the SetFocus function works exactly as I was hoping for. I've been having problems like this every now and then with different things -- I'll sometimes try to apply concepts to things that don't apply and get myself confused xD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM