PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: why can't I select the last tab ?  (Read 80 times)

jermy

  • Junior Member
  • **
  • Posts: 92
why can't I select the last tab ?
« on: March 15, 2021, 03:52: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?

Code: [Select]
       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
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9386
  • Windows 10
    • PlanetSquires Software
Re: why can't I select the last tab ?
« Reply #1 on: March 15, 2021, 04:51: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
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

  • Junior Member
  • **
  • Posts: 92
Re: why can't I select the last tab ?
« Reply #2 on: March 15, 2021, 05:20:13 PM »

thank you very much,

PostMessage solved the problem.
 
Code: [Select]
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;
Code: [Select]
                dim as integer lastTab = TabCtrl_GetItemCount(HWND_FRMMAIN_TAB) - 1
                   PostMessageW ( HWND_FRMMAIN_TAB, TCM_SETCURSEL , lastTab, 0 )
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9386
  • Windows 10
    • PlanetSquires Software
Re: why can't I select the last tab ?
« Reply #3 on: March 15, 2021, 07:47:10 PM »

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