Converting Listbox to text

  1. 5 months ago

    Arief S

    Feb 9 Bandung - Indonesia

    Hi,
    is it possible to remove duplicate name in listbox into text file ?

    I have a Listbox data,

    Fruits Orange 1
    Fruits Apple 1
    Fruits Banana 1
    Cheese Fetta 1
    Cheese Mozarella 1
    Cheese Gouda 1
    Cheese Parmesan 1

    I am using the endofline method, but the result is not what I am expected.

    The result is,

    Fruits
    Orange 1
    Fruits
    Apple 1
    Fruits
    Banana 1
    Cheese
    Fetta 1
    Cheese
    Mozarella 1
    Cheese
    Gouda 1
    Cheese
    Parmesan 1

    What I am expected is,

    Fruits
    Orange 1
    Apple 1
    Banana 1
    Cheese
    Fetta 1
    Mozarella 1
    Gouda 1
    Parmesan 1

    Any Help ?

    Thanks
    Regards,
    Arief

    One approach : keep a record of THIS_first_word and LAST_first_word and only output when they are different?
    This is not the whole code, you still need to do some work.

    if  listBox1.cell(i,0)  <> LAST_first_word  then
     tisx.writeline listBox1.cell(i,0)
     tisx.writeline listBox1.cell(i,1)+" "+listBox1.cell(i,2)
     LAST_first_word = listBox1.cell(i,0)
    else
     tisx.writeline listBox1.cell(i,1)+" "+listBox1.cell(i,2)
    end if
  2. Emile S

    Feb 10 Europe (France, Strasbourg)

    @Arief S. I am using the endofline method

    Please, define what you mean.

  3. Emile S

    Feb 10 Europe (France, Strasbourg)

    Jérôme Leray posted 15 minutes ago…
    But where is the post ?

  4. Markus W

    Feb 10 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...
    Edited 5 months ago

    Yes, it is. But learning to program means wrapping your head around a problem and finding a way to solve it with the computer. And this is a rather simple problem, so it should not take you long to figure out.

    Tip: read up on Listbox.Cell and InStr or for a more advanced solution on dictionaries.

    P.S. A ListBox is similar to a spreadsheet. Cells are separated by tabs, and rows are separated by EndOfLine.

  5. Alberto D

    Feb 10 Pre-Release Testers, Xojo Pro
    Edited 5 months ago

    Good tips Markus. I'll try to find a solution. I visit the forum to find problems so I can learn Xojo by finding a solution.

    Arief, your listbox is single column, 2 or 3 columns? I'll start with 3 columns for my test.

    Edit: I copy/paste Arief listbox and it has tabs between the words, so I guess 3 columns.

  6. Emile S

    Feb 10 Europe (France, Strasbourg)

    Two or more columns: same solution. The difference is in 1 to many columns…

    To read a Row, look ListBox.Cell .

    To write a Row, use AddRow (or AddFolder) and eventually use ListBox.Cell to populate the cells of the just added Row.

    On the other hand, if you have enough trouble(s) to not been able to formulate a question, you are in big troubles.

    Stop, take aminute (or two) try to put your ideas on screen (paper) and at last, explain what you want to do and you may not need to ask question here (but feel free to formulate understandable question(s) here when needed).

  7. create multi dimension array or collection of class objects where fruits and cheese are main groups in which child items would be other, product items from list where in your case listbox is data source.

    After you create such array or collection's of class objects then you can create any output you like and reuse it even to reload/refresh listbox and/or any other control on GUI.

    simple example would be

    Product Group Class Object
    Properties

    • (optional and better) Group ID
    • Group Name
    • Products [ ] As Collection of Product Items

    Method

    • CreateList() As Integer
    • CreateOutput(Filename) As Integer
    • RenderListToListBox()

    Product Item Class Object
    Properties

    • Product Name
    • Qty
    • Group Name (or ID)

    Method

    • (optional) CreateOutput() As String
    • (optional) RenderItemToListBox()

    You can also add sort index and for group of products and for products which can be used to point which product or group will be on top of the list (so called ranking too) :)

    ...and after all it's better to have defined data source and then from it to create and manipulate GUI (in your case listbox control) where you will always take care of data source first which later your going to use for other things such as output to file and in mean while to render to GUI (in your situation listbox) when it's need for it.

  8. Markus W

    Feb 10 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

    @Bogdan P create multi dimension array or collection of class objects where fruits and cheese are main groups in which child items would be other, product items from list where in your case listbox is data source.

    Too advanced, Bogdan (and I’m not even sure it is a suitable solution to his problem). This is more confusing than helpful.

    Let Arief try to figure it out first. Even if it is “just” specifying how he would do it step by step in plain English. After all that’s how you formulate your method for a solution, or as it’s called for computers an algorithm. Then he needs to translate that method into computer speak.

    If he has figured out a step by step approach, THEN he will have specific questions that we can help him with.

    [and sure, we could give him a solution … but where is the fun in that?]

  9. Markus W

    Feb 10 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...
    Edited 5 months ago

    @Arief: write a step by step description of how you would solve this if you did not have a computer, just a pen and paper. That breaks the problem doiwn into small steps that can be transformed into an algorithm that can be put into computer code. Feel free to post that description here, because then we can see where the problem really is and help you become a better programmer. After all, the aim is not to solve this particular problem, but to become more proficient at programming.

    Or in other terms: “Give a man a fish, and he won’t be hungry today. But teach him to catch fish, and he will never be hungry again”. The aim has to be to teach you fishing.

  10. Emile S

    Feb 10 Europe (France, Strasbourg)

    Since the original question, nearly one days have ellapsed and the op (Arief) do not make another appearance.

    So, what happens Arief ?

  11. Arief S

    Feb 11 Bandung - Indonesia

    hi,
    sorry, I dont know how to explained nicely in english. Basically I want to remove repeated cell text, if its has the same name.

      Listbox1.AddRow "Fruits", "Orange", "1"
      Listbox1.AddRow "Fruits", "Apple", "1"
      Listbox1.AddRow "Fruits", "Banana", "1"
      
      Listbox1.AddRow "Cheese", "Fetta", "1"
      Listbox1.AddRow "Cheese", "Mozarella", "1"
      Listbox1.AddRow "Cheese", "Gouda", "1"
      Listbox1.AddRow "Cheese", "Parmesan", "1"
      
      
      if Listbox1.ListCount>0 then
        dim f as folderitem
        dim tisx as TextOutputStream
        f = new folderitem("item.txt")
        tisx = f.CreateTextFile
        
        dim i as integer
        for i=0 to listBox1.listcount-1
        tisx.writeline listBox1.cell(i,0)+EndOfLine+listBox1.cell(i,1)+" "+listBox1.cell(i,2)
        next
        tisx.WriteLine " "
        tisx.Close
      end if

    The results shows :

    Fruits
    Orange 1
    Fruits
    Apple 1
    Fruits
    Banana 1
    Cheese
    Fetta 1
    Cheese
    Mozarella 1
    Cheese
    Gouda 1
    Cheese
    Parmesan 1

    I want to remove the duplicate text based from cell 0, so the result must be like this :

    Fruits
    Orange 1
    Apple 1
    Banana 1
    
    Cheese
    Fetta 1
    Mozarella 1
    Gouda 1
    Parmesan 1

    Thanks
    Regards,
    Arief

  12. Jeff T

    Feb 11 Answer Midlands of England, Europe
    Edited 5 months ago

    One approach : keep a record of THIS_first_word and LAST_first_word and only output when they are different?
    This is not the whole code, you still need to do some work.

    if  listBox1.cell(i,0)  <> LAST_first_word  then
     tisx.writeline listBox1.cell(i,0)
     tisx.writeline listBox1.cell(i,1)+" "+listBox1.cell(i,2)
     LAST_first_word = listBox1.cell(i,0)
    else
     tisx.writeline listBox1.cell(i,1)+" "+listBox1.cell(i,2)
    end if
  13. Arief S

    Feb 27 Bandung - Indonesia

    Hi Mr. Tullin,

    What I am trying hard so far, its seems closely with the goals, the method that you are giving, really never know it, so I am using double listbox array.

    I dont know why the items is write looping.

      Listbox1.AddRow "Fruits", "Orange", "1"
      Listbox1.AddRow "Fruits", "Apple", "1"
      Listbox1.AddRow "Fruits", "Banana", "1"
      
      Listbox1.AddRow "Cheese", "Fetta", "1"
      Listbox1.AddRow "Cheese", "Mozarella", "1"
      Listbox1.AddRow "Cheese", "Gouda", "1"
      Listbox1.AddRow "Cheese", "Parmesan", "1"
      
      
      dim f as folderitem
      dim tisx as TextOutputStream
      f = new folderitem("item.txt")
      tisx = f.CreateTextFile
      
      
      dim i as integer
      dim j as integer
      For i=0 To Listbox1.ListCount-1
        For j=0 To Listbox1.ListCount-1
          If i=j Then
            tisx.writeline listBox1.cell(i,0)
            tisx.WriteLine " "
          Else
            If Listbox1.Cell(i,0)=Listbox1.Cell(j,0) Then
              tisx.writeline listBox1.cell(j,1)+" "+listBox1.cell(j,2)
            End if
          End if
        Next j
        tisx.WriteLine " "
      Next i
      
      tisx.Close
      

    which code that need to be fixed ?
    any help?

    Thanks,
    Regards,
    Arief

  14. Alberto D

    Feb 27 Pre-Release Testers, Xojo Pro

    This:

    For i=0 To Listbox1.ListCount-1
        For j=0 To Listbox1.ListCount-1
          If i=j Then

    will always be true when I and j are 0, 1, 2, 3,..., and j will always start at 0 and maybe you want it to start at least at i.

  15. Jeff T

    Feb 27 Midlands of England, Europe

    The code I gave you is close to what you need, but your code afterwards has ignored it.

    Think.

    Pretend there is an invisible first row where the first cell holds 'XXXXX'
    Set that as 'current code' (put it in a variable)

    Start at the top and work down.
    for each row:
    //============
    If the contents of cell 0 is different from the current code (XXXX) ,
    print the contents of cell 0
    update current code to the new value
    end if
    print cell 1 + cell 2
    //=================

  16. 3 months ago

    Arief S

    Apr 18 Bandung - Indonesia

    Dear Mr. Tullin,

    Thanks for the code,
    finally I made it with the sample code that you gave couple weeks ago.

      dim Last_first_word as String
      dim maxRow as Integer = Listbox1.listcount-1
      for row as integer =  0 to maxRow
        if Listbox1.Cell(row,0)<> Last_first_word then
          tisx.WriteLine ""
          tisx.writeline listBox1.cell(row,0)
          tisx.writeline listBox1.cell(row,1)+" "+listBox1.cell(row,2)
          Last_first_word=Listbox1.Cell(row,0)
        else
          tisx.writeline listBox1.cell(row,1)+" "+listBox1.cell(row,2)
        end if
        
      next
      
      tisx.Close
      f.Launch

    Regards,
    Arief

or Sign Up to reply!