Need help to show text lines from .txt file in listbox
-
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";
-
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";
You have created a new
ListBox
in thelistBoxStudentat_SelectedIndexChanged
method, but at the end of the routine you have let it get lost. You need to create aListBox
on your form and add the items to that. Are you sure you want to add items just at the point that theListBox
selection changes?One of these days I'm going to think of a really clever signature.
-
You have created a new
ListBox
in thelistBoxStudentat_SelectedIndexChanged
method, but at the end of the routine you have let it get lost. You need to create aListBox
on your form and add the items to that. Are you sure you want to add items just at the point that theListBox
selection changes?One of these days I'm going to think of a really clever signature.
-
Yes, I want that my listbox has to show every line from text file, and also, I don't want that to get lost.
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.
-
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.
-
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...
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.
- During initialisation read your text file and build the
-
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.
- During initialisation read your text file and build the