ListBox Initial Value Setup

new BookmarkLockedFalling
sundale
New Member
*

sundale Avatar

Posts: 34

Post by sundale on Dec 29, 2009 8:49:13 GMT -5

Having difficulties in ListBox initial value setting.
I cannot get the expected results.


I have tried following code:


' list of shop code - 12 items
shoplist$ = "CH,HH,HC,HS,TO,KI,SH,WH,CS,ET,AG,YJ"
idcount = 12

' allocate listbox array
dim shopgroup$(idcount)
for x = 1 to idcount
shopgroup$(x)=word$(shoplist,ドル x, ",")
next x

' set initial value for listbox
curID$ = "TO"
idx = 5
htmlcode = 1

print chr$(160)
if htmlcode then
html "<TABLE><TR><TD align=right>"
html "Shop Code:</TD><TD>"
else
print "Shop Code:"
end if

listbox #shopchoice, shopgroup$(), 0

if htmlcode then html "</TD></TR><TR><TD>"
print " "

#shopchoice select(idx)
print "initial setup"
print "============="
print "index = "; idx; "("; #shopchoice selection(); ")"
print "selection = "; #shopchoice selection$()
print " "

link #conf, "[OK]", [showSelected]
link #can, "[Cancel]", [cancel]
if htmlcode then html "</TD></TR></TABLE>"
wait


[showSelected]
print " "
print "Result values"
print "============="
print "index = "; #shopchoice selection()
print "choice = "; #shopchoice selection$()

[cancel]
end


The result shows bellow:

Shop Code: KI

initial setup
=============
index = 5(5)
selection = 5

[OK][Cancel]

Result values
=============
index = 0
choice = KI


Why?
1) The initial value has been set properly at initial time index=5
2) But, the listbox still shows the first item
3) After "OK" is clicked the result is not correct index=0
4) But, the string value is correct.

How?
1) do I make it display fifth item "TO" at initial time?
2) if the user change the item, any method to understand that event occured?
so that I can display other items accordingly? ex) Actual Name from the code selected.
Last Edit: Dec 29, 2009 8:53:43 GMT -5 by sundale
i7Q720/win7/ RB v1.0.1 Build 2.44
sundale
New Member
*

sundale Avatar

Posts: 34

i7Q720/win7/ RB v1.0.1 Build 2.44
JackWebb
Junior Member
**

JackWebb Avatar

Posts: 69

Post by JackWebb on Dec 29, 2009 22:30:06 GMT -5

Sundale,

Nothing wrong with your logic except that the variable curID$ was not being used anywhere in the program. the line that says

#shopchoice select(idx) is what gives you the 5 instead of the word "TO".. Change the line to

#shopchoice select(curID$)

and it will return "TO" but you probably already knew that. Other than that, there isn't an index anywhere. The statement

#shopchoice selection()

is not a valid runbasic statement as far as I know. I searched the wikki and could not find that method so it's always 0. Why it prints 5 is beyond me... Looks like 5 might be a string ??? I rewrote some sections, seems to work ok. By the way, nice work!


' allocate listbox array
dim shopgroup$(12)
gosub [LoadArray]

' set initial value for listbox
idx = 5 'make "TO" initial (default) setting
shopgroup$(0) = shopgroup$(idx) 'assign default setting to array 0

htmlcode = 1
print chr$(160)
if htmlcode then
html "<TABLE><TR><TD align=right>"
html "Shop Code:</TD><TD>"
else
print "Shop Code:"
end if

listbox #shopchoice, shopgroup$(), 1
if htmlcode then
html "</TD></TR><TR><TD>"
end if
print " "

print "initial setup"
print "============="
print "index = "; idx; "("; shopgroup$(idx); ")"
print "selection = "; shopgroup$(idx)
print " "

link #conf, "[OK]", [showSelected]
link #can, "[Cancel]", [cancel]
if htmlcode then
html "</TD></TR></TABLE>"
end if
wait

[showSelected]
gosub [LoadArray]
for idx = 0 to 12
if shopgroup$(idx) = #shopchoice selection$() then
exit for
end if
next idx

print " "
print "Result values"
print "============="
print "index = "; idx
print "choice = "; #shopchoice selection$()

[cancel]
end

[LoadArray]
restore
for idx = 0 to 12
read shopgroup$(idx)
next idx
return

