Help with desktop listbox to tabpanel link

I wonder if anyone can help me with this?
I have a listbox and a tabpanel in a window. When I select an item in the listbox, it adds a tab panel and adds a container to the panel.
What I would like to do is when I change panels the corresponding item in the list box that created the tab is selected, also when I re-select the item in the list box it moves to the tab panel which was created with the first selection.
The selection part works fine, but I cant seem to figure out how to create a link from the listbox item to the tabpanel and back.

Here is the code in the Selection Changed event of the list box.

Var row As Integer = Me.RowFromXY(mLastClickX, mLastClickY)
var rcnt as Integer = me.RowCount
redim mItemOpenFlag(rcnt)

If mItemOpenFlag(row) = False Then ' keeps from selecting item in list more than once
  var pcnt as int8 = wndTransEditRef.tabTransEdit.PanelCount
  var apk as Int8 = me.CellTextAt(row,0).ToInteger 'id
  var anm as String = me.CellTextAt(row, 1) ' aname
  
  wndTransEditRef.tabTransEdit.AddPanel("  "+anm+" - " + apk.ToString + "  ")
  cntTransEditRef = new cntTransEdit
  cntTransEditRef.EmbedWithinPanel(wndTransEditRef.tabTransEdit,pcnt,3,30)
  wndTransEditRef.tabTransEdit.SelectedPanelIndex = wndTransEditRef.tabTransEdit.LastAddedPanelIndex
  me.RowTagAt(row) = apk 'used de-select the item in the list box when I remove the tab panel
  mItemOpenFlag(row) = True
  cntTransEditRef.BuildData(apk,anm)
end if

Was it the implementation or the logic you’re having an issue with? The logic seems pretty straightforward:

In the cntTransEditRef.BuildData(apk,anm) function save a unique identifier reference. Then, in the SelectionChanged event iterate all the panels created, looking for that unique identifier. If found, change the SelectedTabPanelIndex instead of creating a new panel.

I’m not sure if apk+anm is unique because I don’t know your project. If it is not, you will want to be sure you are using a unique identifier.

You probably don’t want to be using cntTransEditRef as a single instance since you’re embedding multiple containers. Make that an array so that you can iterate it looking for the container that has the correct unique identifier. Don’t forget to clean it up if you’re dynamically closing views as well.

Thank you Tim,
apk and anm are both unique fields from a sqllite table.
I think I am getting it now. I was iterating through the list rows, but did not think about iterating though the tab panels. This give me some ideas.

By the way, this is how I am removing the panels, which works as well. I am doing it in a button pressed event in the transeditref container.

var spi as Integer = wndTransEditRef.tabTransEdit.SelectedPanelIndex
wndTransEditRef.tabTransEdit.RemovePanelAt(spi)
var pc as Integer = wndTransEditRef.tabTransEdit.PanelCount - 1
if spi > pc then wndTransEditRef.tabTransEdit.SelectedPanelIndex = pc

var rcnt as Integer = wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowCount - 1

for i as Int16 = 0 to rcnt
  if wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowTagAt(i) = mAcctPK then
    wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowSelectedAt(i) = False
    wndTransEditRef.cntAccountSelectionRef.mItemOpenFlag(i) = False
  end if
next

OK, here is the best I can come up with and it works. Thank for your help.

In the listbox selection event.

Var row As Integer = Me.RowFromXY(mLastClickX, mLastClickY)

 If mChkdFlag(row) = False Then
  wndTransEditRef.mNewTabPanel = True
  var pc as int8 = wndTransEditRef.tabTransEdit.PanelCount 
  var apk as Int8 = me.CellTextAt(row,0).ToInteger
  var anm as String = me.CellTextAt(row, 1) ' aname
  var rc as Integer = me.RowCount
  redim mChkdFlag(rc)
  redim mRowToTab(rc)
  redim mTabToRow(pc)
  me.RowTagAt(row) = apk
  
  wndTransEditRef.tabTransEdit.AddPanel("  "+anm+" - " + apk.ToString + "  ")
  var lpi as Integer = wndTransEditRef.tabTransEdit.LastAddedPanelIndex
  cntTransEditRef = new cntTransEdit
  cntTransEditRef.EmbedWithinPanel(wndTransEditRef.tabTransEdit,pc,3,30)
  wndTransEditRef.tabTransEdit.SelectedPanelIndex = lpi
  mTabToRow(lpi) = row
  mRowToTab(row) = lpi
  
  cntTransEditRef.BuildData(apk,anm)
  mChkdFlag(row) = True
