loading text file into listbox with a filter (solved)

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
pentilisea
Posts: 6
Joined: Thursday 24th February 2022 12:18pm

loading text file into listbox with a filter (solved)

Post by pentilisea » Thursday 24th February 2022 12:34pm

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
Last edited by pentilisea on Friday 25th February 2022 3:41pm, edited 1 time in total.

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: loading text file into listbox with a filter

Post by AMUR-WOLF » Thursday 24th February 2022 4:54pm

pentilisea 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] ???
Try
Listbox1.Remove(0, 12)

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: loading text file into listbox with a filter

Post by AMUR-WOLF » Thursday 24th February 2022 5:31pm

pentilisea wrote:
Thursday 24th February 2022 12:34pm
Another approach would be to list only the numbers, only one number as one item/line.
Try
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

pentilisea
Posts: 6
Joined: Thursday 24th February 2022 12:18pm

Re: loading text file into listbox with a filter

Post by pentilisea » Thursday 24th February 2022 6:54pm

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 ...

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: loading text file into listbox with a filter

Post by AMUR-WOLF » Thursday 24th February 2022 7:13pm

Hi pentilisea,
happy to help.
pentilisea wrote:
Thursday 24th February 2022 6:54pm
Loading only numbers (isInteger) has still space befor number, space after number
Use the "Trim$" function:
Filtered.Add(Trim$(OriginLine))

pentilisea
Posts: 6
Joined: Thursday 24th February 2022 12:18pm

Re: loading text file into listbox with a filter

Post by pentilisea » Friday 25th February 2022 3:40pm

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

Post Reply