Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need help to show text lines from .txt file in listbox

Need help to show text lines from .txt file in listbox

Scheduled Pinned Locked Moved C#
helpcsharplinqgraphicstutorial
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dr_iton
    wrote on last edited by
    #1

    I created a text file using StreamWriter in a Form Application. I can't put a code to show the lines from a text file in a listbox. How to fix this problem.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace AplikacioniStudentet
    {
    public partial class FormaStudentet : Form
    {
    public FormaStudentet()
    {
    InitializeComponent();

        }
    
        private void Form1\_Load(object sender, EventArgs e)
      
        {
    
        }
    
        private void textBoxID\_TextChanged(object sender, EventArgs e)
        {
            string pathi = "Studentet.txt";
            using (FileStream fs = new FileStream(pathi, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (string line in textBoxID.Lines)
                        sw.Write(line + sw.NewLine);
                    sw.Close();
                }
            }
            
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            string pathi = "Studentet.txt";
            using (FileStream fs = new FileStream(pathi, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(textBoxID.Text + " " + textBoxEmri.Text + " " + textBoxMbiemri.Text + " ");
                    sw.Flush();
                    sw.Close();
                }
            }
           
            /\*TextWriter tw = new StreamWriter (pathi, true);
            tw.ID = ID;
            tw.emri = emri;
            tw.mbiemri = mbiemri;
    
    
            tw.Close();\*/
            
        }
    
        private void textBoxEmri\_TextChanged(object sender, EventArgs e)
        {
            string pathi = "Studentet.txt";
            using (FileStream fs = new FileStream(pathi, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    foreach (string line in textBoxEmri.Lines)
                        sw.Write(line + sw.NewLine);
                    sw.Close();
                }
            }
        }
    
        private void textBoxMbiemri\_TextChanged(object sender, EventArgs e)
        {
            string pathi = "Studentet.txt";
    
    L 1 Reply Last reply
    0
    • D dr_iton

      I created a text file using StreamWriter in a Form Application. I can't put a code to show the lines from a text file in a listbox. How to fix this problem.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.IO;

      namespace AplikacioniStudentet
      {
      public partial class FormaStudentet : Form
      {
      public FormaStudentet()
      {
      InitializeComponent();

          }
      
          private void Form1\_Load(object sender, EventArgs e)
        
          {
      
          }
      
          private void textBoxID\_TextChanged(object sender, EventArgs e)
          {
              string pathi = "Studentet.txt";
              using (FileStream fs = new FileStream(pathi, FileMode.Create))
              {
                  using (StreamWriter sw = new StreamWriter(fs))
                  {
                      foreach (string line in textBoxID.Lines)
                          sw.Write(line + sw.NewLine);
                      sw.Close();
                  }
              }
              
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              string pathi = "Studentet.txt";
              using (FileStream fs = new FileStream(pathi, FileMode.Create))
              {
                  using (StreamWriter sw = new StreamWriter(fs))
                  {
                      sw.WriteLine(textBoxID.Text + " " + textBoxEmri.Text + " " + textBoxMbiemri.Text + " ");
                      sw.Flush();
                      sw.Close();
                  }
              }
             
              /\*TextWriter tw = new StreamWriter (pathi, true);
              tw.ID = ID;
              tw.emri = emri;
              tw.mbiemri = mbiemri;
      
      
              tw.Close();\*/
              
          }
      
          private void textBoxEmri\_TextChanged(object sender, EventArgs e)
          {
              string pathi = "Studentet.txt";
              using (FileStream fs = new FileStream(pathi, FileMode.Create))
              {
                  using (StreamWriter sw = new StreamWriter(fs))
                  {
                      foreach (string line in textBoxEmri.Lines)
                          sw.Write(line + sw.NewLine);
                      sw.Close();
                  }
              }
          }
      
          private void textBoxMbiemri\_TextChanged(object sender, EventArgs e)
          {
              string pathi = "Studentet.txt";
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have created a new ListBox in the listBoxStudentat_SelectedIndexChanged method, but at the end of the routine you have let it get lost. You need to create a ListBox on your form and add the items to that. Are you sure you want to add items just at the point that the ListBox selection changes?

      One of these days I'm going to think of a really clever signature.

      D 1 Reply Last reply
      0
      • L Lost User

        You have created a new ListBox in the listBoxStudentat_SelectedIndexChanged method, but at the end of the routine you have let it get lost. You need to create a ListBox on your form and add the items to that. Are you sure you want to add items just at the point that the ListBox selection changes?

        One of these days I'm going to think of a really clever signature.

        D Offline
        D Offline
        dr_iton
        wrote on last edited by
        #3

        Yes, I want that my listbox has to show every line from text file, and also, I don't want that to get lost.

        L 1 Reply Last reply
        0
        • D dr_iton

          Yes, I want that my listbox has to show every line from text file, and also, I don't want that to get lost.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then you need to initialise it at the beginning of your program, not in the event handler for selection change. Just read the file and add each item from the file into the listbox that is on your form, don't create a new one that is not connected to anything.

          One of these days I'm going to think of a really clever signature.

          D 1 Reply Last reply
          0
          • L Lost User

            Then you need to initialise it at the beginning of your program, not in the event handler for selection change. Just read the file and add each item from the file into the listbox that is on your form, don't create a new one that is not connected to anything.

            One of these days I'm going to think of a really clever signature.

            D Offline
            D Offline
            dr_iton
            wrote on last edited by
            #5

            So you are saying that I nedd to create a text file only once, and after that i can manipulate with file details in my form calling them. Am I right or...

            L 1 Reply Last reply
            0
            • D dr_iton

              So you are saying that I nedd to create a text file only once, and after that i can manipulate with file details in my form calling them. Am I right or...

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              dr_iton wrote:

              So you are saying that I nedd to create a text file only once,

              No, I did not discuss creating a file, I was explaining how to fill your ListBox.

              dr_iton wrote:

              and after that i can manipulate with file details in my form calling them.

              Sorry, I have no idea what that is referring to. Your application should be something like:

              • During initialisation read your text file and build the ListBox from its contents.
              • As information is added or changed in the application you may make changes to the ListBox.
              • At program termination you should save the items from the ListBox if it has been modified.
              • Other processing as required.

              One of these days I'm going to think of a really clever signature.

              D 1 Reply Last reply
              0
              • L Lost User

                dr_iton wrote:

                So you are saying that I nedd to create a text file only once,

                No, I did not discuss creating a file, I was explaining how to fill your ListBox.

                dr_iton wrote:

                and after that i can manipulate with file details in my form calling them.

                Sorry, I have no idea what that is referring to. Your application should be something like:

                • During initialisation read your text file and build the ListBox from its contents.
                • As information is added or changed in the application you may make changes to the ListBox.
                • At program termination you should save the items from the ListBox if it has been modified.
                • Other processing as required.

                One of these days I'm going to think of a really clever signature.

                D Offline
                D Offline
                dr_iton
                wrote on last edited by
                #7

                I got the point now. If I can not post a solution tonight because I'll be busy, tomorrow I'll tell you if I solved my problem. Once again thank you for your reply. Cheers.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups