Converting Listbox to text

  1. 2 weeks ago

    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

  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 2 weeks 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 2 weeks 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 2 weeks 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. 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 Midlands of England, Europe
    Edited 2 weeks 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

or Sign Up to reply!