Reputation: 109
My apologies in advance if i use the wrong terminology, please fell free to ask me to clarify if anything seems confusing in this question
I'm trying to write a program in C# that does 3 things: adds 4 text fields to a listbox, edits any or all the fields in one record of the listbox, deletes all 4 fields (in a single record)
I am storing the data in a text file "test1.txt" and using StreamWriter to write the file and StreamReader to read it. Ive managed to add records but I'm not getting it to delete or edit records this is what my code looks like:
string path = "test1.txt";
int index = -1;
public Form1()
{InitializeComponent();}
private void Form1_Load(object sender, EventArgs e)
{
readFile();
}
private void readFile()
{
displayEventsBox.Items.Clear();
StreamReader sr = new StreamReader(path);
string record = sr.ReadLine();
{
displayEventsBox.Items.Add(record);
record = sr.ReadLine();
}
sr.Close();
}
private void displayEventsBox_SelectedIndexChanged(object sender, EventArgs e)
{
index = displayEventsBox.SelectedIndex;
if (index > -1)
{
string record = displayEventsBox.Items[index].ToString();
char[] delim = { ',' };
string[] tokens = record.Split(delim);
txtTaskName.Text = tokens[0];
txtTaskDescription.Text = tokens[1];
txtDateCreated.Text = tokens[2];
txtDateCompleted.Text = tokens[3];
}
}
private void butAddTask_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine(txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text);
sw.Close();
readFile();
clearText();}
private void butEditTask_Click(object sender, EventArgs e)
{
File.Delete(path);
StreamWriter sw = new StreamWriter(path, true);
for (int i = 0; i < displayEventsBox.Items.Count; i++)
{
if (i != index)
{
sw.WriteLine(displayEventsBox.Items[i].ToString());
}
else
{
sw.WriteLine(txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text);
}
}
}
private void butDeleteTask_Click(object sender, EventArgs e)
{
File.Delete(path);
StreamWriter sw = new StreamWriter(path, true);
for (int i = 0; i < displayEventsBox.Items.Count; i++)
{
if (i != index)// write from listbox
{
sw.WriteLine(displayEventsBox.Items[i].ToString());
}
}
sw.Close();
readFile();
}
could someone please help me identify why it wont let me delete or update records
In case it could help clarify here is a link to everything in the code http://collabedit.com/83rev
Upvotes: 1
Views: 948
Reputation: 10781
Use File.ReadAllLines
and File.WriteAllLines
will make your life easier. In your read method, File.ReadAllLines
should make that straightforward. Then in your delete method, rather than writing to file and then updating the listbox, I'd do it in the opposite order. First just delete the item from the listbox (listBox1.Items.Remove(listBox1.SelectedItem);
), and then write the rest out to file File.WriteAllLines(filename, listBox1.Items.Cast<string>());
. Then there's no need for the call to readFile()
at the end since you already updated the listbox. Similar for add and edit.
Also since none of the other functions are calling readFile
anymore, you can just put all that code directly into Form1_Load
. Also you can just save once when the form is closing.
Here are the functions after fixing them up.
private void Form1_Load(object sender, EventArgs e) {
displayEventsBox.Items.AddRange(File.ReadAllLines(path));
}
string Format() {
return txtTaskName.Text + "," + txtTaskDescription.Text + "," + txtDateCreated.Text + "," + txtDateCompleted.Text;
}
private void butAddTask_Click(object sender, EventArgs e) {
displayEventsBox.Items.Add(Format());
}
private void butDeleteTask_Click(object sender, EventArgs e) {
displayEventsBox.Items.RemoveAt(displayEventsBox.SelectedIndex);
}
private void butEditTask_Click(object sender, EventArgs e) {
displayEventsBox.Items[displayEventsBox.SelectedIndex] = Format();
}
protected override void OnClosing(CancelEventArgs e) {
File.WriteAllLines(path, displayEventsBox.Items.Cast<string>());
base.OnClosing(e);
}
Upvotes: 1
Reputation: 43300
Like I said, your problem is probably that readfile
only ever loads one item, try
using(StreamReader sr = new StreamReader(path))
{
string record;
while((record = sr.ReadLine()) != null)
{
displayEventsBox.Items.Add(record);
}
}
Apart from that you shouldn't care to load from this once you've loaded once. instead to add an item...
just try the following.
string s = txtTaskName.Text + "," + txtTaskDescription.Text + ","
+ txtDateCreated.Text + "," + txtDateCompleted.Text;
displayEventsBox.Items.Add(s);
There will be similar methods for edit, delete, etc
Heres a breakdown of your current method...
string record = sr.ReadLine(); //read a line
{ //Open a block that doesn't do much
displayEventsBox.Items.Add(record); //Add the item
record = sr.ReadLine(); //Read the next item but ignore it
} //Close the random block
Upvotes: 2