Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'The following compilable code demonstrates a dialog with two
'listbox controls. The left contains source items. The right
'can be filled with items from the left. Items on the right
'can be repositioned or removed.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_ListBoxLeft = 500 : %IDC_ListBoxRight = 501
%IDC_ButtonAdd = 600 : %IDC_ButtonRemove = 601
%IDC_ButtonMoveup = 603 : %IDC_ButtonMoveDown = 602
Global hDlg As DWord, hList1 As DWord, hList2 As DWord
Function PBMain() As Long
Local i As Long
Dim MyArray(20) As String
For i = 0 To 20 : MyArray(i) = "Line" + Format$(i, "00") : Next i
Dialog New Pixels, 0, "ListBox Selector",300,300,300,200, %WS_SysMenu, 0 To hDlg
Control Add ListBox, hDlg, %IDC_ListBoxLeft, MyArray(), 10,40,75,100
Control Add ListBox, hDlg, %IDC_ListBoxRight, , 100,40,75,100, %LBS_Notify Or %WS_TabStop Or %WS_VScroll, %WS_Ex_ClientEdge
ListBox Add hDlg, %IDC_ListBoxRight, "Line00"
ListBox Add hDlg, %IDC_ListBoxRight, "Line07"
ListBox Add hDlg, %IDC_ListBoxRight, "Line05"
Control Add Button, hDlg,%IDC_ButtonAdd,"Add -->", 10,10,70,20
Control Add Button, hDlg,%IDC_ButtonRemove,"Remove Selection",100,10,100,20
Control Add Button, hDlg,%IDC_ButtonMoveUp,"Move Up",190,50,70,20
Control Add Button, hDlg,%IDC_ButtonMoveDown,"Move Down",190,80,70,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local iResult, iSelect, iCount As Long, temp As String
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case %IDC_ListBoxLeft
Select Case CB.Ctlmsg
Case %LBN_DblClk
ListBox Get Select hDlg, %IDC_ListBoxLeft To iSelect 'get selection item#
If iSelect = 0 Then Exit Function 'no selection
ListBox Get Count hDlg, %IDC_ListBoxLeft To iCount
If iCount = 0 Then Exit Function 'nothing to add
ListBox Get Text hDlg, %IDC_ListBoxLeft To temp 'get selection text
ListBox Find Exact hDlg, %IDC_ListBoxRight, 1,temp To iResult
If iResult Then Exit Function 'already exists on right
ListBox Insert hDlg, %IDC_ListBoxRight, 1, temp 'insert at top of list
ListBox Select hDlg, %IDC_ListBoxRight, 1 'keep something selected
Control Set Focus hDlg, %IDC_ListBoxRight 'keep focus on right listbox
End Select
Case %IDC_ListBoxRight
Select Case CB.Ctlmsg
'no action?
End Select
Case %IDC_ButtonAdd
ListBox Get Select hDlg, %IDC_ListBoxLeft To iSelect 'get selection item#
If iSelect = 0 Then Exit Function 'no selection
ListBox Get Count hDlg, %IDC_ListBoxLeft To iCount
If iCount = 0 Then Exit Function 'nothing to add
ListBox Get Text hDlg, %IDC_ListBoxLeft To temp 'get selected from left
ListBox Find Exact hDlg, %IDC_ListBoxRight, 1, temp To iResult
If iResult Then Exit Function 'already exists on right
ListBox Insert hDlg, %IDC_ListBoxRight, 1, temp 'insert at top to right
ListBox Select hDlg, %IDC_ListBoxRight, 1 'keep something selected
Control Set Focus hDlg, %IDC_ListBoxRight 'keep focus on right listbox
Case %IDC_ButtonRemove
ListBox Get Select hDlg, %IDC_ListBoxRight To iSelect 'get selection item#
If iSelect = 0 Then Exit Function 'no selection
ListBox Get Count hDlg, %IDC_ListBoxRight To iCount
If iCount = 0 Then Exit Function 'nothing to remove
ListBox Delete hDlg, %IDC_ListBoxRight, iSelect 'remove selected
ListBox Get Count hDlg, %IDC_ListBoxRight To iCount 'get new count
If iCount = 0 Then Exit Function 'nothing to select
If iSelect > iCount Then iSelect = iCount 'keep something selected
ListBox Select hDlg, %IDC_ListBoxRight, iSelect 'keep something selected
Control Set Focus hDlg, %IDC_ListBoxRight 'keep focus on right listbox
Case %IDC_ButtonMoveUp
ListBox Get Select hDlg, %IDC_ListBoxRight To iSelect 'get selection item#
If iSelect = 1 Then Exit Function 'already at top
If iSelect = 0 Then Exit Function 'no selection
ListBox Get Count hDlg, %IDC_ListBoxRight To iCount
If iCount = 0 Then Exit Function 'nothing to move
ListBox Get Text hDlg, %IDC_ListBoxRight To temp 'get selection text
ListBox Delete hDlg, %IDC_ListBoxRight, iSelect 'remove selected
ListBox Insert hDlg, %IDC_ListBoxRight, iSelect-1, temp 'insert selected at iSelect
ListBox Select hDlg, %IDC_ListBoxRight, iSelect-1 'keep something selected
Control Set Focus hDlg, %IDC_ListBoxRight 'keep focus on right listbox
Case %IDC_ButtonMoveDown
ListBox Get Select hDlg, %IDC_ListBoxRight To iSelect 'get selection item#
If iSelect = 0 Then Exit Function 'no selection
ListBox Get Count hDlg, %IDC_ListBoxRight To iCount
If iCount = 0 Then Exit Function 'nothing to move
If iSelect = iCount Then Exit Function 'already at bottom
ListBox Get Text hDlg, %IDC_ListBoxRight To temp 'get selection text
ListBox Delete hDlg, %IDC_ListBoxRight, iSelect 'remove selected
ListBox Insert hDlg, %IDC_ListBoxRight, iSelect+1, temp 'insert selected at iSelect
ListBox Select hDlg, %IDC_ListBoxRight, iSelect+1 'keep something selected
Control Set Focus hDlg, %IDC_ListBoxRight 'keep focus on right listbox
End Select
End Select
End Function
'gbs_00557
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm