Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

DLL32: Dealing with List boxes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • DLL32: Dealing with List boxes

    Code:
    '==============================================================================
    '
    '   List Box encapsulation for PB/DLL 32-bit
    '   Copyright (c) 1997,98 by PowerBASIC, Inc.
    '
    '   Notes:
    '
    '   *  Assumes that WIN32API.INC will also be included into your code.
    '   *  Windows 95 has a limit of 32767 items per listbox.
    '   *  Success = 0 if an operation is successful, otherwise a non-zero value
    '      is returned.
    '
    '==============================================================================
     
    '------------------------------------------------------------------------------
    '  TITLE: LbAddFile
    '   DESC: Add a specified filename to a listbox that contains a directory
    '         listing.
    ' SYNTAX: RetVal = LbAddFile(hListBox, Filename$)
    'RETURNS: The return value is the zero-based index to the filename in the list
    '         box. The return value is %LB_ERR if an error occurs; the return value
    '         is %LB_ERRSPACE if insufficient space is available to store the new
    '         filename.
    'REMARKS: The listbox to which Filename$ is added must have been filled by
    '         the LbDir call.
    '
    FUNCTION LbAddFile(BYVAL hListBox AS LONG, BYVAL Filename AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_ADDFILE, 0, STRPTR(Filename))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbAddString
    '   DESC: Add a string to a listbox.
    ' SYNTAX: RetVal = LbAddString(hListBox, Text$)
    'RETURNS: The return value is the zero-based index to the string in the list
    '         box. The return value is %LB_ERR if an error occurs; the return value
    '         is %LB_ERRSPACE if insufficient space is available to store the new
    '         string.
    'REMARKS: If the listbox does not have the %LBS_SORT style, the string is
    '         added to the end of the list. Otherwise, the string is inserted
    '         into the list and the list is sorted.
    '
    FUNCTION LbAddString(BYVAL hListBox AS LONG, BYVAL Text AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_ADDSTRING, 0, STRPTR(Text))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbDeleteString
    '   DESC: Delete a string from a listbox.
    ' SYNTAX: RetVal = LbDeleteString(hListBox, Position)
    '  PARAM: hListBox - Handle of the listbox.
    '         Position - Zero based index of the string to delete in the listbox.
    'RETURNS: The return value is a count of the strings remaining in the list.
    '         The return value is %LB_ERR if the index parameter specifies an index
    '         greater than the number of items in the list.
    '
    FUNCTION LbDeleteString(BYVAL hListBox AS LONG, BYVAL Index AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_DELETESTRING, Index, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbDir
    '   DESC: Fill the listbox with a file directory.
    ' SYNTAX: RetVal = LbDir(hListBox, FileAttribute, FileSpec$)
    '
    FUNCTION LbDir(BYVAL hListBox AS LONG, BYVAL Attr AS LONG, _
                   BYVAL FileSpec AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_DIR, Attr, STRPTR(FileSpec))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbFindString
    '   DESC: Find the first string in a listbox that contains the specific prefix.
    ' SYNTAX: n = LbFindString(hListBox, IndexStart, Prefix$)
    '
    FUNCTION LbFindString(BYVAL hListBox AS LONG, BYVAL indexStart AS LONG, BYVAL szCtStr AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_FINDSTRING, indexStart, STRPTR(szCtStr))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbFindStringExact
    '   DESC: Find the first string in a listbox that matches the specified string.
    ' SYNTAX: n = LbFindStringExact(hListBox, IndexStart, Prefix$)
    '
    FUNCTION LbFindStringExact(BYVAL hListBox AS LONG, BYVAL indexStart AS LONG, BYVAL szCtStr AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_FINDSTRINGEXACT, indexStart, STRPTR(szCtStr))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetAnchorIndex
    '   DESC: Retrieve the index of the anchor item (the item from which a
    '         multiple select starts).  A multiple selection spans all items
    '         from the anchor item to the carent item.
    ' SYNTAX: n = LbGetAnchorIndex(hListBox)
    '
    FUNCTION LbGetAnchorIndex(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETANCHORINDEX, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetCaretIndex
    '   DESC: Determine the index of the item that has the focus retangle in a
    '         multiple-selection listbox.  The item may or may not be selected.
    ' SYNTAX: n = LbGetCaretIndex(hListBox)
    '
    FUNCTION LbGetCaretIndex(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETCARETINDEX, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetCount
    '   DESC: Return the total number of items in a listbox.
    ' SYNTAX: n = LbGetCount( hListBox )
    '
    FUNCTION LbGetCount(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETCOUNT, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetCurSel
    '   DESC: Get the current selection index of the listbox.
    ' SYNTAX: ItemPosition = LbGetCurSel( hListBox )
    '
    FUNCTION LbGetCurSel(BYVAL hListBox AS LONG) AS LONG
     
      LOCAL Msg AS LONG
     
      IF (GetWindowLong(hListBox, %GWL_STYLE) AND %LBS_MULTIPLESEL) THEN
        Msg = %LB_GETCARETINDEX
      ELSE
        Msg = %LB_GETCURSEL
      END IF
     
      FUNCTION = SendMessage(hListBox, Msg, 0, 0)
     
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetHorizontalExtent
    '   DESC: Retrieve from a listbox the width, in pixels, by which the listbox
    '         can be scrolled horizontally (the scrollable width) if the listbox
    '         has a horizontal scroll bar.
    ' SYNTAX: Extent = LbGetHorizontalExtent(hListBox)
    'REMARKS: The return value is the scrollable width, in pixels, of the listbox.
    '         The listbox must have been defined with the %WS_HSCROLL style.
    '
    FUNCTION LbGetHorizontalExtent(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETHORIZONTALEXTENT, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetItemData
    '   DESC: Retrieve the application-defined 32-bit value associated with the
    '         specified list box item.
    ' SYNTAX: nData = LbGetItenData(hListBox, index)
    'REMARKS: The return value is the 32-bit value associated with the item, or
    '         %LB_ERR if an error occurs.
    '
    FUNCTION LbGetItemData(BYVAL hListBox AS LONG, BYVAL index AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETITEMDATA, index, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetLocale
    '   DESC: Retrieve the current locale of the listbox.
    ' SYNTAX: Locale = LbGetLocale(hListBox)
    'REMARKS: The return value is a 32-bit value that specifies the current locale
    '         of the listbox. The high-order word contains the country code and
    '         the low-order word contains the language identifier. You can use the
    '         locale to determine the correct sorting order of displayed text (for
    '         listboxes with the %LBS_SORT style) and text added by LbAddString.
    '
    FUNCTION LbGetLocale(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETLOCALE, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetSel
    '   DESC: Returns the select status of the specified item.
    ' SYNTAX: Status = LbGetSel(hListBox, ItemPosition)
    '
    FUNCTION LbGetSel(BYVAL hListBox AS LONG, BYVAL Index AS LONG) AS LONG
      FUNCTION = (SendMessage(hListBox, %LB_GETSEL, Index, 0) <> 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetSelCount
    '   DESC: Retrieve the number of selected items in a multi-selection listbox.
    ' SYNTAX: Count = LbGetSelCount(hListBox)
    'REMARKS: The return value is the count of selected items in the listbox. If
    '         the listbox is single-selection, the return value is %LB_ERR.
    '
    FUNCTION LbGetSelCount(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETSELCOUNT, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetSelItems
    '   DESC: Fill a buffer with an array of integers that specify the item numbers
    '         of selected items in a multiple-selection listbox.
    ' SYNTAX: items = LbGetSelItems(hListBox, UBOUND(n()), n(1))
    'REMARKS: The return value is the number of items placed in the buffer. If the
    '         listbox is a single-selection listbox, the return value is %LB_ERR.
    '
    FUNCTION LbGetSelItems(BYVAL hListBox AS LONG, BYVAL items AS LONG, FirstItem AS INTEGER) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETSELITEMS, items, VARPTR(FirstItem))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetText
    '   DESC: Return the text of the selected listbox item.
    ' SYNTAX: Text$ = LbGetText( hListBox, ItemPosition )
    '
    FUNCTION LbGetText(BYVAL hListBox AS LONG, BYVAL Index AS LONG) AS STRING
     
      LOCAL Buffer AS ASCIIZ * 256
     
      IF Index < 0 THEN
        Index = LbGetCurSel(hListBox)
      END IF
     
      SendMessage hListBox, %LB_GETTEXT, Index, VARPTR(Buffer)
     
      FUNCTION = Buffer
     
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbGetTopIndex
    '   DESC: Retrieve the index of the first visible item in a listbox.
    ' SYNTAX: Which = LbGetTopIndex
    'REMARKS: Initially the item with index 0 is at the top of the listbox, but if
    '         the contents have been scrolled another item may be at the top.
    '
    FUNCTION LbGetTopIndex(BYVAL hListBox AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_GETTOPINDEX, 0, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbInsertString
    '   DESC: Insert a string into a listbox.
    ' SYNTAX: RetVal = LbInsertString(hListBox, Position, Text$)
    'RETURNS: The return value is the index of the position at which the string was
    '         inserted. The return value is %LB_ERR if an error occurs.  The return
    '         value is %LB_ERRSPACE if insufficient space is available to store the
    '         new string.
    'REMARKS: Unlike LbAddString, LbInsertString does not cause a list with the
    '         %LBS_SORT style to be sorted.
    '
    FUNCTION LbInsertString(BYVAL hListBox AS LONG, BYVAL Index AS LONG, _
                            BYVAL Text AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_INSERTSTRING, Index, STRPTR(Text))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbResetContent
    '   DESC: Clear the contents of a listbox.
    ' SYNTAX: LbResetContent hListBox
    'REMARKS: If you create the listbox with an owner-drawn style but without the
    '         %LBS_HASSTRINGS style, the owner of the listbox receives a
    '         %WM_DELETEITEM message for each item in the listbox.
    '
    SUB LbResetContent(BYVAL hListBox AS LONG)
      SendMessage hListBox, %LB_RESETCONTENT, 0, 0
    END SUB
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSelectString
    '   DESC: Select an item in a listbox by its text (or partial text).
    ' SYNTAX: RetVal = LbSelectString(hListBox, StartPosition, Text$)
    'REMARKS: The return value is the index of the selected item if the search was
    '         successful.  The return value is %LB_ERR if the search was
    '         unsuccessful and the current selection is not changed.
    '
    FUNCTION LbSelectString(BYVAL hListBox AS LONG, BYVAL Start AS LONG, _
                            BYVAL Text AS STRING) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_SELECTSTRING, Start, STRPTR(Text))
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetAnchorIndex
    '   DESC: Set the anchor item -- that is, the item from which a multiple
    '         selection starts. A multiple selection spans all items from the
    '         anchor item to the caret item.
    ' SYNTAX: Success = LbSetAnchorIndex(hListBox, index)
    '
    FUNCTION LbSetAnchorIndex(BYVAL hListBox AS LONG, BYVAL index AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_SETANCHORINDEX, index, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetCurSel
    '   DESC: Set the highlighted item in a listbox.
    ' SYNTAX: RetVal = LbSetCurSel(hListBox, ItemPosition)
    'RETURNS: The return value is %LB_ERR if an error occurs.  The return value
    '         will be %LB_ERR even though no error has occurred if the index
    '         parameter is -1.
    'REMARKS: LbSetCurSel selects a string and scrolls it into view, if necessary.
    '         When the new string is selected, the listbox removes the highlight
    '         from the previously selected string.  If the index parameter is -1,
    '         the listbox is set to have no selection.
    '
    FUNCTION LbSetCurSel(BYVAL hListBox AS LONG, BYVAL Index AS LONG) AS LONG
     
      LOCAL Msg AS LONG
     
      IF (GetWindowLong(hListBox, %GWL_STYLE) AND %LBS_MULTIPLESEL) THEN
        Msg = %LB_SETSEL
      ELSE
        Msg = %LB_SETCURSEL
      END IF
     
      FUNCTION = SendMessage(hListBox, Msg, 0, Index)
     
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetHorizontalExtent
    '   DESC: Set the width, in pixels, by which a list box can be scrolled
    '         horizontally (the scrollable width). If the width of the list box is
    '         smaller than this value, the horizontal scroll bar horizontally
    '         scrolls items in the list box. If the width of the list box is equal
    '         to or greater than this value, the horizontal scroll bar is hidden.
    ' SYNTAX: LbSetHorizontalExtent hListBox, cxExtent
    'REMARKS: The list box must have been defined with the WS_HSCROLL style.
    '
    SUB LbSetHorizontalExtent(BYVAL hListBox AS LONG, BYVAL cxExtent AS LONG)
      SendMessage hListBox, %LB_SETHORIZONTALEXTENT, cxExtent, 0
    END SUB
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetLocale
    '   DESC: Set the current locale of the listbox.  You can use the locale to
    '         determine the correct sorting order of displayed text (for listboxes
    '         with the LBS_SORT style.
    ' SYNTAX: OldLocale = LbSetLocale(hListBox, wLocaleID)
    'REMARKS: The return value is the previous locale identifier. If the wLocaleID
    '         parameter specifies a locale that is not installed, the return value
    '         is LB_ERR and the current listbox locale is not changed.
    '
    FUNCTION LbSetLocale(BYVAL hListBox AS LONG, BYVAL wLocaleID AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_SETLOCALE, wLocaleID, 0)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetSel
    '   DESC: Select a string in a multiple-selection listbox.
    ' SYNTAX: Result = LbSetSel(hListBox, fSelect, index)
    'REMARKS: fSelect is the selection status for the string. index is the string
    '         whose selection status is being changed.  %LB_ERR is returned if
    '         is out of range.
    '
    FUNCTION LbSetSel(BYVAL hListBox AS LONG, BYVAL fSelect AS LONG, BYVAL index AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_SETSEL, fSelect, index)
    END FUNCTION
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetTabStops
    '   DESC: Set the tab stops in a listbox.
    ' SYNTAX: LbSetTabStops hListBox, TabStops(0), TotalTabStops
    'REMARKS: TabStops() is a long integer array with each succeeding tab stop
    '         greater than the previous.  The listbox must have been created with
    '         the %LB_USETABSTOPS style.
    '
    SUB LbSetTabStops(BYVAL hListBox AS LONG, TabStops AS LONG, BYVAL TabStopsCnt AS LONG)
      SendMessage hListBox, %LB_SETTABSTOPS, TabStopsCnt, VARPTR(TabStops)
    END SUB
     
    '------------------------------------------------------------------------------
    '  TITLE: LbSetTopIndex
    '   DESC: Ensure that a particular item in a listbox is visible.
    ' SYNTAX: Result = LbSetTopIndex(hListBox, index)
    'REMARKS: The system scrolls the listbox contents so that either the specified
    '         item appears at the top of the listbox or the maximum scroll range
    '         has been reached.
    '
    FUNCTION LbSetTopIndex(BYVAL hListBox AS LONG, BYVAL index AS LONG) AS LONG
      FUNCTION = SendMessage(hListBox, %LB_SETTOPINDEX, index, 0)
    END FUNCTION

    -------------
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Home of the BASIC Gurus
    www.basicguru.com
Working...
X