• Welcome to Theos PowerBasic Museum 2017.

Replace a ListBox item with a new item

Started by Paul Squires, August 05, 2007, 11:03:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Paul Squires


#Compile Exe

#Include "win32api.inc"
#Include "commctrl.inc"

%IDC_LIST1   = 100
%IDC_REPLACE = 101


'//
'//  Replace the specified ListBox text with the new text/data
'//
Function ReplaceListBoxText( ByVal hWndControl As Dword, _
                             ByVal nIndex      As Long, _
                             ByVal sNewText    As String, _
                             ByVal nNewData    As Long _
                             ) Export As Long
                             
   If IsWindow( hWndControl ) = 0 Then Exit Function
   
   ' Delete the item at the specified location
   Listbox_DeleteString hWndControl, nIndex
   nIndex = Listbox_InsertString( hWndControl, nIndex, sNewText )
   SendMessage hWndControl, %LB_SETITEMDATA, nIndex, nNewData
   Listbox_SetCurSel hWndControl, nIndex
                             
End Function



Function PBMain() As Long

  Local c    As Long
  Local hDlg As Dword
 
  Dim sArray(1 To 5) As String

  Dialog New 0, "ListBox Replace Item Demo",,, 160, 155, _
             %WS_CAPTION Or %WS_SYSMENU, 0 To hDlg

  sArray(1) = "Paul Squires"
  sArray(2) = "Patrice Terrier"
  sArray(3) = "Jose Roca"
  sArray(4) = "Theo Gottwald"
  sArray(5) = "Eros Olmi"
 
  Control Add ListBox, hDlg, %IDC_LIST1, sArray(), 5, 5, 150, 65, _
                       %WS_VSCROLL Or %WS_TABSTOP, %WS_EX_CLIENTEDGE


  Control Add Button, hDlg, %IDC_REPLACE, "&Replace", 105, 137, 50, 14

  Dialog Show Modal hDlg Call DlgProc

End Function

                                       
                                       
CallBack Function DlgProc() As Long

  Select Case As Long CbMsg

     Case %WM_COMMAND
     
        If ( CbCtl = %IDC_REPLACE ) And ( HiWrd(CbWParam) = %BN_CLICKED ) Then
           ' Replace the 3rd item (Jose Roca) with Edwin Knoppert. Note that
           ' we must convert our positioning to zero-based because DDT uses
           ' one based number (this is a great source of frustration when you
           ' are mixing DDT and WinAPI functions).
           ReplaceListBoxText GetDlgItem( CbHndl, %IDC_LIST1), 2, "Edwin Knoppert", 0
        End If

  End Select

End Function


Paul Squires
FireFly Visual Designer SQLitening Database System JellyFish Pro Editor
http://www.planetsquires.com

Kent Sarikaya

The replace function is listed twice in the program and causing a simple error.

Works fine otherwise. Thanks as I will be getting into listbox use soon.

Paul Squires

Quote from: Kent Sarikaya on August 06, 2007, 04:18:40 AM
The replace function is listed twice ...
Thanks - I edited the post to remove the duplicate.  oops.
Paul Squires
FireFly Visual Designer SQLitening Database System JellyFish Pro Editor
http://www.planetsquires.com

José Roca

 
To get it working with my new include files, change


#Include "win32api.inc"
#Include "commctrl.inc"


to:


#Include "ListBoxCtrl.inc"


José Roca

 
The following version uses only DDT statements and requires PBWIN 9.0+


#COMPILE EXE
#DIM ALL

%IDC_LIST1   = 100
%IDC_REPLACE = 101

'//
'//  Replace the specified ListBox text with the new text/data
'//
FUNCTION ReplaceListBoxText(BYVAL hDlg     AS DWORD, _
                            BYVAL cID      AS LONG, _
                            BYVAL nIndex   AS LONG, _
                            BYVAL sNewText AS STRING, _
                            BYVAL nNewData AS LONG _
                            ) AS LONG
                             
   IF ISWIN(hDlg) = 0 THEN EXIT FUNCTION
   
   ' Delete the item at the specified location
   LISTBOX DELETE hDlg, %IDC_LIST1, 3
   LISTBOX INSERT hDlg, %IDC_LIST1, 3, "Edwin Knoppert"
   LISTBOX SET USER hDlg, %IDC_LIST1, 3, nNewData
   LISTBOX SELECT hDlg, %IDC_LIST1, 3

END FUNCTION

FUNCTION PBMAIN() AS LONG

   LOCAL c    AS LONG
   LOCAL hDlg AS DWORD
 
   DIM sArray(1 TO 5) AS STRING

   DIALOG NEW 0, "ListBox Replace Item Demo",,, 160, 155, _
              %WS_CAPTION Or %WS_SYSMENU, 0 TO hDlg

   sArray(1) = "Paul Squires"
   sArray(2) = "Patrice Terrier"
   sArray(3) = "Jose Roca"
   sArray(4) = "Theo Gottwald"
   sArray(5) = "Eros Olmi"
 
   CONTROL ADD LISTBOX, hDlg, %IDC_LIST1, sArray(), 5, 5, 150, 65, _
                        %WS_VSCROLL Or %WS_TABSTOP, %WS_EX_CLIENTEDGE


   CONTROL ADD BUTTON, hDlg, %IDC_REPLACE, "&Replace", 105, 137, 50, 14

   DIALOG SHOW MODAL hDlg CALL DlgProc

END FUNCTION

CALLBACK FUNCTION DlgProc() AS LONG

   SELECT CASE AS LONG CBMSG

      Case %WM_COMMAND
     
         IF (CB.CTL = %IDC_REPLACE) AND (HI(WORD, CB.WPARAM) = %BN_CLICKED) THEN
            ' Replace the 3rd item (Jose Roca) with Edwin Knoppert.
            ReplaceListBoxText CB.HNDL, %IDC_LIST1, 3, "Edwin Knoppert", 0
         END IF

   END SELECT

END FUNCTION


Paul Squires

Hi Jose,

Looks like you hard coded some values in the ReplaceListBox Text. Maybe it should be like the following:


'//
'//  Replace the specified ListBox text with the new text/data
'//
FUNCTION ReplaceListBoxText(BYVAL hDlg     AS DWORD, _
                            BYVAL cID      AS LONG, _
                            BYVAL nIndex   AS LONG, _
                            BYVAL sNewText AS STRING, _
                            BYVAL nNewData AS LONG _
                            ) AS LONG
                             
   IF ISWIN(hDlg, cID) = 0 THEN EXIT FUNCTION
   
   ' Delete the item at the specified location
   LISTBOX DELETE hDlg, cID, nIndex
   LISTBOX INSERT hDlg, cID, nIndex, sNewText
   LISTBOX SET USER hDlg, cID, nIndex, nNewData
   LISTBOX SELECT hDlg, cID, nIndex

END FUNCTION

Paul Squires
FireFly Visual Designer SQLitening Database System JellyFish Pro Editor
http://www.planetsquires.com

José Roca

#6
 
True. I added the cID parameter but forgot to change the hard coded values. Just trying to keep the code updated to no confuse newcomers.