loading text file into listbox with a filter (solved)

Post

Posted
Rating:
#1 (In Topic #824)
Trainee
 Haya,
working with Gambas 3.16.2 and as a beginner, I'm getting around quite good. However in a couple of cases my logic cat does not comply …

I have a simple txt file and load it into a ListBox.
__________________________________________________________

 Dialog.Filter = ["*.txt", "Text Files", "*", "All Files"]
  If Dialog.OpenFile() Then Return
  ListBox1.List = Split(File.Load(Dialog.Path), "\n")
  
__________________________________________________________
  
  So far, so good.
  
  Now I want to remove the first 12 lines/items and even as it seems simple to do I'm running into sytax errors.
  
   __________________________________________________________
   
  Listbox1.Remove(ListBox1[0…11] ???
  __________________________________________________________
  
  Sorry, not getting it right.
  
  Further, the text file comes in approx. 250 lines as follows:
  
    
       16
 10      
       27
 13
 – – –      
        14
       16
 26      
 28      
       25
    0
 (sorry on submitting the post the mentiond spaces do not show - so, before each number there are 3 spaces or two spaces and also after the numbers …)
  
No I want to remove with a FOR loop - or on loading the file -  all spaces, empty lines and the awkward dashes inbetween (– – –)  

Another approach would be to list only the numbers, only one number as one item/line.

Sipping throu a lot of documentation did not help me to write the right syntax…

Would really appreciate some few lines in order to get things going.

Thanks and regards.
Klaus
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

pentilisea said

Now I want to remove the first 12 lines/items and even as it seems simple to do I'm running into sytax errors.

Listbox1.Remove(ListBox1[0…11] ???

Try

Code (gambas)

  1. Listbox1.Remove(0, 12)
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

pentilisea said

Another approach would be to list only the numbers, only one number as one item/line.

Try

Code (gambas)

  1. Dialog.Filter = ["*.txt", "Text Files", "*", "All Files"]
  2. If Dialog.OpenFile() Then Return
  3.  
  4. Dim Originlines As String[]
  5. Originlines = Split(File.Load(Dialog.Path), "\n")
  6.  
  7. Dim Filtered As New String[]
  8. For Each OriginLine As String In Originlines
  9.   If IsInteger(OriginLine) Then
  10.     Filtered.Add(OriginLine)
  11.  
  12. ListBox1.List = Filtered
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
 Hi Wolf,
thanks so much, and your suggestion works great.

Only thing I'm still fighting with are empty lines and spaces.

Loading only numbers (isInteger) has still space befor number, space after number - that how the original text was created and I have problems to display it here.

2
 3
   4
5  

However, extracting these numbers from the listbox into valueboxes, doing calculations etc. works ok - no errors so far.

Thanks again. I'm a slow learner, but getting there anyqay …
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’
Hi pentilisea,
happy to help.

pentilisea said

Loading only numbers (isInteger) has still space befor number, space after number

Use the "Trim$" function:

Code (gambas)

  1. Filtered.Add(Trim$(OriginLine))
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Trainee
 Thanks to Amour-Wolf I got the solution:

1. Only load digits
2. Eliminate empty lines in ListBox
3. Done

 SOLUTION
  
  Dialog.Filter = ["*.txt", "Text Files", "*", "All Files"]
If Dialog.OpenFile() Then Return
 
Dim Originlines As String[]
Originlines = Split(File.Load(Dialog.Path), "\n")
 
Dim Filtered As New String[]
For Each OriginLine As String In Originlines
  If IsInteger(OriginLine) Then
  
    Filtered.Add(Trim$(OriginLine))
  Endif
Next

 ListBox1.List = Filtered
Online now: No Back to the top
1 guest and 0 members have just viewed this.