blah blah blah is here! blah blah » Close

up1down
link

My code below is able to easily load textfile contents to a textBox, with Multiline.

FileInfo myFile = new FileInfo(@"D:\output.txt");
TextReader myData = myFile.OpenText();

textBox1.Text = myData.ReadToEnd();
myData.Close();

But when I try with ListBox, it fails to display anything. Why? How do I do the same with ListBox?

thanks.

last answered one year ago

4 answers

link

The ListBox uses an ObjectCollection of items rather than lines of text.

If the file contains line breaks for each item, I'd try:

string[] items = File.ReadAllLines(@"D:\output.txt");
listBox1.Items.Clear(); // if necessary
listBox1.Items.AddRange(items);
listBox1.SelectedIndex = 0;

thank you, it works perfectly.

up0down
link

is there a limit on the maximum number of rows that ListBox can display? I have data in the range of more than 20,000 rows, and it cannot display all of them. What is the limit then? thanks.

up0down
link

I don't know what the limit is but I'd have thought that you don't really want to display more than 1000 items in the list box at once if you're expecting the user to have to scroll through them all.

I'd be inclined to implement a paging system with a Next and Prev button to navigate between pages and a label to display the current page.

up0down
link

thanks for your comment. It seems that Excel is more suitable for my huge dataset.

Feedback