Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode=1
#Include "win32api.inc"
Global hDlg, hComboBox, hComboEdit,hFont As Dword, iRow, iCol As Long
Enum Equates Singular
IDC_ComboBox = 500
IDC_ListView
IDM_One
IDM_Two
IDM_Exit
End Enum
Function PBMain() As Long
Dialog Default Font "Tahoma",10,0
Dialog New Pixels, 0, "ListView Test",300,300,320,200,%WS_SysMenu, 0 To hDlg
CreateListView
CreateComboBox
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local iReturn As Long, ComboInfo As ComboBoxInfo, temp$
Select Case Cb.Msg
Case %WM_InitDialog
ComboInfo.cbSize = SizeOf(ComboBoxInfo)
GetComboBoxInfo(hComboBox, ByVal VarPtr(ComboInfo)) 'get data about combobox
hComboEdit = ComboInfo.hwndItem 'handle to edit control of combobox
Case %WM_Command
Select Case Cb.Ctl
Case %IdCancel : HideComboBox 'hide ComboBox when ESC is pressed
Case %IdOk 'detect if user presses ENTER
Select Case GetParent(GetFocus)
Case hComboBox
Control Get Text hDlg, %IDC_ComboBox To temp$
ListView Set Text hDlg, %IDC_ListView, iRow+1, iCol+1, temp$
HideComboBox 'hide ComboBox if ENTER is pressed AND ComboBox has focus
End Select
Case %IDC_ComboBox
Select Case Cb.CtlMsg
Case %CBN_SelChange
ComboBox Get Select hDlg, %IDC_ComboBox To iReturn
ComboBox Get Text hDlg, %IDC_ComboBox, iReturn To temp$
ListView Set Text hDlg, %IDC_ListView, iRow+1, iCol+1, temp$
HideComboBox 'hide ComboBox when an item in the dropdown list is selected
End Select
End Select
Case %WM_Notify
Select Case Cb.NmId
Case %IDC_ListView
Select Case Cb.NmCode
Case %NM_Click
HideComboBox
End Select
End Select
Case %WM_ContextMenu 'detect right mouse click in ListView, then make ComboBox visible (hide if click outside allowed cells)
iReturn = GetDlgCtrlID (Cb.WParam)
Select Case iReturn
Case %IDC_ListView : ShowAndPositionComboBox
End Select
End Select
End Function
Sub HideComboBox
Control Show State hDlg, %IDC_ComboBox, %SW_Hide
Control Set Focus hDlg, %IDC_ListView
Control ReDraw hDlg, %IDC_ListView
End Sub
Sub CreateListview
Local i As Long
Control Add ListView, hDlg, %IDC_ListView, "", 10,10,300,180
ListView Insert Column hDlg, %IDC_ListView, 1, "Column1", 100, 0
ListView Insert Column hDlg, %IDC_ListView, 2, "Column2", 100, 0
ListView Insert Column hDlg, %IDC_ListView, 3, "Column3", 100, 0
For i = 1 To 8
ListView Insert Item hDlg, %IDC_ListView, i, 1, Trim$(Str$(i))
ListView Set Text hDlg, %IDC_ListView, i, 2, Trim$(Str$(i+100))
ListView Set Text hDlg, %IDC_ListView, i, 3, Trim$(Str$(i+200))
Next i
End Sub
Sub CreateComboBox
Dim MyArray(3) As String
Array Assign MyArray() = "zero", "one", "two", "three"
Control Add ComboBox, hDlg, %IDC_ComboBox, MyArray(), 0,0,100,100
Control Handle hDlg, %IDC_ComboBox To hComboBox
Font New "Tahoma",8,0 To hFont
Control Set Font hDlg, %IDC_ComboBox, hFont
Control Set Color hDlg, %IDC_ComboBox, %Black, %rgb_LightGreen
Control Show State hDlg, %IDC_ComboBox, %SW_Hide
End Sub
Sub ShowAndPositionComboBox
Local rc As Rect, i,j As Long, temp$
Local pt As Point, x,y As Long, LVHT As LVHitTestInfo
'get row/col under cursor
GetCursorPos LVHT.pt
ScreenToClient GetDlgItem(hDlg,%IDC_ListView), LVHT.pt
Control Send hDlg, %IDC_ListView, %LVM_SubItemHitTest, 0, VarPtr(LVHT)
iRow = LVHT.iItem
iCol = LVHT.isubItem
'move/resize TextBox over ListView cell
If iCol = 0 Then 'only show combobox on selected column (0=first column)
'put cell value into ComboBox
ListView Get Text hDlg, %IDC_ListView, iRow+1, iCol+1 To temp$
Control Set Text hDlg, %IDC_ComboBox, temp$
'show/position combobox
Control Get Loc hDlg, %IDC_ListView To i,j
ListView_GetSubItemRect GetDlgItem(hDlg,%IDC_ListView), iRow,iCol, %LVIR_Label, rc
'resize combobox height
Control Set Loc hDlg, %IDC_ComboBox, i+rc.nLeft, j+rc.nTop
Control Set Size hDlg, %IDC_ComboBox, rc.nRight-rc.nLeft+4, rc.nBottom-rc.nTop+4
Control Show State hDlg, %IDC_ComboBox, %SW_Show
Control Set Focus hDlg, %IDC_ComboBox
BringWindowToTop hComboBox
Control ReDraw hDlg, %IDC_ComboBox
Else
HideComboBox
End If
End Sub
http://www.garybeene.com/sw/gbsnippets.htm