' data section
data "--", "CH", "HH", "HC", "HS", "TO", "KI", "SH", "WH", "CS", "ET", "AG", "YJ"


Happy New Year! ;D
Live to code, code to live!
sundale
New Member
*

sundale Avatar

Posts: 34

Post by sundale on Dec 29, 2009 23:26:45 GMT -5

Hi Jack, Thanks for your help.

The main idea is to set the initial value to array zero.
Just adding one line worked as desired.

shopgroup$(0)= shopgroup$(idx)

I have seen the selection() statement from the "Run BASIC Document" the help file in the Run Basic - section LISTBOX methods. Anyway learned new stuff!

Happy New Year!!!
Last Edit: Dec 29, 2009 23:29:25 GMT -5 by sundale
i7Q720/win7/ RB v1.0.1 Build 2.44
JackWebb
Junior Member
**

JackWebb Avatar

Posts: 69

Post by JackWebb on Dec 30, 2009 20:05:53 GMT -5

Hey Sundale thanks!

I found the method in the RunBasic documentaion. From the documentation:

LISTBOX methods

#handle SELECTION() - Return the current selection as a number.
#handle SELECTION$() - Return the current selection as a string.

So if i'm reading this correctly selection() should work the same way selection$() does, but it always returns 0.


' allocate listbox array
dim shopgroup$(12)
gosub [LoadArray]

' set initial value for listbox
idx = 5 'make "TO" initial (default) setting
shopgroup$(0) = shopgroup$(idx) 'assign default setting to array 0

htmlcode = 1
print chr$(160)
if htmlcode then
html "<TABLE><TR><TD align=right>"
html "Shop Code:</TD><TD>"
else
print "Shop Code:"
end if

listbox #shopchoice, shopgroup$(), 1
if htmlcode then
html "</TD></TR><TR><TD>"
end if
print " "

print "initial setup"
print "============="
print "index = "; idx; "("; shopgroup$(idx); ")"
print "selection = "; shopgroup$(idx)
print " "

link #conf, "[OK]", [showSelected]
link #can, "[Cancel]", [cancel]
if htmlcode then
html "</TD></TR></TABLE>"
end if
wait

[showSelected]
gosub [LoadArray]
for idx = 0 to 12
if shopgroup$(idx) = #shopchoice selection$() then
exit for
end if
next idx

print " "
print "Result values"
print "============="

print "index from idx = "; idx
print "index from selection() = "; #shopchoice selection()
print "choice from selection$() = "; #shopchoice selection$()

[cancel]
end

[LoadArray]
restore
for idx = 0 to 12
read shopgroup$(idx)
next idx
return

' data section
data "--", "CH", "HH", "HC", "HS", "TO", "KI", "SH", "WH", "CS", "ET", "AG", "YJ"
Live to code, code to live!
sundale
New Member
*

sundale Avatar

Posts: 34

Last Edit: Dec 31, 2009 1:05:04 GMT -5 by sundale
i7Q720/win7/ RB v1.0.1 Build 2.44
StefanPendl
Global Moderator
*****

StefanPendl Avatar

Run for BASIC ...
Posts: 945

Post by StefanPendl on Dec 31, 2009 12:15:32 GMT -5

sundale Avatar
Right! we need to move this topic to bug report, but I do not know how to do it.


Only moderators can move threads.

I have been struggling with this before and there is already a bug report, which turned out to be an enhancement request.

There is nothing wrong with both methods, but there is currently no method to get the array index of the selected item.

selection() is the same as val(selection$()).
Last Edit: Dec 31, 2009 12:16:20 GMT -5 by StefanPendl
[b]Stefan[/b] - [a href=http://stefanpendl.runbasichosting.com/]Homepage[/a][br][br][b]Please give credit if you use code I post, no need to ask for permission.[/b][br][br]Run BASIC 1.01, Fire-/Waterfox (IE11, Edge), Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
rich357
Senior Member
****

rich357 Avatar

Posts: 353

Post by rich357 on Jan 1, 2010 2:29:50 GMT -5

This has been discussed before as I brought up the same question once last year.
The only other way for now is to physically number the items.
Then selection() will return the number.
Since selection$() returns a string, the value will always be 0 (zero).

I believe Carl had mentioned he was working on getting indexing to work with it.

metro
Full Member
***

metro Avatar

Posts: 207

Intel Core2 QUAD CPU @2.54 x 4 Mint Linux 19.3 Mate