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
loading text file into listbox with a filter (solved)
-
- Posts: 8
- Joined: Thursday 24th February 2022 12:18pm
loading text file into listbox with a filter (solved)
Last edited by pentilisea on Friday 25th February 2022 3:41pm, edited 1 time in total.
Re: loading text file into listbox with a filter
Trypentilisea wrote: ↑Thursday 24th February 2022 12:34pm 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] ???
Listbox1.Remove(0, 12)
Re: loading text file into listbox with a filter
Trypentilisea wrote: ↑Thursday 24th February 2022 12:34pm Another approach would be to list only the numbers, only one number as one item/line.
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(OriginLine)
Endif
Next
ListBox1.List = Filtered
-
- Posts: 8
- Joined: Thursday 24th February 2022 12:18pm
Re: loading text file into listbox with a filter
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 ...
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 ...
Re: loading text file into listbox with a filter
Hi pentilisea,
happy to help.
happy to help.
Use the "Trim$" function:pentilisea wrote: ↑Thursday 24th February 2022 6:54pm Loading only numbers (isInteger) has still space befor number, space after number
Filtered.Add(Trim$(OriginLine))
-
- Posts: 8
- Joined: Thursday 24th February 2022 12:18pm
Re: loading text file into listbox with a filter
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
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