• Welcome to PlanetSquires Forums.
 

why can't I select the last tab ?

Started by jermy, March 15, 2021, 03:52:45 PM

Previous topic - Next topic

jermy

The tab control just works fine until I want to close one tab and focus on another tab, what am I doing wrong here?


       Case WM_LBUTTONDOWN
         ' Get the position of the hit and determine if the CLOSE icon was clicked.
         GetCursorPos( @pInfo.pt )
         ScreenToClient( HWND_FRMMAIN_TAB,  @pInfo.pt) 
         nCurSel = TabCtrl_HitTest(HWND_FRMMAIN_TAB, @pInfo)

         ' Was the close icon pressed
         If pInfo.flags = TCHT_ONITEMICON Then
               
            Dim as long CurFocus = TabCtrl_GetCurFocus(HWND_FRMMAIN_TAB)                 
              if CurFocus =  0 then
                    TabCtrl_SetCurFocus( HWND_FRMMAIN_TAB, nCurSel )
? "hide the control "

             Elseif CurFocus = nCurSel then
                 TabCtrl_SetCurFocus( HWND_FRMMAIN_TAB, nCurSel - 1)
                 TabCtrl_DeleteItem( HWND_FRMMAIN_TAB, nCurSel)
             elseif CurFocus < nCurSel then
                 TabCtrl_DeleteItem( HWND_FRMMAIN_TAB, nCurSel)
           
             elseif CurFocus > nCurSel then    ' Why cant i select the lastTab ?
                 TabCtrl_DeleteItem( HWND_FRMMAIN_TAB, nCurSel)
    dim as integer lastTab = TabCtrl_GetItemCount(HWND_FRMMAIN_TAB) - 1 ' Returns the number of items if successful, or zero otherwise
                 TabCtrl_SetCurSel( HWND_FRMMAIN_TAB, lastTab)                      why can't I select the last tab ?                         
                 TabCtrl_SetCurFocus( HWND_FRMMAIN_TAB, lastTab)                  'why can't I select the last tab ?
              end if
                 AfxRedrawWindow( HWND_FRMMAIN_TAB )
         end if

Paul Squires

What is the value of lastTab in your example above? Does it actually return an integer >= 0 ?
Likewise, is HWND_FRMMAIN_TAB actually reporting a valid value?

Hard to debug without a working example but will try to give it a shot. Sometimes it is better to PostMessage a user defined message during actions involving setting focus and deleting or adding items. This is because during and after a message like WM_LBUTTONDOWN you could get multiple other windows messages that set or steal focus. The PostMessage ensures that all of those messages are processed prior to you performing your actions. That is not to say that this is the problem in this particular case of yours, but it is worth considering if we can't diagnose the problem you are experiencing
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

thank you very much,

PostMessage solved the problem.


dim as integer lastTab = TabCtrl_GetItemCount(HWND_FRMMAIN_TAB) - 1         
                                    TabCtrl_SetCurSel( HWND_FRMMAIN_TAB, lastTab)


TabCtrl_SetCurSel starts at zero index, while TabCtrl_GetItemCount returns the total number of tabs.
I'm not going to use that of course because it doesn't work.
this works well;

                dim as integer lastTab = TabCtrl_GetItemCount(HWND_FRMMAIN_TAB) - 1
                   PostMessageW ( HWND_FRMMAIN_TAB, TCM_SETCURSEL , lastTab, 0 )

Paul Squires

Awesome, happy to hear that PostMessage worked in this case!
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer