none
How to show one string of row at a time in a textbox RRS feed

  • Question

  • So, this is what i've done so far, but it shows the whole file at once instead of showing one string row at time.

    I have only one textbox which must like a book list like this "Origins" by Dan Brown. (Novel) with each click.

    My imported file has a long list of books but this program shows a whole list in one single line instead.

    I'm thinking of using random to get a random list, but i have difficulty figuring it out.

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class FileLoader : Form { List<string> itemSaver = new List<string>(); public FileLoader() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.button1.Text = "Browse!"; var sb = new StringBuilder(); string item; if (File.Exists("texter.txt")) { StreamReader reader = new StreamReader("texter.txt", Encoding.Default, false); while ((item = reader.ReadLine()) != null) { itemSaver.Add(item); } } foreach (string a in itemSaver) { string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None); sb.AppendLine(string.Format("\"{0}\" by {1}. ({2})", vektor)); } this.textBox1.AppendText(sb.ToString()); } } }


    Thursday, February 6, 2020 3:46 PM

All replies

  • See updated code here

    https://github.com/karenpayneoregon/AsynchronousSharp/tree/master/ForumQuestion1


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 6, 2020 4:10 PM
    Moderator

  • My imported file has a long list of books but this program shows a whole list in one single line instead.


    Show a sample of several lines from the input file.

    - Wayne

    Thursday, February 6, 2020 6:20 PM
  • This is a sample of the result when i clicked the button in one whole line :

    by Publisher Joakim Svensson. (Journal)"Marvel Life" by August Strindberg. (Short stories)"A barrel of amontillado" by Edgar Allan Poe. (Short stories)

    instead of showing one book in each click: (Is this what you mean?)

    "Marvel Life" by August Strindberg. (Short stories)

    "A barrel of amontillado" by Edgar Allan Poe. (Short stories)

    Thursday, February 6, 2020 6:36 PM

  • this program shows a whole list in one single line instead.


    Does your textbox have the multiline property set to True?

    - Wayne

    Thursday, February 6, 2020 6:36 PM
  • No, it doesn't have.

    Thursday, February 6, 2020 6:42 PM

  • instead of showing one book in each click: (Is this what you mean?)


    No. I asked for "a sample of several lines from the input file".

    Show the file contents. Not the textbox contents.

    As written, the code you posted will process all of the lines from the file
    in a single button click. It will show all of the lines from the file on
    separate lines if the textbox Multiline property is set to True. If it's 
    False then the program will show all of the lines from the file on a single
    line in the textbox.

    - Wayne


    • Edited by WayneAKing Thursday, February 6, 2020 6:49 PM
    Thursday, February 6, 2020 6:48 PM

  • instead of showing one book in each click:

    You need to rethink your logic. You have put everything in the button click
    event. So every time you click the button the program will:

    Read all of the lines from the file and put them into a List of Strings.

    Split each string in the List into an array of strings, format each array
    of strings into a string for displaying which it then adds to a StringBuilder
    object.

    After all strings in the List (which means all of the lines in the file) are
    formatted and placed in the StringBuilder object, the button click event then
    appends the entire formatted file contents to the textbox.

    If you have the Multiline property set to True you will see each line from the
    file on a separate line after a button click. You will also see them repeated
    each time you click the button, as you don't clear the textbox.

    If you want to show one line at a time with each click of the button then you
    need to rearrange your code so that it processes only one line at a time.
    This can be done in a number of different ways.

    One way is to read all of the lines from the file the first time the button
    is clicked *only* - storing them into a List of Strings as you have done.
    Then you process one line *only* from that List on each button click, keeping
    track between clicks of where you are in the List (the current index).

    Another way is to read one line at a time from the file on each button click
    and process that line only.

    I/we could easily modify the code for you, but as this clearly is a learning
    exercise or assignment it will be far better for you - and more ethical - if
    we allow you to correct your own code.

    - Wayne

    Thursday, February 6, 2020 7:18 PM
  • Thank you for the advice.  I will look into this multiline property you speak of.  Yes this is a lesson and it's particularly difficult studying distance on my own so i appreciate any help i could get, thank you very much.

    -Morena

    Thursday, February 6, 2020 7:27 PM