简体   繁体   中英

Load data from file and add to textbox C#

this is my text data in .txt file

G300#Logitech#Mouse
G500#Logitech#Mouse
G1010#Logitech#Mouse

and I want to read it and place it in text box I have tried but it just show the last line I want to write all the line

this is my code:

        List<string> lines = new List<string>();


        using (TextReader tr = new StreamReader(@"databarang.dat"))
        {
            string line;
            while ((line = tr.ReadLine()) != null)
            {
                lines.Add(line);
            }

            foreach (string s in lines)
            {
                txtOutput.Text = s + "\n";
            }
        }

and this is my layout program

and how can I split the '#' and write it in text box so in the text box it must be

G300 Logitech Mouse

G500 Logitech Mouse

G1010 Logitech Mouse

You can do as shown below.

在此处输入图片说明

This way you can be able to get the rows as you need.

Try using

txtOutput.AppendText(s + Environment.NewLine);

You can split a string based on the hash as shown below

string[] parts = "G300#Logitech#Mouse".Split('#');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM