Posts: 1,058
Threads: 367
Joined: Oct 2007
The following dialog has a list box, initiated for example with 3 lines :
str controls = "3"
str lb3="Line 1[]Line 2[]Line 3"
if(!ShowDialog("My_Show_Dialog" &My_Show_Dialog &controls)) ret
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
BEGIN DIALOG
0 "" 0x90C80A44 0x100 0 0 223 136 "Dialog"
2 Button 0x54030000 0x4 170 116 48 14 "Stop-End"
3 ListBox 0x54331101 0x300 6 6 212 106 ""
END DIALOG
DIALOG EDITOR: "" 0x2030000 "" "" ""
ret
messages
sel message
case WM_INITDIALOG
case WM_DESTROY
case WM_COMMAND goto messages2
ret
messages2
sel wParam
case IDOK
case IDCANCEL
ret 1
I am wondering about the most efficient way to append some more lines to the list, at run-time later within above macro.
Thanks
Posts: 12,073
Threads: 140
Joined: Dec 2002
Function LB_Add
;/
function# hlb $text [itemdata]
;Adds an item to a list box control.
;Returns new item index. On error, returns a negative value.
int ni=SendMessageW(hlb LB_ADDSTRING 0 @text)
if(itemdata) SendMessage(hlb LB_SETITEMDATA ni itemdata)
ret ni
To remove all
Macro
SendMessage hlb LB_RESETCONTENT 0 0
Posts: 1,058
Threads: 367
Joined: Oct 2007
Dear Gintaras,
Perfect, many thanks, best regards
Posts: 1,006
Threads: 330
Joined: Mar 2007
Hi,
I am trying to do what ssimop asked above. I haven't been able to get the LB-Add function to work. Can anyone post a simple example of how the dialog with a listbox would interact with LB_Add function. Does LB_Add go under one of the cases in sel wParam or does it run after the Dialog closes?
Thank you so much!
Stuart
Posts: 12,073
Threads: 140
Joined: Dec 2002
Function Dialog65
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "3"
str lb3
if(!ShowDialog("Dialog65" &Dialog65 &controls)) ret
;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 223 135 "Dialog"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;3 ListBox 0x54230101 0x200 2 2 220 112 ""
;4 Button 0x54032000 0x0 4 116 48 14 "Update"
;END DIALOG
;DIALOG EDITOR: "" 0x2030001 "" "" ""
ret
;messages
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 4 ;;Update
,ARRAY(str) a; int i
,EnumProcessesEx 0 &a 2
,
,int hlb=id(3 hDlg)
,SendMessage hlb LB_RESETCONTENT 0 0
,for i 0 a.len
,,LB_Add hlb a[i]
,
,case IDOK
,case IDCANCEL
ret 1
Posts: 1,006
Threads: 330
Joined: Mar 2007
Thanks Gintaras!!!!
Your example was clear and allowed me to make my own taglist creator. This will eventually be part of a form that a user will fill out to categorize various data with their own individual tags.
Right now it only mes 's the selected tag but this will eventually be part of a form that a user will fill out to categorize various data with their own individual tags.
I hope this will be useful to others.
I will eventually put the .txt file in an xml or .ini file for each user
Any suggestions on how to more quickly delete all the tags. Is there a function to get all the contents of a ListBox and put it into a string or an Array. As you will see below, I kind of do it in a roundabout way (clear it, then count it, then create new array)
Thanks again for all your help!!!
Stuart
Function TagListDialog
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "3 5"
str lb3 e5
e5="Enter New Tag Here..."
if(dir("$my qm$\TagList.txt"))
,lb3.getfile("$my qm$\TagList.txt")
else lb3.setfile("$my qm$\TagList.txt")
if(!ShowDialog("TagListDialog" &TagListDialog &controls)) ret
str SelectedTag = lb3
SelectedTag.replacerx("^\d+ " "")
if SelectedTag = "-1"; ret; else mes SelectedTag
;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 193 189 "Tag List"
;1 Button 0x54030001 0x4 94 72 48 14 "OK"
;2 Button 0x54030000 0x4 144 72 48 14 "Cancel"
;4 Button 0x54032000 0x0 94 20 98 14 "Add Tag"
;5 Edit 0x54030080 0x200 94 4 98 14 "Enter New Tag Here...."
;7 Button 0x54032000 0x0 94 54 98 12 "Clear Tag List"
;6 Button 0x54032000 0x0 94 38 98 14 "Delete Selected Tag"
;3 ListBox 0x54230101 0x200 2 2 90 186 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030000 "" "" ""
ret
;messages
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 4 ;;Update
,,str AddTag
,,AddTag.getwintext(id(5 hDlg))
,,str AddTagBracket.format("%s[]" AddTag)
,,AddTagBracket.setfile("$my qm$\TagList.txt" -1);;adds new tag with bracket to file
,,str TagList.getfile("$my qm$\TagList.txt");; gets updated TagList from file
,,ARRAY(str) TagArray=TagList; int i;;puts updated TagList into array for display in ListBox
,,int hlb=id(3 hDlg)
,,SendMessage hlb LB_RESETCONTENT 0 0;;clears current ListBox
,,for i 0 TagArray.len;;populates ListBox with Updated TagList(placed into Array)
,,,LB_Add hlb TagArray[i]
,case 6
,,hlb=id(3 hDlg)
,,_i=LB_SelectedItem(hlb)
,,if(_i>=0) SendMessage hlb LB_DELETESTRING _i 0;;deletes selected item from taglist
,,;update taglist
,,_i=LB_GetCount(hlb)
,,ARRAY(str) NewTagArray
,,NewTagArray.create(_i)
,,for i 0 _i
,,,str tag
,,,LB_GetItemText(hlb i tag)
,,,NewTagArray[i]=tag
,,str NewTagList= NewTagArray
,,out NewTagList
,,NewTagList.setfile("$my qm$\TagList.txt" 0);;replaces prior TagList with NewTagList
,case 7;; Clear TagList
,,if(mes("Are you sure you want to delete the entire TagList?" "" "YNC!")!='Y') ret
,,hlb=id(3 hDlg)
,,SendMessage hlb LB_RESETCONTENT 0 0
,;,update taglist with empty list
,,_i=LB_GetCount(hlb)
,,ARRAY(str) EmptyArray
,,EmptyArray.create(_i)
,,str notags
,,for i 0 _i
,,,LB_GetItemText(hlb i notags)
,,,EmptyArray[i]=notags
,,str EmptyTagList= EmptyTagList
,,EmptyTagList.setfile("$my qm$\TagList.txt" 0);;replaces prior TagList with NewTagList
,,
,case IDOK
,case IDCANCEL
ret 1
Posts: 473
Threads: 33
Joined: Aug 2007
That's some nice work you did, I might copy this and try to make something else out of it.
Taking on Quick Macros one day at a time
Posts: 262
Threads: 63
Joined: Nov 2005
Outstanding stupomer I my have uses for it as well.
Posts: 1,006
Threads: 330
Joined: Mar 2007
Thanks guys...
I'll post updates...
I've learned so much from the forum and of course our Jedi master, Gintaras, in particular!
Stuart
Posts: 1,006
Threads: 330
Joined: Mar 2007
Hi All,
Don't know why I didn't do this with a combo box to begin with.
Almost got it all switched over. Now instead of explicitly having to add new tags, it will offer to add the new tag if it isn't in your list already.
Will post soon,
Stuart
Posts: 262
Threads: 63
Joined: Nov 2005
Posts: 1,006
Threads: 330
Joined: Mar 2007
Hi All,
Here it is - a TagList Manager based on ComboBox in Dialog. It is fully commented for learning purposes. I learned a huge amount on this little project. Output is currently only a mes statement but you can do with it whatever you want.
If anybody sees better or more elegant ways of doing some of the things I describe in the comments, please let me know.
For example, - (1) I am not sure why in the declaration the int has to be global. SInce it is at initiation of the dialog, won't it be established already before anything else has to see it? It seems to work fine being global, but I always try to avoid the global variables if possible
Thanks,
Stuart
Function TagListManager
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "8"
str cb8 c3Add
if(dir("$my qm$\TagList.txt"));; checks to see if a tag list already exists
,cb8.getfile("$my qm$\TagList.txt");; if so preloads it into the ComboBox string
else cb8.setfile("$my qm$\TagList.txt");; if not, uses 'setfile' to create an empty txt doc and loads it
if(!ShowDialog("TagListManager" &TagListManager &controls)) ret
str SelectedComboBox = cb8;;gets the CB selection at close of the dialog
if(TO_CBGetItem(SelectedComboBox)!=-1);;removes the index number from the string text and messages only if not empty
,mes SelectedComboBox;; messages the selection. You can do anything with this selection..i.e. save it to a file, fill a cell in Excel, etc
;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 146 87 "TagList"
;1 Button 0x54030001 0x4 26 71 48 14 "OK"
;2 Button 0x54030000 0x4 76 71 48 14 "Cancel"
;7 Button 0x54032000 0x0 26 57 98 12 "Clear Tag List"
;6 Button 0x54032000 0x0 26 39 98 14 "Delete Selected Tag"
;8 ComboBox 0x54230242 0x0 26 5 98 213 ""
;3 Button 0x54032000 0x0 26 21 98 14 "Add New Tag to Tag List"
;END DIALOG
;DIALOG EDITOR: "" 0x2030000 "" "" ""
ret
;messages
sel message
,case WM_INITDIALOG;;upon initiation of the Dialog
,,int+ hcb= id(8 hDlg);;establishes an int name (hcb) for the CB box which happens to be id 8
,,int i;;establishes a reusable counting integer
,case WM_DESTROY;;upon closing of the Dialog
,,str CBEntryFieldText.getwintext(hcb);; gets whatever is in EntryField, whether selected (i.e. pre-existing tag) or newly entered
,,int IsTagNew=CB_FindItem((hcb) CBEntryFieldText 0 1);; checks if CBEntryFieldText is a new tag; 0 1 = must match whole tag from the beginning
,,if IsTagNew=-1;;no matches, therefore a new tag
,,,if CBEntryFieldText=""; ret; else;;since want to ignore cases where no CB item is selected or no text entered (which returns returns ""))
,,,,if(mes("Add new tag ''%s'' to Tag List?" "TagList" "YN?a" CBEntryFieldText)!='Y') ret
,,,,CBEntryFieldText.setfile("$my qm$\TagList.txt" -1 -1 1);;appends new tag to Taglist (-1 -1 1 = appens all of 'SelectedText' as a new line at the end of the file
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3;;add Tag
,,CBEntryFieldText.getwintext(hcb);; gets whatever is in CB EntryField, whether selected (i.e. pre-existing tag) or newly entered
,,IsTagNew=CB_FindItem((hcb) CBEntryFieldText 0 1);; checks if CBEntryFieldText is a new tag; 0 1 = must match whole tag from the beginning
,,if IsTagNew >=0; mes("The tag ''%s'' already exists in your Tag List" "TagList" "i" CBEntryFieldText);ret ;;if there is a tag match (greater than or equal to 0), informs user
,,if IsTagNew=-1;;no matches, therefore a new tag
,,,if CBEntryFieldText=""; ret; else;;since want to ignore cases where no CB item is selected or no text entered (which returns returns ""))
,,,,if(mes("Add new tag ''%s'' to Tag List?" "TagList" "YN?a" CBEntryFieldText)!='Y') ret
,,,,int NewItemNum=CB_Add(hcb CBEntryFieldText)
,,,,CBEntryFieldText.setfile("$my qm$\TagList.txt" -1 -1 1);;appends new tag to Taglist (-1 -1 1 = appens all of 'SelectedText' as a new line at the end of the file
,case 6;;delete tag
,,str TagToDelete
,,_i=CB_SelectedItem(hcb TagToDelete)
,,if _i = -1; mes("Must have tag selected in order to delete from the Tag List" "TagList" "i");ret ;;gets selected tag and only continues if there is indeed a selected tag;; i.e. not if CB entry field is empty ("" i.e. _i=-1)
,,if(mes("Are you sure you want to delete the tag ''%s'' from the Tag List" "TagList" "YN?a" TagToDelete)!='Y') ret
,,SendMessage (hcb) CB_DELETESTRING _i 0;;deletes selected item from taglist in CB
,,;;but still need to update the file where the taglist is stored:
,,_i=CB_GetCount(hcb);; gets the total number of tags in CB taglist
,,ARRAY(str) NewTagArray;;creates new array
,,NewTagArray.create(_i);;with _i number of items
,,for i 0 _i;; for each item
,,,str tag
,,,CB_GetItemText(hcb i tag);;puts the item text in 'tag' string
,,,NewTagArray[i]=tag;; and puts this new tag string into the new tag array
,,str NewTagList= NewTagArray;; converts the array into a string collection
,,NewTagList.setfile("$my qm$\TagList.txt" 0);;replaces prior TagList with NewTagList (since no -1 flag, it replaces instead of appends)
,case 7;; Clear TagList
,,if(mes("Are you sure you want to delete the entire TagList?" "TagList" "YNC!")!='Y') ret
,,SendMessage hcb CB_RESETCONTENT 0 0;;clears CB
,,del "$my qm$\TagList.txt";;deletes TagList.txt which will be recreated fresh when dialog is re-run or if New Tag is added even in this Dialog instance
,case IDOK
,case IDCANCEL
ret 1
Posts: 473
Threads: 33
Joined: Aug 2007
I must say, I like this way better. Great work Stupomer.
Taking on Quick Macros one day at a time
Posts: 1,006
Threads: 330
Joined: Mar 2007
Posts: 1,000
Threads: 253
Joined: Feb 2008
Can't get this to work...Any advice?
I first got and error on the LB_Add...so then I realized had to add LB_Add function...Then there was LB_Add function errors.
Oh well. I am sure I am just doing something wrong.
Jimmy Vig
Posts: 473
Threads: 33
Joined: Aug 2007
I had no problem, maybe you should download the latest version of QM?
Taking on Quick Macros one day at a time
Posts: 12
Threads: 6
Joined: Jul 2008
Just to clarify (for noobs out there like me who might be having issues) you have to paste this code into a new function and name it "TagListManager" to get it to run. Awesome work and thanks for sharing this is very helpful to see how its done.
|