Windows Develop Bookmark and Share   
 index > Windows Forms General > C# How do I load a .txt document into ListView after a button
 

C# How do I load a .txt document into ListView after a button

Ok I'm trying to make a program so that when they click a button it will bring up a browser and have them locate the .txt document, and after they found it the information in the document will be sent into a listview grid if anyone can help me ASAP I would appreciate it a lot.

Sincerely,
rbhavocpzr
  • Moved byeryangMSFTTuesday, October 06, 2009 8:23 AM (From:.NET Base Class Library)
  •  
rbhavoc  Saturday, October 03, 2009 12:51 AM

Simply invoke this method in the click event of a button:

        /// <summary>
        /// Loads the text into a list view.
        /// </summary>
        /// <param name="listView">The list view to load the text into.</param>
        private void LoadTextIntoListView(ListView listView)
        {
            // Ask the user to select a file
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // Clear listview
                listView.Items.Clear();

                // Load text
                using (TextReader textReader = new StreamReader(openFileDialog.FileName))
                {
                    // Load the text line by line
                    string line = string.Empty;
                    while ((line = textReader.ReadLine()) != null)
                    {
                        // Append
                        listView.Items.Add(line);
                    }
                }
            }
        }
For example:

        private void button1_Click(object sender, EventArgs e)
        {
            LoadTextIntoListView(listView1);
        }

Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Sunday, October 04, 2009 9:15 AM
which listview?
If you mean the Windows Forms listview, post to the Windows Forms General forum
If you mean the WPF listview, post to the WPF forum
If you mean the asp.net listview, post to forums.asp.net


The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Saturday, October 03, 2009 2:03 AM
can someone move this to the windows forms general forum
rbhavoc  Saturday, October 03, 2009 2:05 AM

Simply invoke this method in the click event of a button:

        /// <summary>
        /// Loads the text into a list view.
        /// </summary>
        /// <param name="listView">The list view to load the text into.</param>
        private void LoadTextIntoListView(ListView listView)
        {
            // Ask the user to select a file
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // Clear listview
                listView.Items.Clear();

                // Load text
                using (TextReader textReader = new StreamReader(openFileDialog.FileName))
                {
                    // Load the text line by line
                    string line = string.Empty;
                    while ((line = textReader.ReadLine()) != null)
                    {
                        // Append
                        listView.Items.Add(line);
                    }
                }
            }
        }
For example:

        private void button1_Click(object sender, EventArgs e)
        {
            LoadTextIntoListView(listView1);
        }

Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Sunday, October 04, 2009 9:15 AM

You can use google to search for other answers

Custom Search

More Threads

• Bind form location to external process
• Application Icon
• open a new window using javascript
• Problem with TreeView
• Visual heritance bug in Visual Studio 2008
• Vista Aero small preview window in c#?
• Something called after apllication finalizers
• using a simple app from a network server...
• cross-thread UI updating?
• Display Problem: Unicode & RichTextBox