Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Listbox and Textbox - Adding information from file.

  1. #11
    Junior Member
    Join Date
    Jun 2024
    Posts
    12
    Rep Power
    2
    Hi

    Fixed the identified problems, but still having issue with the FOR statement:

    ....
    dim count as long = 15
    dim MaxElements as long = 15
    dim vList(15) AS STRING
    dim i As Integer = 15
    dim iTm (15) As String
    dim sBuffer (15) as string
    ...
    i = 0
    FOR count = 1 TO 15
    vList(Count) = iTm(Count)
    i = i+1
    NEXT

    I get a "...Subscript out of range in array or matrix..." error. Even though the variables are set to 15. Thank You...

  2. #12
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Rep Power
    11

    for next loop

    Quote Originally Posted by KF4GHG View Post
    Hi

    Fixed the identified problems, but still having issue with the FOR statement:

    ....
    dim count as long = 15
    dim MaxElements as long = 15
    dim vList(15) AS STRING
    dim i As Integer = 15
    dim iTm (15) As String
    dim sBuffer (15) as string
    ...
    i = 0
    FOR count = 1 TO 15
    vList(Count) = iTm(Count)
    i = i+1
    NEXT

    I get a "...Subscript out of range in array or matrix..." error. Even though the variables are set to 15. Thank You...
    Hi,
    Your exanple as displayed, works fine. The subscript error is not in this part of code.
    Carefully check the line where the error is reported.
    And also, remember that in ThinBasic, not as other old basics, the for counter is incremented when the last NEXT is reached.
    If you check 'count' after the NEXT statement, it will contain 16.


    Yann

  3. #13
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,155
    Rep Power
    736
    I agree with Yann's analysis.

    Seeing that i variable initialization and increment - if you use i as a subscript, it will be 0 in the first iteration.

    As thinBASIC arrays are 1 based, it could cause the problem.

    Maybe it would help to share full code, ideally wrapped using CODE tags, so we can refer to specific line more easily (see # button on toolbar when editing your post to generate the tags for you).


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #14
    Junior Member
    Join Date
    Jun 2024
    Posts
    12
    Rep Power
    2
    Thank you below is the code. i get a 400 error on line 68. The Out5.txt file is:

    one
    two
    three
    ...
    Ten
    -------------------------------------------------------------
    uses "UI"
    uses "file"
    USES "CONSOLE"

    dim tmpStr AS STRING
    dim tmpLong as long

    dim ID_List01 as long
    dim MaxItems as long
    dim CurItem as long
    dim Count as Integer

    dim MaxElements as long = 15 ' = 100
    dim vList(15) AS STRING

    ' *******
    dim InFileToLoad as string
    dim InFileChannel as long
    Dim i As Integer
    Dim iTm (15) As String
    dim sBuffer (15) as string

    InFileToLoad = app_sourcepath & "Out5.txt"
    ' ******

    Begin ControlID
    %Text01
    %List01
    %Butt01
    End ControlID

    function TBMain()
    DIM hDlg AS LONG

    '---Create a new dialog
    DIALOG NEW 0, "ListBox example", -1, -1, 275, 120, _
    %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU, _
    0 TO hDlg
    randomize

    '*******
    InFileChannel = file_open(InFileToLoad , "INPUT" )

    i = 1
    while not file_eof(InFileChannel)
    iTm (i) = file_lineinput(InFileChannel)
    print iTm(i)
    i=i+1
    wend

    String iTm() ' Notice you don't have to specify size ahead!
    PARSE(FILE InFileToLoad, iTm(), $CRLF)

    i = 1
    FOR count = 1 TO 15 ' MaxElements
    vList(Count) = iTm(i) 'sBuffer(i) ' "Ronnie Item " & FORMAT$(RND(1, 100000), "000000")
    i = i+1
    NEXT
    ' ******

    '---Show dialog in MODAL state
    DIALOG SHOW MODAL hDlg, call dlgCallback
    end function

    callback function dlgCallback() as long

    '---Now test the message
    SELECT CASE cbMsg

    case %WM_INITDIALOG '---Message fired at the very beginning when dialog is initialized
    '---This can be the right place to initialize dialog or create controls
    CONTROL ADD listbox , cbHndl, %List01 , vList() , 5, 20, 200, 100, 0, 0
    CONTROL ADD textbox , cbHndl, %Text01 , "" , 5, 5, 200, 12
    control add button , cbHndl, %Butt01 , "Set" , 210, 5, 60, 12

    CASE %WM_COMMAND

    '---Test which control has been clicked
    SELECT CASE cbCtl

    '---Something has happened with %List01
    case %List01

    select case cbCtlMsg
    case %LBN_SELCHANGE
    LISTBOX GET TEXT cbHndl, %List01 TO tmpStr
    control set text cbHndl, %Text01, tmpStr

    case %LBN_DBLCLK
    LISTBOX GET TEXT cbHndl, %List01 TO tmpStr
    control append text cbHndl, %Text01, "(DblClick " & tmpStr & ")"

    end select

    case %Butt01
    if cbCtlMsg = %BN_CLICKED then
    '---Get the text from a textbox and place into a string
    control get text cbHndl, %Text01 to tmpStr

    '---Get the window unique ID of the control
    CONTROL HANDLE cbHndl, %List01 TO ID_List01

    '---Get the number of items present into listbox
    MaxItems = sendmessage(ID_List01, %LB_GETCOUNT, 0, 0)

    '---Get current selected item (remember it start at zero position)
    listbox get selected cbHndl, %List01 to CurItem

    '---Now delete selected item ...
    listbox delete cbHndl, %List01, CurItem

    '---...and insert new text in same position
    sendmessage(ID_List01, %LB_INSERTSTRING, CurItem - 1, strptr(tmpStr))

    listbox select cbHndl, %List01, CurItem
    end if
    end select
    END SELECT
    end function




    Quote Originally Posted by Petr Schreiber View Post
    I agree with Yann's analysis.

    Seeing that i variable initialization and increment - if you use i as a subscript, it will be 0 in the first iteration.

    As thinBASIC arrays are 1 based, it could cause the problem.

    Maybe it would help to share full code, ideally wrapped using CODE tags, so we can refer to specific line more easily (see # button on toolbar when editing your post to generate the tags for you).


    Petr

  5. #15
    Junior Member
    Join Date
    Feb 2021
    Posts
    8
    Rep Power
    11
    Quote Originally Posted by KF4GHG View Post
    Thank you below is the code. i get a 400 error on line 68. The Out5.txt file is:
    Hi KF4GHG


    The debugging of your code was not obvious

    Look at the line flagged by " 'add Yann ============================"

    An instruction you should know and use "
    Ubound(Array)
    
    " it shows the upper boud of an Array , often the size of the array
      uses "UI"
      uses "file"
      USES "CONSOLE"
         
    %red_text = 12 'add Yann ============================================
    
      dim tmpStr  AS STRING
      dim tmpLong as long
      
      dim ID_List01 as long
      dim MaxItems  as long
      dim CurItem   as long
      dim Count as Integer
    
      dim MaxElements as long = 15 ' = 100
      dim vList(15) AS STRING
      
      ' *******
      dim InFileToLoad    as string    
      dim InFileChannel   as long
      Dim i               As Integer
      Dim iTm (15)        As String
      dim sBuffer (15)         as string
      
      InFileToLoad  = app_sourcepath & "Out5.txt"
      ' ****** 
    
      Begin ControlID
        %Text01 
        %List01
        %Butt01
      End ControlID  
    
      function TBMain()
        DIM hDlg    AS LONG
      
        '---Create a new dialog
        DIALOG NEW 0, "ListBox example", -1, -1, 275, 120, _
                                                      %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU, _
                                                      0 TO hDlg
        randomize
        
        '*******
    
      printl  ubound(itm) in %red_text   'add Yann ================================================ should be 15 as defined in line 22
    
        InFileChannel   = file_open(InFileToLoad  , "INPUT" ) 
        
        i = 1
        while not file_eof(InFileChannel)
          iTm (i) = file_lineinput(InFileChannel)
          print iTm(i)
          i=i+1
        wend
        
        String iTm()  ' Notice you don't have to specify size ahead!
      printl  ubound(itm) in %red_text   'add Yann ===================================  ahah now = 0  the preceding line sets size of iTm array to zero
      'add Yann ===================================  in fact it redefines iTm array without a real size   so this line should be suppressed
    
     'add Yann ==================================================
      printl    PARSE(FILE InFileToLoad, iTm(), $CRLF)  in %red_text         
      'add Yann =======================================curiously  the count is one and the array is empty
      'add Yann ======================================= the reason is not obvious
      'add Yann ======================================= I then tryed to load the file in a buffer and print len and content of the buffer 
      'add Yann ======================================= The len was 0 zero  and effectively nothing in the buffer  
      'add Yann ======================================= Then I reread the code .... line  47  File_Open (InFileToLoad , "INPUT" )  ...OK
      'add Yann ======================================= But  nowhere File_Close  
      'add Yann ======================================= The parsefile uses the opend file who reached EOF  and load nothing
      'add Yann ======================================= 
      'add Yann ======================================= So  add  :    file_close(infilechannel)        after Wend  and Parse will do better
        i = 1
        FOR count = 1 TO 15 ' MaxElements
            vList(Count) = iTm(i) 'sBuffer(i) ' "Ronnie Item " & FORMAT$(RND(1, 100000), "000000")
        i = i+1
        NEXT
         ' ******
        .
    .
    .
    .
    .
    .
    
      end function
    
    I simplified the code lines 50 -- 54 by loading the file data in a buffer and parsing that buffer.

    The code fails now : because PARSE redimension iTm array to the number of elements parsed
    The loop in lines 63 to 66 have to be modified to agree with the element count which could be obtained
    For example by using for count = 1 to maxelements
    and maxelements = PARSE(data_buffer, iTm(), $CRLF)

    and as you open a Console, I suggest you to use many Printl lines, to follow the way your script follows


    Enjoy.


    Yann


    element_count = PARSE(data_buffer.......
    
    '---Script created on 2024-07-10 21:00:49.595 by 
    uses "UI"
    uses "file"
    USES "CONSOLE"
    
    
    
    %red_text = 12
    dim parse_count_file as long
    dim data_buffer as string
    
    
    dim tmpStr AS STRING
    dim tmpLong as long
    
    dim ID_List01 as long
    dim MaxItems as long
    dim CurItem as long
    dim Count as Integer
    
    dim MaxElements as long = 15 ' = 100
    dim vList(15) AS STRING
    
    ' *******
    dim InFileToLoad as string
    dim InFileChannel as long
    Dim i As Integer
    Dim iTm (15) As String
    dim sBuffer (15) as string
    
    
    itm(12) = "xxxx"
    
    InFileToLoad = app_sourcepath & "Out5.txt"
    ' ******
    
    Begin ControlID
    %Text01
    %List01
    %Butt01
    End ControlID
    
    function TBMain()
    DIM hDlg AS LONG
    
    '---Create a new dialog
    DIALOG NEW 0, "ListBox example", -1, -1, 275, 120, _
    %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU, _
    0 TO hDlg
    randomize
    
    '*******
    data_buffer = file_load(InFileToLoad)
    
     maxelements =  PARSE(data_buffer, iTm(), $CRLF)     '   see note
    
    
    i = 1
    FOR count = 1 TO   MaxElements    'see note
    vList(Count) = iTm(i) 'sBuffer(i) ' "Ronnie Item " & FORMAT$(RND(1, 100000), "000000")
    i = i+1
    NEXT
    ' ******
    
    '---.
    .
    .
    .
    .
    ..
    
    Last edited by Yann_F_29; 11-07-2024 at 15:46.

  6. #16
    Junior Member
    Join Date
    Jun 2024
    Posts
    12
    Rep Power
    2
    Hi Yann -

    Thank you it worked. One strange thing is that the list box, which gets the information from a text file does not post the information in the order it was received. That is the text file is one, two,... ten and the list box first shows several blank lines and then eight, five, four, nine, one, seven... Again, thank you...

  7. #17
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,155
    Rep Power
    736
    Dear KF4GHG,

    you may consider having a closer look at Style and ExStyle parameters for Control Add Listbox and submitting them explicitly instead of passing zeroes.

    I suspect it could help. I don't remember if 0 is handled as "default", but help mentions the default Style is %LBS_NOTIFY Or %LBS_SORT Or %WS_TABSTOP Or %WS_VSCROLL, which means it sorts everything, which is not what you may want.

    Petr
    Last edited by Petr Schreiber; 21-07-2024 at 10:29.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  8. #18
    Junior Member
    Join Date
    Jun 2024
    Posts
    12
    Rep Power
    2
    Petr - Thank you for your suggestion. I will check it out... Ronnie

    Quote Originally Posted by Petr Schreiber View Post
    Dear KF4GHG,

    you may consider having a closer look at Style and ExStyle parameters for Control Add Listbox and submitting them explicitly instead of passing zeroes.

    I suspect it could help. I don't remember if 0 is handled as "default", but help mentions the default Style is %LBS_NOTIFY Or %LBS_SORT Or %WS_TABSTOP Or %WS_VSCROLL, which means it sorts everything, which is not what you may want.

    Petr

Page 2 of 2 FirstFirst 12

Similar Threads

  1. HELP FILE : Unicode information swapped
    By Michael Clease in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 09-03-2011, 12:19
  2. Listbox and Textbox
    By sandyrepope in forum UI (User Interface)
    Replies: 2
    Last Post: 30-05-2007, 00:50
  3. Outdated thinBASIC help file information
    By Petr Schreiber in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 05-09-2005, 17:40

Members who have read this thread: 9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •