Page 1 of 1

String or Variant as ItemData

Posted: 01 Nov 2009, 14:39
by Mex
Hi Timo!

Thnx for the UniCombo :)

Is it possible to use string as ItemData or in the future maby add class or object as ComboItem, like in .NET?


BR;
Meelis

Re: String or Variant as ItemData

Posted: 02 Nov 2009, 16:49
by TiKu
Hi,

using Strings as ItemData is possible using two helper functions. If I have time tonight and if find the sourcecode in my mail archive, I'll post both functions tonight.
The baseline is to allocate memory for the string and store the address as ItemData. When you need the string, read it from this address. And don't forget to free the memory when removing the item (use the FreeItemData event).

Re: String or Variant as ItemData

Posted: 03 Nov 2009, 19:54
by TiKu
I've finally found the functions. They are for usage with the TabStrip control, but all you have to do to use it with ComboBox is change some data types.

Code: Select all

Private Function GetTabTag(ByVal tsTab As TabStripCtlLibUCtl.TabStripTab) As String
  Dim pMem As Long
  Dim s As String
  Dim sz As Long
 
  If Not (tsTab Is Nothing) Then
    pMem = tsTab.TabData
    If pMem Then
      CopyMemoryNew VarPtr(sz), pMem, 4
      s = String$(sz / 2, Chr$(0))
      CopyMemoryNew StrPtr(s), pMem + 4, sz
    End If
  End If
  GetTabTag = s
End Function
 
 
Private Sub FreeTabTag(ByVal tsTab As TabStripCtlLibUCtl.TabStripTab)
  If Not (tsTab Is Nothing) Then
    If tsTab.TabData <> 0 Then
      HeapFree GetProcessHeap(), 0, tsTab.TabData
      tsTab.TabData = 0
    End If
  End If
End Sub
 
Private Sub SetTabTag(ByVal tsTab As TabStripCtlLibUCtl.TabStripTab, ByVal Tag As String)
  Const HEAP_ZERO_MEMORY = &H8
  Dim pMem As Long
  Dim sz As Long
 
  If Not (tsTab Is Nothing) Then
    If tsTab.TabData <> 0 Then FreeTabTag tsTab
 
    sz = LenB(Tag)
    pMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz + 4)
    If pMem Then
      CopyMemoryNew pMem, VarPtr(sz), 4
      CopyMemoryNew pMem + 4, StrPtr(Tag), sz
      tsTab.TabData = pMem
    End If
  End If
End Sub
 
Private Sub TabStrip1_FreeTabData(ByVal tsTab As TabStripCtlLibUCtl.ITabStripTab)
  If Not tsTab Is Nothing Then
    FreeTabTag tsTab
  End If
End Sub