Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetFocus problem

SetFocus problem

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I have an SDI application, with a splitter window that have in left view an CTreeView and right side an CListView.In each one I have some items. I override PreTranslateMessage in each one to be able on tab key to move focus from tree to view and viceversa :

    BOOL CMyTree::PreTranslateMessage(MSG* pMsg)
    {
    // TODO: Add your specialized code here and/or call the base class

    if(pMsg->message == WM\_KEYDOWN && pMsg->wParam == VK\_TAB)
    {
    	CMyListView\* pView = (CMyListView\*)pChild->GetMyListView();
    			pView->SetFocus();
    			pView->GetListCtrl().SetItemState(0, LVIS\_SELECTED | LVIS\_FOCUSED, LVIS\_SELECTED | LVIS\_FOCUSED);
    	}
    
    	return TRUE;
    }
    
    return CTreeView::PreTranslateMessage(pMsg);
    

    }

    I see that the focus has moved. Somewhere in menu I have a delete menu that could delete items from tree or from listview. Let's say that I'm in treeview. I press tab and I see that the focus has moved on listview. When I try to delete an item I see that I delete an item from treeview not from list view .... so, from some reason, the focus remained in treeview ... The the deletion goes well only when I move the focus from tree on list view by click with mouse in list view ... My question is, is enough to call SetFocus(); to move an focus from tree view to listview ?

    C J 2 Replies Last reply
    0
    • _ _Flaviu

      I have an SDI application, with a splitter window that have in left view an CTreeView and right side an CListView.In each one I have some items. I override PreTranslateMessage in each one to be able on tab key to move focus from tree to view and viceversa :

      BOOL CMyTree::PreTranslateMessage(MSG* pMsg)
      {
      // TODO: Add your specialized code here and/or call the base class

      if(pMsg->message == WM\_KEYDOWN && pMsg->wParam == VK\_TAB)
      {
      	CMyListView\* pView = (CMyListView\*)pChild->GetMyListView();
      			pView->SetFocus();
      			pView->GetListCtrl().SetItemState(0, LVIS\_SELECTED | LVIS\_FOCUSED, LVIS\_SELECTED | LVIS\_FOCUSED);
      	}
      
      	return TRUE;
      }
      
      return CTreeView::PreTranslateMessage(pMsg);
      

      }

      I see that the focus has moved. Somewhere in menu I have a delete menu that could delete items from tree or from listview. Let's say that I'm in treeview. I press tab and I see that the focus has moved on listview. When I try to delete an item I see that I delete an item from treeview not from list view .... so, from some reason, the focus remained in treeview ... The the deletion goes well only when I move the focus from tree on list view by click with mouse in list view ... My question is, is enough to call SetFocus(); to move an focus from tree view to listview ?

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      How does your command handler for the delete operation determine whether the treeview or the listview has focus? You haven't provided that code. As a user, my expectation would be that the delete operation should be deleting the item in the control that has focus. Though I guess a case could be made for deleting anything that is selected regardless of focus. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        I have an SDI application, with a splitter window that have in left view an CTreeView and right side an CListView.In each one I have some items. I override PreTranslateMessage in each one to be able on tab key to move focus from tree to view and viceversa :

        BOOL CMyTree::PreTranslateMessage(MSG* pMsg)
        {
        // TODO: Add your specialized code here and/or call the base class

        if(pMsg->message == WM\_KEYDOWN && pMsg->wParam == VK\_TAB)
        {
        	CMyListView\* pView = (CMyListView\*)pChild->GetMyListView();
        			pView->SetFocus();
        			pView->GetListCtrl().SetItemState(0, LVIS\_SELECTED | LVIS\_FOCUSED, LVIS\_SELECTED | LVIS\_FOCUSED);
        	}
        
        	return TRUE;
        }
        
        return CTreeView::PreTranslateMessage(pMsg);
        

        }

        I see that the focus has moved. Somewhere in menu I have a delete menu that could delete items from tree or from listview. Let's say that I'm in treeview. I press tab and I see that the focus has moved on listview. When I try to delete an item I see that I delete an item from treeview not from list view .... so, from some reason, the focus remained in treeview ... The the deletion goes well only when I move the focus from tree on list view by click with mouse in list view ... My question is, is enough to call SetFocus(); to move an focus from tree view to listview ?

        J Offline
        J Offline
        Jonathan Davies
        wrote on last edited by
        #3

        Looking at (ATL) code of mine for handling tabs, and which uses GetDlgItem rather than SetFocus, the only difference I can see is that I have a call:

        // Now select all like normal tab behaviour
        SendMessage(hNew, EM_SETSEL, 0, -1);

        at the end. Presumably as my tabs have edit controls on and the cursor needs setting up. So unless there's a parallel with your views e.g. ensure, say, a node or list item has focus or is other wise set up, I'd agree with Chris and wonder about the code doing the deleting.

        1 Reply Last reply
        0
        • C Chris Meech

          How does your command handler for the delete operation determine whether the treeview or the listview has focus? You haven't provided that code. As a user, my expectation would be that the delete operation should be deleting the item in the control that has focus. Though I guess a case could be made for deleting anything that is selected regardless of focus. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Of course that program delete the item of window that have the focus ... the problem is : I am in the listview, I select 2 items, and when I go to main menu, click on delete item from menu, program wants to delete an tree view item, not list view items ... I try to make a little demo, to see what is happend.

          C 1 Reply Last reply
          0
          • _ _Flaviu

            Of course that program delete the item of window that have the focus ... the problem is : I am in the listview, I select 2 items, and when I go to main menu, click on delete item from menu, program wants to delete an tree view item, not list view items ... I try to make a little demo, to see what is happend.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            If we could see your 'delete' code, that could help. That command handler code could delete items from either control. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            _ 1 Reply Last reply
            0
            • C Chris Meech

              If we could see your 'delete' code, that could help. That command handler code could delete items from either control. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              void CMyTree::OnEditDelete()
              {
              // TODO: Add your command handler code here

              CString sItem,sMessage;
              CTreeCtrl& Tree = GetTreeCtrl();
              HTREEITEM hSelectedItem = Tree.GetSelectedItem();
              if(! hSelectedItem)return;
              
              sItem = Tree.GetItemText(hSelectedItem);
              sMessage.Format(\_T("Are you sure you want to delete '%s' item ?"),sItem);
              	if(IDYES != MessageBox(sMessage,NULL,MB\_YESNO | MB\_ICONWARNING))return;
              }
              
              
              // here I effectivelly delete an item into a serialized file and after that ... ( the tree view is loaded with serialized data )
              theApp.UpdateAllViews(NULL,CMyApp::UH\_ITEM\_CHANGED);
              

              }

              the code for delete an item from list view is similar, but at the point when I have confirm message-box, the message said to me that want to delete item from listview, not from tree view .... I don't know if I'm was understable, but thank you anyway for your interest !!!!

              G 1 Reply Last reply
              0
              • _ _Flaviu

                void CMyTree::OnEditDelete()
                {
                // TODO: Add your command handler code here

                CString sItem,sMessage;
                CTreeCtrl& Tree = GetTreeCtrl();
                HTREEITEM hSelectedItem = Tree.GetSelectedItem();
                if(! hSelectedItem)return;
                
                sItem = Tree.GetItemText(hSelectedItem);
                sMessage.Format(\_T("Are you sure you want to delete '%s' item ?"),sItem);
                	if(IDYES != MessageBox(sMessage,NULL,MB\_YESNO | MB\_ICONWARNING))return;
                }
                
                
                // here I effectivelly delete an item into a serialized file and after that ... ( the tree view is loaded with serialized data )
                theApp.UpdateAllViews(NULL,CMyApp::UH\_ITEM\_CHANGED);
                

                }

                the code for delete an item from list view is similar, but at the point when I have confirm message-box, the message said to me that want to delete item from listview, not from tree view .... I don't know if I'm was understable, but thank you anyway for your interest !!!!

                G Offline
                G Offline
                Goto_Label_
                wrote on last edited by
                #7

                [CListCtrl::DeleteItem] Deletes an item from a list view control.

                CListCtrl* pmyListCtrl;

                int nCount = pmyListCtrl->GetItemCount();

                // Delete all of the items from the list view control.
                for (int i=0;i < nCount;i++)
                {
                pmyListCtrl->DeleteItem(0);
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups