Thread: Problem with changing input focus

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403

    Problem with changing input focus

    I'm writing a program that involves 3 edit controls, and I want to be able to use tab to flit between them.

    I've managed to do that, but for some weird reason when tab is pressed while the cursor is in the 3rd edit control, before it switches focus back to the first edit box it does a regular tab stop, ie the cursor jumps forward 5 spaces or so.

    So if you repeatedly press tab you notice that when the cursor appears in the third edit box each time it is 1 tab forward from it's previous position.

    Hmm.. that was quite poorly explained, but i hope you get the gist. How do I stop it doing that?

    I'm not really sure how this whole thing is working, i've basically stolen the code from one of the program's in Petzold's book.

    As far as I can see, I tell the program to pass all of its messages first to ScrollProc (LRESULT CALLBACK ScrollProc (HWND hwnd,.... blah blah)

    ScrollProc looks like this:

    Code:
    LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int id = GetWindowLong (hwnd, GWL_ID);
    
    	switch (message)
    	{
    	case WM_KEYDOWN:
    		if (wParam == VK_TAB)
    			SetFocus (GetDlgItem (GetParent (hwnd),
    			(id + (GetKeyState (VK_SHIFT) < 0 ? 2: 1)) % 3));
    		break;
    
    	case WM_SETFOCUS:
    		idFocus = id;
    		break;
    	}
    	return CallWindowProc (Focus[id], hwnd, message, wParam, lParam);
    }
    I "call" ScrollProc in WndProc, WM_CREATE, after i've used CreateWindow to make the edit boxes:

    Code:
    temp1 = CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           NULL,                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,         //control style: how it looks
                           160,                                    //control position: left
                           10,                                    //control position: top
                           570,                                   //control width
                           25,                                    //control height
                           hWnd,                                  //parent window handle
                           (HMENU) 0,                                  //control's ID
                           hInst,                                 //application instance
                           NULL);   
    
    			   Focus[0] = (WNDPROC) SetWindowLong (temp1, GWL_WNDPROC, (LONG) ScrollProc);
                           
                   temp2 = CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           NULL,                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,         //control style: how it looks
                           160,                                    //control position: left
                           50,                                    //control position: top
                           570,                                   //control width
                           25,                                    //control height
                           hWnd,                                  //parent window handle
                           (HMENU) 1,                                  //control's ID
                           hInst,                                 //application instance
                           NULL);   
    
    			   Focus[1] = (WNDPROC) SetWindowLong (temp2, GWL_WNDPROC, (LONG) ScrollProc);
                           
                   temp3 = CreateWindowEx(WS_EX_CLIENTEDGE,                      //more or 'extended' styles
                           TEXT("EDIT"),                          //'class' of control to create
                           NULL,                  //the control caption
                           WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_BORDER|ES_LEFT|ES_MULTILINE, 	//|ES_NOHIDESEL|ES_AUTOHSCROLL|ES_AUTOVSCROLL        //control style: how it looks
                           160,                                    //control position: left
                           100,                                    //control position: top
                           570,                                   //control width
                           370,                                    //control height
                           hWnd,                                  //parent window handle
                           (HMENU) 2,                                  //control's ID
                           hInst,                                 //application instance
                           NULL);     
                                                 
    			   Focus[2] = (WNDPROC) SetWindowLong (temp3, GWL_WNDPROC, (LONG) ScrollProc);
    and I have the following under WndProc, WM_SETFOCUS:
    Code:
    case WM_SETFOCUS:
    			if (idFocus == 0)
    				SetFocus (temp1);
    			else if (idFocus == 1)
    				SetFocus (temp2);
    			else if (idFocus == 2)
    				SetFocus (temp3);
    Now my understanding of the above code is that when tab or shift tab is pressed it changes the input focus, if any other message comes in that message is passed to WndProc, so surely when I press tab in the 3rd edit box it should NOT act like a normal tab at all..... but it does. And I don't understand why it happens for the 3rd one but not the other two.

    *confused*

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Just a thought, but if the tab is hit while in the first box, shouldn't you set focus to the second box. So:
    Code:
    case WM_SETFOCUS:
    			if (idFocus == 0)
    				SetFocus (temp2);
    			else if (idFocus == 1)
    				SetFocus (temp3);
    			else if (idFocus == 2)
    				SetFocus (temp1);
    This seems to make since if there are 3 edit box handles (temp1,temp2,temp3) and idFocus has the one that is losing the focus. Hope this helps.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    strangely that made no difference at all, *still confused*

    Thanks for your input tho

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  2. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 03:55 AM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  4. losing focus problem
    By hiya in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2005, 11:02 AM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM