Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listbox question
#1
Hello,

Can somebody help me with this (if it's even possible)

I like to have a multiselect listbox with some items (of course) like: one,two,three,four

the items selected have to be stored with some kind of seperation mark (eg ',' )

Also a editbox for entering extra information, this has to be stored in the same way.

If possible than the information in the editbox should be used the next time the listbox is shown. So it has to be added to the list. So if i add seven to the editbox, the next time the list should be: one,two,three,four,seven

Thanks for reading this and any help
#2
Maybe better use grid control? Look for function sample_Grid_Sqlite, it's in System folder, you can find it with the Find dialog/pane.
Instead of Listbox control it uses grid control. Instead of Edit control, user can add/edit directly in the grid. Saves in a Sqlite database file. Can be modified to save in a CSV file.

If grid is too difficult, can use Listbox. Some example are in the forum but may be difficult to find. I can help but need some more info.
Where the data should be saved? In a file, registry, or just in memory (eg global variable)?
Will it be a simple list or a 2-column table?
Items in the list must be unique?
Will need to save selected items? Or just to get into a variable?
When to get/save dialog data? After closing dialog (OK) or before closing (OK) or without closing (some other button)?
#3
Hello,

Is you can point me out to the examples, I will be thankfull. I will try the possibilities, and try to manage it.

Thanks
#4
Function dialog_listbox_add_remove_save
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

str- t_file="$my qm$\dialog_listbox_save.txt" ;;change this
ARRAY(str)- t_selected ;;the dialog will create this array on OK

if(!dir(t_file)) _s="one[]two[]three"; _s.setfile(t_file) ;;create file for testing

str controls = "4 3"
str e4 lb3
lb3.getfile(t_file)
if(!ShowDialog("dialog_listbox_add_remove_save" &dialog_listbox_add_remove_save &controls)) ret

lb3=t_selected ;;convert array to string, if need
out "SELECTED:"
out lb3

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;4 Edit 0x54030080 0x200 4 4 96 14 "" "Item to find or add"
;3 ListBox 0x54230109 0x200 4 22 98 90 ""
;7 Button 0x54032000 0x0 104 4 48 14 "Find" "Finds the specified item"
;5 Button 0x54032000 0x0 104 20 48 14 "Add" "Adds the specified item"
;6 Button 0x54032000 0x0 104 36 48 14 "Remove" "Removes selected items"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2030604 "*" "" "" ""

ret
;messages
int hlb i n; str s ss
int- t_changed
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
hlb=id(3 hDlg)
sel wParam
,case 5 ;;Add
,s.getwintext(id(4 hDlg)); if(!s.len) ret
,i=LB_FindItem(hlb s 0 1); if(i>=0 and mes("Already exists. Add anyway?" "" "YN?")!='Y') ret
,LB_Add hlb s
,t_changed=1
,
,case 6 ;;Remove
,ARRAY(int) ai
,if(!LB_GetSelectedItems(hlb ai)) ret
,for(i n-1 -1 -1) SendMessage(hlb LB_DELETESTRING ai[i] 0)
,t_changed=1
,
,case 7 ;;Find
,LB_SelectItem(hlb -1 2) ;;deselect all
,s.getwintext(id(4 hDlg)); if(!s.len) ret
,i=LB_FindItem(hlb s 0 1); if(i<0) i=LB_FindItem(hlb s); if(i<0) ret ;;find exact or partial
,LB_SelectItem(hlb i 1)
,
,case IDOK
,LB_GetSelectedItems(hlb 0 t_selected)
,if t_changed
,,for i 0 LB_GetCount(hlb)
,,,if(LB_GetItemText(hlb i s) and s.len) ss.addline(s)
,,ss.setfile(t_file)
,,t_changed=0
,
,case IDCANCEL
ret 1
Function LB_GetSelectedItems
Code:
Copy      Help
;/
function# hwnd [ARRAY(int)&ai] [ARRAY(str)&as]

;Gets selected items in a multisel listbox control.
;Returns number of selected items.

;hwnd - control handle.
;ai - array variable that receives indices of selected items. Optional, can be 0.
;as - array variable that receives text of selected items. Optional, can be 0.

;REMARKS
;The control must belong to current process. Use eg in dialog procedures.


if(&ai) ai=0
if(&as) as=0

int i n
n=SendMessage(hwnd LB_GETSELCOUNT 0 0); if(n<1) ret
if &ai or &as
,ARRAY(int) _ai; if(!&ai) &ai=_ai
,ai.create(n)
,n=SendMessage(hwnd LB_GETSELITEMS n &ai[0]); if(n<0) n=0
,if(n!ai.len) ai.redim(n)
,if &as
,,as.create(n)
,,for(i 0 n) LB_GetItemText(hwnd ai[i] as[i])
ret n
#5
Hello Gintaras!

That's quite an example! Thank you for this!

How can the selected items be used in a string?

Like one and two are selected, the 'OK' button is pressed, these are extracted to a sting with a seperator (like ',') and the dialog is closed till next time?

This would be the ultimum for my use... so if you are willing to help me with this i will be greatfully thankfull!

Thanks
#6
Updated. Also added function LB_GetSelectedItems, it can be used in all dialogs.
#7
HEllo Gintaras,

Well thank you for your time ! What a great example!

Have a nice day!
#8
Is it possible to automatically sort the listboxes content? So when something is added, next time it will appear automatically sorted?

TIA

Sonic
#9
Edit dialog:

'dialog editor'- select 'ListBox' - select 'styles' and in 'specific styles': LBS_SORT


Forum Jump:


Users browsing this thread: 1 Guest(s)