|
Post by jarychk on Jul 22, 2022 4:18:42 GMT
BEWARE : THE PROGRAM POSTED BELOW STILL HAS A PROBLEM.
One of the members (xxx000abc) did help with part of this, about "reload" and "select... ". This is a self-assigned exercise for learning to manage combobox control and arrays. The code may be slightly messy, and maybe some of the code sequencing might be improved; but this sample program seems to work.
User can run the program, and makes changes to the names of items or delete, remove some item; but all one at a time. Program is not connected to any files; but that could be done (harder, and longer code).
rasz=8 'start with 8 array filled elements dim samples$(20) collection$="domain kingdom phylum class order family genera species" ' i=0 pull$="?" while pull$<>"" i=i+1 pull$=word$(collection$,i) samples$(i)=pull$ wend ' for j=1 to i print samples$(j) next j
'nomainwin
WindowWidth = 550 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
combobox #main.combobox1, samples$(, [combobox1DoubleClick], 38, 46, 144, 100 textbox #main.textbox2, 222, 41, 120, 25 button #main.button3, "to change", [button3Click], UL, 222, 116, 122, 25 button #main.button4, "to remove", [button4Click], UL, 222, 156, 122, 25 open "combobox study" for window as #main print #main, "trapclose [quit.main]"
print #main, "font ms_sans_serif 10"
wait
[quit.main] Close #main END
[combobox1DoubleClick] 'Perform action for the combobox named 'combobox1' 'Insert your own code here '#main.combobox1,"selection? id" 'jb member zzz000abc telling to make the change to #main.combobox1,"selectionindex? id" #main.combobox1,"contents? picked$" print #main.textbox2,picked$ wait
[button3Click] 'Perform action for the button named 'button3' 'Insert your own code here prompt "Make a different name to replace that?"; newname$ if picked$<>"" and newname$<>"" then 'tricky conditions for decision samples$(id)=newname$ 'need to reload the combobox to see new array #main.combobox1,"reload" 'And want textfield of combobox display to show as empty #main.combobox1,"select ";samples$(0) 'makes text field of combobox BLANK notice "progressing" for j=1 to rasz print samples$(j) next j end if 'reset variables let picked$="" let newname$="" id=0 wait
[button4Click] 'Perform action for the button named 'button4' 'Insert your own code here if picked$="" then wait 'samples$(id)="EMPTY" samples$(id)="" #main.combobox1,"reload" '#main.combobox1,"selectindex ";id 'effective up to here #main.combobox1,"select " 'code line removes textfield content of combobox 'NOW CONDENSE ARRAY one fewer elements 'NEW CODE BLOCK------------------------------------------- for d=id to rasz-1 'which initially is 8 rasz samples$(id)=samples$(id+1) next d samples$(rasz)="" 'because array is 1 fewer elements 'NOW make sure program knows new quantity of array values. rasz=rasz-1 'ENDLINE NEW CODE BLOCK----------------------------------- wait
|
|
|
Post by tsh73 on Jul 23, 2022 5:34:08 GMT
somehow it replaces wrong item steps to reproduce: run a program select "phylum" press remove select "order" press change in combobox last line (species) is gone, new line appears just above "order" program output
domain kingdom phylum class order family genera species
domain kingdom class hhgfh order family genera
|
|
|
Post by Rod on Jul 23, 2022 9:09:27 GMT
There are a few issues. Rather than blow by blow correct the code I offer this alternative to study. I think it does what you were trying to do. You can add and delete items to the combobox list.
One thing you should get in the habit of is correcting the syntax for the PRINT command. Often you will come across old code that has this form
PRINT #main, "something"
Now the syntax has changed and the , can catch you out. The new form is
#main "something"
So we lose the PRINT statement and the comma, easier?
'lets put eight items into a large array to allow for later additions 'in this code we will ignore cbarray$(0) as the combobox will reserve 'that item for its own use dim cbarray$(100) items=8
'here is our initial array, note that we start from cbarray$(1) as does word$() collection$="domain kingdom phylum class order family genra species" for w=1 to items cbarray$(w)=word$(collection$,w) next sort cbarray$(,1,items
nomainwin WindowWidth = 550 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
combobox #main.cb1, cbarray$(, [select], 38, 46, 144, 100 button #main.badd, "Add", [add], UL, 38, 116, 122, 25 button #main.bremove, "Remove Selected", [remove], UL, 38, 156, 122, 25 open "combobox study" for window as #main #main "trapclose [quit.main]"
wait
[quit.main] Close #main END
[select] 'fetch what has been selected #main.cb1 "contents? picked$" wait
[add] 'get what is in the combobox text field 'store it in cbarray$( , re-sort and reload 'then set the text field #main.cb1 "contents? picked$" items=items+1 cbarray$(items)=picked$ sort cbarray$(,1,items #main.cb1 "reload" #main.cb1 "select ";picked$ wait
[remove] 'fetch what has been selected 'remove it from cbarray$( , re-sort and reload 'then blank the text field #main.cb1 "contents? picked$" for i=1 to items if picked$=cbarray$(i) then cbarray$(i)="" next sort cbarray$(,1,items items=items-1 #main.cb1 "reload" #main.cb1 "selectindex 0" wait
|
|
|
Post by zzz000abc on Jul 24, 2022 8:16:53 GMT
here is a version to do the same without touching GUI 'wc(s$) showlist(a,b) yesno$(msg$) selnum(msg$,a,b) 'fillarray(n,l$) collection$="domain kingdom phylum class order family genera species" n=wc(collection$) :dim list1$(n) tmp=fillarray(n,collection$) print "list of items:" tmp=showlist(1,n) [selection] msg$="enter a number 1 to ";n;"..." resp=selnum(msg$,1,n) print "you have selected ";list1$(resp) [action] print "what you want to do?" msg$="1.change 2. remove : " tmp=selnum(msg$,1,n) if tmp=1 then goto [change] else goto [remove] wait [change] print "you have selected ";list1$(resp);" tochange" input"enter new item ";new$ l$="" for i=1 to n if i<>resp then l$=l$+word$(collection$,i)+chr$(32)_ else l$=l$+new$+chr$(32) next collection$=trim$(l$) print list1$(resp);"is replced with ";new$ tmp=fillarray(n,collection$) print "here is the new list:" tmp=showlist(1,n) goto [end] wait [remove] print "you have selected ";list1$(resp);" to remove" msg$="please conform y/n: " tmp$=yesno$(msg$) if tmp$="Y" then l$="" for i=1 to n if i<>resp then l$=l$+word$(collection$,i)+chr$(32) next collection$=trim$(l$) n=n-1 print list1$(resp);" removed" tmp=fillarray(n,collection$) print "here is the updated list:" tmp=showlist(1,n) else 'do nothing end if goto [end]
wait [end] print "thank you" end wait
'**************Functions************* 'wc(s$) showlist(a,b) yesno$(msg$) selnum(msg$,a,b) 'fillarray(n,l$) function wc(s$) k=0 while(word$(s$,k+1)<>"") k=k+1 wend wc=k end function function showlist(a,b) for i=a to b print i;". ";list1$(i) next showlist=1 end function function yesno$(msg$) while 1 input "";msg$;res$ if upper$(res$)="Y" or upper$(res$)="N" then exit while else print "invalid input" end if wend yesno$=upper$(res$) end function function selnum(msg$,a,b) while 1 input "";msg$;"";res$ if val(res$)<a or val(res$)>b or val(res$)=0 _ or int(val(res$))<>val(res$) then print "invalid input" else exit while end if wend selnum=val(res$) end function function fillarray(n,l$) redim list1$(n) for i=1 to n list1$(i)=word$(l$,i) next fillarray=1 end function
Note. 1. to dim an array list1$(). if you change that name you have to change the name in the functions fillarray() and showlist() also. 2. observe the position of wait 3. messages before and after updating the array 4. you can make the changes permanent by writing collection$ to file. you can use those functions in other programs too.
Edit. change the code to tmp=selnum(msg$,1,2) in line number 15
|
|
|
Post by jarychk on Jul 24, 2022 8:33:07 GMT
Rod, I rewrote the removal code branch. I did NOT ignore what you told me. I read and studied the code you wrote and thought on it carefully. My new code does not follow too closely everything you showed and explained. Now the repeatability during program run works. Or, at least it worked when I run the program a couple of times to test including a sequence of item removals. Anybody want to run, analyze, and check, you can try and see or other comments.
'My top notes documentation, remarks not shown here 'but a few are within program code
rasz=8 'start with 8 array filled elements dim samples$(20) collection$="domain kingdom phylum class order family genera species" ' i=0 pull$="?" while pull$<>"" i=i+1 pull$=word$(collection$,i) samples$(i)=pull$ wend ' for j=1 to i print samples$(j) next j
'nomainwin
WindowWidth = 550 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
combobox #main.combobox1, samples$(, [combobox1DoubleClick], 38, 46, 144, 100 textbox #main.textbox2, 222, 41, 120, 25 button #main.button3, "to change", [button3Click], UL, 222, 116, 122, 25 button #main.button4, "to remove", [button4Click], UL, 222, 156, 122, 25 open "combobox study" for window as #main print #main, "trapclose [quit.main]"
print #main, "font ms_sans_serif 10"
wait
[quit.main] Close #main END
[combobox1DoubleClick] 'Perform action for the combobox named 'combobox1' 'Insert your own code here '#main.combobox1,"selection? id" 'jb member zzz000abc telling to make the change to #main.combobox1,"selectionindex? id" #main.combobox1,"contents? picked$" print #main.textbox2,picked$ wait
[button3Click] 'Perform action for the button named 'button3' 'Insert your own code here prompt "Make a different name to replace that?"; newname$ if picked$<>"" and newname$<>"" then 'tricky conditions for decision samples$(id)=newname$ 'need to reload the combobox to see new array #main.combobox1,"reload" 'And want textfield of combobox display to show as empty #main.combobox1,"select ";samples$(0) 'makes text field of combobox BLANK notice "progressing" for j=1 to rasz print samples$(j) next j end if 'reset variables let picked$="" let newname$="" id=0 wait
[button4Click] 'Perform action for the button named 'button4' 'Insert your own code here 'ONLY focus on item remove, including rebuild the array, and then reset the combobox if id=0 then wait for dd=id to rasz-1 samples$(dd)=samples$(dd+1) next dd samples$(rasz)="" LET rasz=rasz-1 'NOW reload the combobox. Do NOT say PRINT(but seem no good); Do say PRINT! print #main.combobox1, "reload" print #main.combobox1,"select 0" print #main.textbox2,"" 'reset some variables picked$="" id=0
wait
|
|
|
Post by jarychk on Jul 24, 2022 8:42:06 GMT
zzz000abcI am trying to look through some of your code. I am unsure about using an array within a function. Difficult to think about when or how to do something like that. Understanding your example program will take me much more time.
|
|
|
Post by Rod on Jul 24, 2022 8:46:38 GMT
I have changed my sample code to fix the removal section. The sorting was the gremlin because it was sorting the "" ahead of other items so things were dropping off the list. This is fixed, it still uses sort(. I will try and post a version that does not use sort(.
'lets put eight items into a large array to allow for later additions 'in this code we will ignore cbarray$(0) as the combobox will reserve 'that item for its own use dim cbarray$(100)
'now we need a variable to track how many items we have loaded in the array items=8
'here is our initial array, note that we start from cbarray$(1) as does word$(index) 'and so to the combobox index. cbarray(0) is ignored. collection$="aone btwo cthree dfour efive fsix gseven height" for w=1 to items cbarray$(w)=word$(collection$,w) next sort cbarray$(,1,items
'nomainwin WindowWidth = 550 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
combobox #main.cb1, cbarray$(, [select], 38, 46, 144, 100 button #main.badd, "Add", [add], UL, 38, 116, 122, 25 button #main.bremove, "Remove Selected", [remove], UL, 38, 156, 122, 25 open "combobox study" for window as #main #main "trapclose [quit.main]"
wait
[quit.main] Close #main END
[select] 'fetch what has been selected #main.cb1 "contents? picked$" wait
[add] 'get what is in the combobox text field 'add it to the end of cbarray$( , re-sort and reload 'then set the text field #main.cb1 "contents? picked$" items=items+1 cbarray$(items)=picked$ sort cbarray$(,1,items #main.cb1 "reload" #main.cb1 "select ";picked$ wait
[remove] 'get the index of the item to delete 'compress the array to remove it from cbarray$( 're-sort and reload 'then blank the text field #main.cb1 "selectionindex? s" for i=s to items cbarray$(i) = cbarray$(i+1) next items=items-1 sort cbarray$(,1,items #main.cb1 "reload" #main.cb1 "selectindex 0" wait
Actually just rem out the sort commands and it still works. The issue though is that added items always are added to the end of the list which may not be what is required.
|
|
|
Post by jarychk on Jul 24, 2022 9:03:30 GMT
RIght now I am mostly confused about the meaning and difference between "select" and "selectindex".
|
|
|
Post by Rod on Jul 24, 2022 15:17:38 GMT
Just to be sure everyone gets what I am doing by ignoring cbarray$(0). I don't place anything in it, so it is always an "". When the array gets loaded to the combobox what the combobox does is looks at the first item in the array, cbarray$(0), because it is "" the combobox ignores it and then looks at the next item cbarray$(1) it then makes this the first item on the combo list with index 1. Thus the combobox index matches our actual cbarray$(index).
So none of this deducting one from the combo index value to match the input array.
If you had used cbarray$(0) and not left it as an empty string the combobox would load cbarray$(0) as combo index 1. The combo index does not point to the array index you need to deduct 1 from the combo index every time you want to get to the proper array item.
Using this technique and ensuring the combo index points at the actual array index allows you to use selectionindex? i where i is the actual index to your array item
|
|
|
Post by jarychk on Jul 24, 2022 17:41:06 GMT
I guess, what you mean is that SELECTIONINDEX is used for putting an array into the combobobox text field according to desired index. (??) Looking at the code again in Rod's example, #main.cb1 "selectindex 0" , this looks as a good way to make the textfield of the combobox set to empty string. Done according to array's INDEX.
|
|
|
Post by Rod on Jul 24, 2022 18:48:49 GMT
No, selectionindex? s is a commandstring that fetches the combo box index number that is currently selected. Nothing to do with sekectindex 0 which sets the text field contents to an empty string ie nothing selected.
note the ? Which is used to signal that you are getting info rather than sending info to the combo box.
|
|
|
Post by jarychk on Jul 24, 2022 19:12:28 GMT
No, selectionindex? s is a commandstring that fetches the combo box index number that is currently selected. Nothing to do with sekectindex 0 which sets the text field contents to an empty string ie nothing selected. note the ? Which is used to signal that you are getting info rather than sending info to the combo box. Selectionindex needs to be used always with the question mark. Reading about these commands in the helpfile becomes quickly confusing because so many similar words with "select-..." in them. This should be logical but still difficult to read and remember. A way is possible to organize these into a table, which I will try... soon. (The amount of these commands is basically 4 of them, so this should be simple.)
|
|
|
Post by tsh73 on Jul 24, 2022 20:39:46 GMT
Just in case someone still wonder what was wrong in original program in post #1 It was [remove] branch
for d=id to rasz-1 'which initially is 8 rasz samples$(id)=samples$(id+1) next d The only line in the loop is wrong - it changes samples$(id) but index of the loop not 'id' but 'd' Should be
samples$(d)=samples$(d+1)
|
|
|
Post by jarychk on Jul 25, 2022 0:58:52 GMT
Just in case someone still wonder what was wrong in original program in post #1 It was [remove] branch for d=id to rasz-1 'which initially is 8 rasz samples$(id)=samples$(id+1) next d The only line in the loop is wrong - it changes samples$(id) but index of the loop not 'id' but 'd' Should be samples$(d)=samples$(d+1)
EXCELLENT CATCH OF MISTAKE! I DO NOT KNOW HOW I MISSED THAT.
|
|
|
Post by zzz000abc on Jul 25, 2022 2:51:53 GMT
still it suffers from a small error that is when you try to remove the last element here is the updated code with some modifications
rasz=8 dim samples$(rasz) dim tsmp$(rasz) ' this temp. array is useful 'while updating samples$ array after any entry is removed collection$="domain kingdom phylum class order family genera species" ' i=0 pull$="?" while 1 ' it says while true i=i+1 'when i=9 pull$ becoms "" and while exits pull$=word$(collection$,i) if pull$<>"" then samples$(i)=pull$ else exit while wend 'you can do same thing using for loop also 'for i=1 to rasz 'pull$=word$(collection$,i) 'samples$(i)=pull$ 'next WindowWidth = 550 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
combobox #main.combobox1, samples$(, [combobox1DoubleClick], 38, 48, 220, 200 textbox #main.textbox2, 280, 41, 220, 50 button #main.button3, "to change", [button3Click], UL, 222, 116, 122,50 button #main.button4, "to remove", [button4Click], UL, 222, 186, 122,50 open "combobox study" for window as #main print #main, "trapclose [quit.main]"
print #main, "font 20" #main.textbox2,"!font 20" #main.button3,"!font 15" #main.button4,"!font 15" wait [combobox1DoubleClick] #main.combobox1,"selectionindex? id" #main.combobox1,"contents? picked$" print #main.textbox2,picked$ wait [button3Click] prompt "Make a different name to replace that?"; newname$ if picked$<>"" and newname$<>"" then 'tricky conditions for decision samples$(id)=newname$ #main.textbox2,"" #main.combobox1,"reload" #main.combobox1,"select ";samples$(0) notice "progressing" for j=1 to rasz print samples$(j) next j end if 'reset variables let picked$="" let newname$="" id=0 wait [button4Click] print id if samples$(id)="" then wait if id<rasz then for d=id to rasz-1 samples$(d)=samples$(d+1) 'id is replaced with d 'this logic works only when the selected element is not the last one 'if the selected element is last one then we do nothing here 'but simply ignore to it while storing the elemnets in temp array in the next for loop next d end if for i=1 to rasz-1 tsmp$(i)=samples$(i) next rasz=rasz-1 : id=0 redim samples$(rasz)'by using redim you can change the size of the array for i=1 to rasz samples$(i)=tsmp$(i) next #main.textbox2,"" #main.combobox1,"reload" #main.combobox1,"select ";samples$(0) wait [quit.main] Close #main END
|
|