else
  if mChkdFlag(row) and wndTransEditRef.mRevSelection = false then 
    wndTransEditRef.tabTransEdit.SelectedPanelIndex = mRowToTab(row)
  end if
end if
wndTransEditRef.mRevSelection = false

in the tabpanel PanelChanged event

var rc as Integer = wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowCount - 1
var spi as Integer = me.SelectedPanelIndex
var wct as integer = wndTransEditRef.cntAccountSelectionRef.mTabToRow(spi)
var wcr as integer = wndTransEditRef.cntAccountSelectionRef.mRowToTab(wct)

if wndTransEditRef.cntAccountSelectionRef.mChkdFlag(wct) = True and wndTransEditRef.mNewTabPanel = false then
  wndTransEditRef.mRevSelection = true
  wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowSelectedAt(wndTransEditRef.cntAccountSelectionRef.mTabToRow(wcr)) = True
end if
wndTransEditRef.mNewTabPanel = false

I case anyone is interested. I had some issues with deleting after I made the changes above. I have worked out the code and cleaned it up a bit and it all works as expected.

Here is the ListBox SelectionChanged event.

Var row As Integer = mRow ' from mousedown x,y
var rc as Integer = me.RowCount
redim wndTransEditRef.mNewTabFlag(rc)
redim wndTransEditRef.mRowToTab(rc)

 If wndTransEditRef.mNewTabFlag(row) = False Then
  wndTransEditRef.mNewTabPanel = True
  var apk as Int8 = me.CellTextAt(row,0).ToInteger ' id
  var anm as String = me.CellTextAt(row, 1) ' aname
  var pc as int8 = wndTransEditRef.tabTransEdit.PanelCount 
  redim wndTransEditRef.mTabToRow(pc + 1)
  me.RowTagAt(row) = apk
  
  wndTransEditRef.tabTransEdit.AddPanel("  "+anm+" - " + apk.ToString + "  ")
  
  var lpi as Integer = wndTransEditRef.tabTransEdit.LastAddedPanelIndex
  cntTransEditRef = new cntTransEdit
  cntTransEditRef.EmbedWithinPanel(wndTransEditRef.tabTransEdit,pc,3,30)
  
  wndTransEditRef.mTabToRow(lpi) = row
  wndTransEditRef.mRowToTab(row) = lpi
  
  cntTransEditRef.BuildData(apk,anm)
  wndTransEditRef.mNewTabFlag(row) = True
  wndTransEditRef.tabTransEdit.SelectedPanelIndex = lpi
else
  if wndTransEditRef.mNewTabFlag(row) and wndTransEditRef.mTabSelection = false then 
    wndTransEditRef.tabTransEdit.SelectedPanelIndex = wndTransEditRef.mRowToTab(row)
  end if
end if
wndTransEditRef.mTabSelection = false
wndTransEditRef.btnCloseTab.Enabled = True

Here if the TabPanel PanelChanged Event.

var spi as Integer = me.SelectedPanelIndex
if spi >= 0 then
  var row as integer = wndTransEditRef.mTabToRow(spi)
  
  if wndTransEditRef.mNewTabFlag(row) = True and wndTransEditRef.mNewTabPanel = false then
    wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowSelectedAt(row) = True
    wndTransEditRef.mTabSelection = true
  end if
  wndTransEditRef.mNewTabPanel = false
end if

Here is the Close Tab button Pressed event

var spi as Integer = wndTransEditRef.tabTransEdit.SelectedPanelIndex
if spi >= 0 then
  var rc as Integer = wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowCount
  var pc as Integer = wndTransEditRef.tabTransEdit.PanelCount
  var row as Integer = mTabToRow(spi)
  
  mTabToRow().RemoveAt(spi)
  mRowToTab(row) = -1
  
  wndTransEditRef.mNewTabFlag(row) = False
  row = mTabToRow(0)
  wndTransEditRef.cntAccountSelectionRef.mRow = row
  wndTransEditRef.cntAccountSelectionRef.lstAccountsSelection.RowSelectedAt(row) = False
  wndTransEditRef.tabTransEdit.RemovePanelAt(spi)
  
  wndTransEditRef.tabTransEdit.SelectedPanelIndex = 0
  
else
  wndTransEditRef.btnCloseTab.Enabled = False
end if