PlanetSquires Forums

Support Forums => General Board => Topic started by: jermy on March 15, 2021, 04:22:45 PM

Title: why can't I select the last tab ?
Post by: jermy on March 15, 2021, 04:22:45 PM
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
Title: Re: why can't I select the last tab ?
Post by: Paul Squires on March 15, 2021, 05:21:41 PM
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
Title: Re: why can't I select the last tab ?
Post by: jermy on March 15, 2021, 05:50:13 PM
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 )
Title: Re: why can't I select the last tab ?
Post by: Paul Squires on March 15, 2021, 08:17:10 PM
Awesome, happy to hear that PostMessage worked in this case!