Re: list question

new topic     » goto parent     » topic index » view thread      » older message » newer message

----- Original Message -----
From: "George Walters" <gwalters at sc.rr.com>
To: "EUforum" <EUforum at topica.com>
Subject: list question

> If you've built a list, is there a way of finding an index where you have
> the 1st character(s) match. The only way I can think of is to do a search
> loop of some kind  using setIndex and getText until you find a match. This
> seems the hard way. Is there a more direct way? You can't keep track while
> you're building the list because win will sort it and get out of the input
> order.

Hi George,
I've included a rather long example of this sort of thing. Its about 130
lines of text but I hope it is worth viewing.


-------------
include win32lib.ew
without warning

constant
    win = create(Window, "test List", 0, 0, 0, 400, 400, 0),
    SB = create(StatusBar, "", win, 0, 0, 0, 0, 0),
    aList = create(SortedCombo, "", win, 5,5, 300, 300, 0),
    aFld = create(EditText,"", win, 5, 50, 300, 25, 0)

integer
    ebox
sequence
    kids

-- Put some stuff in the list.
addItem(aList,{
    "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten"}
        )

-- Here we find the 'editbox' that belongs to the combo.
kids = findChildren(aList)
ebox = kids[1][1]

-- Force the first item to be selected to start with.
setIndex(aList,1)

-- This is used to detect if the user is editing the field
-- with the Delete key, in which case the "matching" is skipped.
integer DelKey
DelKey = 0

procedure ebox_Change()
    sequence itemtext, thisitem
    integer items
    integer posn

    -- If the user pressed the delete key, don't try to match
    -- with any existing entries.
    if DelKey then
        return
    end if

    -- Find out where the cursor is.
    posn = getIndex(ebox)
    -- Get the typed text, convert to lower for easy compare
    itemtext = lower(getText(ebox))
    -- If nothing there then exit
    if length(itemtext) = 0 then
        return
    end if

    -- Reduce the comparision to whats on the left of the cursor
    itemtext = itemtext[1 .. posn-1]

    setText(SB, sprintf("DEBUG: Comparing '%s'", {itemtext}))

    -- Scan through all the items in the list for a partial match
    items = getCount(aList)
    for i = 1 to items do
        -- get the next item's text, convert to lower.
        thisitem = lower(getItem(aList, i))

        -- Skip items that are shorter than what's been typed.
        if length(thisitem) >= length(itemtext) then
            -- Compare the begining of the item
            if equal(itemtext, thisitem[1 .. length(itemtext)]) then
                -- If matching, update the list index
                setIndex(aList,i)
                -- Reposition the cursor
                setIndex(ebox, posn)
                -- Stop searching
                exit
            end if
        end if
    end for

end procedure
onChange[ebox] = routine_id("ebox_Change")

-- Set the delete key flag.
procedure onKeyDown_ebox(integer keycode, integer shifts)
    DelKey = (keycode = VK_DELETE)
end procedure
onKeyDown[ebox] = routine_id("onKeyDown_ebox")

-- See if I need to add to the list.
procedure onLostFocus_ebox()
    sequence itemtext, thisitem
    integer items
    integer found

    -- Get what's been typed, exit if empty
    itemtext = getText(ebox)
    if length(itemtext) = 0 then
        return
    end if

    found = False
    -- Check to see if it is in the list already.
    items = getCount(aList)
    for i = 1 to items do
        thisitem = getItem(aList, i)
        if equal(itemtext, thisitem) then
            -- Found! So mark the spot and stop searching
            found =  True
            exit
        end if
    end for

    -- if not found, add the new item
    if found = False then
        found = message_box(
                    sprintf("Do you wish to add '%s' to the
list?",{itemtext}),
                    "Add to List", MB_YESNO)
        if found = IDYES then
            addItem(aList, itemtext)
            setText(SB, sprintf("Added '%s' to list.", {itemtext}))
        else
            setText(SB, sprintf("'%s' not used.", {itemtext}))
        end if
    else
        setText(SB, sprintf("Using '%s'.", {itemtext}))
    end if

end procedure
onLostFocus[ebox] = routine_id("onLostFocus_ebox")

WinMain(win, Normal)
-------------
cheers,
Derek

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu