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. How to solve the following problem using StreamReader.

How to solve the following problem using StreamReader.

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelptutorial
11 Posts 3 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 have a problem reading lines and showing them from a text file in a ListBox using Windows Form Application. I made some code but now I'm stucked in a place where I made a comment.

    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 LlogariaBankare
    {
    public partial class KlientForm : Form
    {
    public string pathi = "Klientet.txt";
    public KlientForm()
    {
    InitializeComponent();
    }

        private void KlientForm\_Load(object sender, EventArgs e)
        {
            this.ShtoButon.Enabled = true;
            this.AzhuroButon.Enabled = false;
            this.FshijButon.Enabled = false;
    
        }
        
    
        private void ShtoButon\_Click(object sender, EventArgs e)
        {
                    
            StreamWriter swKlientet = new StreamWriter(pathi, true);
            swKlientet.WriteLine(emriBox.Text + "\\t" + mbiemriBox.Text + "\\t" +
                datelindjaBox.Text + "\\t" + vendlindjaBox.Text + "\\t" +
                xhirollogariaBox.Text + "\\t");
            swKlientet.Close();
        
        }
    
        private void ListBox\_SelectedIndexChanged(object sender, EventArgs e)
        {
          /\* what code i have to write here to read lines from text file and show the lines here\*/
    
        }
    }
    

    }

    J L 2 Replies Last reply
    0
    • D dr_iton

      I have a problem reading lines and showing them from a text file in a ListBox using Windows Form Application. I made some code but now I'm stucked in a place where I made a comment.

      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 LlogariaBankare
      {
      public partial class KlientForm : Form
      {
      public string pathi = "Klientet.txt";
      public KlientForm()
      {
      InitializeComponent();
      }

          private void KlientForm\_Load(object sender, EventArgs e)
          {
              this.ShtoButon.Enabled = true;
              this.AzhuroButon.Enabled = false;
              this.FshijButon.Enabled = false;
      
          }
          
      
          private void ShtoButon\_Click(object sender, EventArgs e)
          {
                      
              StreamWriter swKlientet = new StreamWriter(pathi, true);
              swKlientet.WriteLine(emriBox.Text + "\\t" + mbiemriBox.Text + "\\t" +
                  datelindjaBox.Text + "\\t" + vendlindjaBox.Text + "\\t" +
                  xhirollogariaBox.Text + "\\t");
              swKlientet.Close();
          
          }
      
          private void ListBox\_SelectedIndexChanged(object sender, EventArgs e)
          {
            /\* what code i have to write here to read lines from text file and show the lines here\*/
      
          }
      }
      

      }

      J Offline
      J Offline
      Jibesh
      wrote on last edited by
      #2

      like StreamWriter you can use StreamReader to read text files. Check the examples here MSDN StremReader class[^]

      Jibesh.V.P India

      1 Reply Last reply
      0
      • D dr_iton

        I have a problem reading lines and showing them from a text file in a ListBox using Windows Form Application. I made some code but now I'm stucked in a place where I made a comment.

        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 LlogariaBankare
        {
        public partial class KlientForm : Form
        {
        public string pathi = "Klientet.txt";
        public KlientForm()
        {
        InitializeComponent();
        }

            private void KlientForm\_Load(object sender, EventArgs e)
            {
                this.ShtoButon.Enabled = true;
                this.AzhuroButon.Enabled = false;
                this.FshijButon.Enabled = false;
        
            }
            
        
            private void ShtoButon\_Click(object sender, EventArgs e)
            {
                        
                StreamWriter swKlientet = new StreamWriter(pathi, true);
                swKlientet.WriteLine(emriBox.Text + "\\t" + mbiemriBox.Text + "\\t" +
                    datelindjaBox.Text + "\\t" + vendlindjaBox.Text + "\\t" +
                    xhirollogariaBox.Text + "\\t");
                swKlientet.Close();
            
            }
        
            private void ListBox\_SelectedIndexChanged(object sender, EventArgs e)
            {
              /\* what code i have to write here to read lines from text file and show the lines here\*/
        
            }
        }
        

        }

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

        You should not be putting the file reading code in the SelectedIndexChanged event or it will be repeated every time the selection changes. You should initialise your ListBox after the form is first loaded, or in some other method.

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

        D 1 Reply Last reply
        0
        • L Lost User

          You should not be putting the file reading code in the SelectedIndexChanged event or it will be repeated every time the selection changes. You should initialise your ListBox after the form is first loaded, or in some other method.

          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
          #4

          OK, now i konw how to read a text file and put it in a listbox, but the problem is that when I type a new line it doesn't show me that new line in Listbox until i debug again the form. I solved the problem creating a method on form load. Do I need to create a new method to reload the text file again or what ever. Cheers in advance for your reply.

          L 1 Reply Last reply
          0
          • D dr_iton

            OK, now i konw how to read a text file and put it in a listbox, but the problem is that when I type a new line it doesn't show me that new line in Listbox until i debug again the form. I solved the problem creating a method on form load. Do I need to create a new method to reload the text file again or what ever. Cheers in advance for your reply.

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

            dr_iton wrote:

            Do I need to create a new method to reload the text file again or what ever.

            Not necessarily, but you do need to create a method that can add any extra information as it is entered into the program. These controls can be updated at any time by adding or removing entries from them, you do not need to reload them from the beginning.

            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:

              Do I need to create a new method to reload the text file again or what ever.

              Not necessarily, but you do need to create a method that can add any extra information as it is entered into the program. These controls can be updated at any time by adding or removing entries from them, you do not need to reload them from the beginning.

              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
              #6

              After all I have the following problem. I can remove the lines from listbox, but those lines are not deleted from text file, how to delete a specific line from a text file. Cheers in advance for your reply.

              L 1 Reply Last reply
              0
              • D dr_iton

                After all I have the following problem. I can remove the lines from listbox, but those lines are not deleted from text file, how to delete a specific line from a text file. Cheers in advance for your reply.

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

                dr_iton wrote:

                I can remove the lines from listbox, but those lines are not deleted from text file

                Why would you expect them to be, since there is no connection between the two? The easiest solution would be to recreate the text file from the contents of the ListBox, either when the content changes, or when the program terminates.

                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:

                  I can remove the lines from listbox, but those lines are not deleted from text file

                  Why would you expect them to be, since there is no connection between the two? The easiest solution would be to recreate the text file from the contents of the ListBox, either when the content changes, or when the program terminates.

                  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
                  #8

                  I created a method to show the items in ListBox in Form 1, like this

                  string linjaListBox = this.ListBox.SelectedItem.ToString();

                              string emriPjesa = linjaListBox.Split('\\t')\[0\];
                              string mbiemriPjesa = linjaListBox.Split('\\t')\[1\];
                              string datelindjaPjesa = linjaListBox.Split('\\t')\[2\];
                              string vendlindjaPjesa = linjaListBox.Split('\\t')\[3\];
                              string xhirollogariaPjesa = linjaListBox.Split('\\t')\[4\];
                             
                  
                              this.emriBox.Text = emriPjesa;
                              this.mbiemriBox.Text = mbiemriPjesa;
                              this.datelindjaBox.Text = datelindjaPjesa;
                              this.vendlindjaBox.Text = vendlindjaPjesa;
                              this.xhirollogariaBox.Text = xhirollogariaPjesa;
                              indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
                  

                  In the next form I used three of TextBoxes from first Form such as emriBox,mbiemriBox and xhirollogariaBox. How to show the text in those three TextBoxes in form2 if I select a row in ListBox from Form1.

                  L 1 Reply Last reply
                  0
                  • D dr_iton

                    I created a method to show the items in ListBox in Form 1, like this

                    string linjaListBox = this.ListBox.SelectedItem.ToString();

                                string emriPjesa = linjaListBox.Split('\\t')\[0\];
                                string mbiemriPjesa = linjaListBox.Split('\\t')\[1\];
                                string datelindjaPjesa = linjaListBox.Split('\\t')\[2\];
                                string vendlindjaPjesa = linjaListBox.Split('\\t')\[3\];
                                string xhirollogariaPjesa = linjaListBox.Split('\\t')\[4\];
                               
                    
                                this.emriBox.Text = emriPjesa;
                                this.mbiemriBox.Text = mbiemriPjesa;
                                this.datelindjaBox.Text = datelindjaPjesa;
                                this.vendlindjaBox.Text = vendlindjaPjesa;
                                this.xhirollogariaBox.Text = xhirollogariaPjesa;
                                indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
                    

                    In the next form I used three of TextBoxes from first Form such as emriBox,mbiemriBox and xhirollogariaBox. How to show the text in those three TextBoxes in form2 if I select a row in ListBox from Form1.

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

                    Why not just:

                    string linjaListBox = this.ListBox.SelectedItem.ToString();

                    string[] items = linjaListBox.Split('\t');

                    this.emriBox.Text = items[0];
                    this.mbiemriBox.Text = items[1];
                    this.datelindjaBox.Text = items[2];
                    this.vendlindjaBox.Text = items[3];
                    this.xhirollogariaBox.Text = items[04];
                    // why convert to a string just to convert back to integer?
                    // indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
                    // do this instead
                    indeksLista = ListBox.SelectedIndex;

                    // to show in Form2 :
                    Form2.emriBox.Text = items[0];
                    Form2.mbiemriBox.Text = items[1];
                    Form2.xhirollogariaBox.Text = items[4];

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

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      Why not just:

                      string linjaListBox = this.ListBox.SelectedItem.ToString();

                      string[] items = linjaListBox.Split('\t');

                      this.emriBox.Text = items[0];
                      this.mbiemriBox.Text = items[1];
                      this.datelindjaBox.Text = items[2];
                      this.vendlindjaBox.Text = items[3];
                      this.xhirollogariaBox.Text = items[04];
                      // why convert to a string just to convert back to integer?
                      // indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
                      // do this instead
                      indeksLista = ListBox.SelectedIndex;

                      // to show in Form2 :
                      Form2.emriBox.Text = items[0];
                      Form2.mbiemriBox.Text = items[1];
                      Form2.xhirollogariaBox.Text = items[4];

                      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
                      #10

                      OK, my Form2 is named GjendjaForm, and after I rename Form2 with GjendjaForm it shows me the following errors. GjendjaForm.emriBox is inaccessible due to its protection level GjendjaForm.mbiemriBox is inaccessible due to its protection level GjendjaForm.xhirollogariaBox is inaccessible due to its protection level also it says that: Error 3 An object reference is required for the non-static field, method, or property 'LlogariaBankare.GjendjaForm.mbiemriBox' C:\Users\Driton Gashi\Documents\Visual Studio 2008\Projects\LlogariaBankare\LlogariaBankare\KlientetForm.cs 112 17 LlogariaBankare

                      L 1 Reply Last reply
                      0
                      • D dr_iton

                        OK, my Form2 is named GjendjaForm, and after I rename Form2 with GjendjaForm it shows me the following errors. GjendjaForm.emriBox is inaccessible due to its protection level GjendjaForm.mbiemriBox is inaccessible due to its protection level GjendjaForm.xhirollogariaBox is inaccessible due to its protection level also it says that: Error 3 An object reference is required for the non-static field, method, or property 'LlogariaBankare.GjendjaForm.mbiemriBox' C:\Users\Driton Gashi\Documents\Visual Studio 2008\Projects\LlogariaBankare\LlogariaBankare\KlientetForm.cs 112 17 LlogariaBankare

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

                        You need to make your textboxes public, or add some properties or delegate events that allow the transfer of data between the two classes. In the second message you are trying to call a non-static method of the class without an object reference. This is basic C# object handling which you really need to understand before you go much further in this development project. Take a look at these samples[^] for further details.

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

                        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