Editing text file when updating row
I am able to read my text file however when I go and click my edit button it moves all the current rows in the text file to the top row and doesn't update anything. Also how would I go about adding a row to the text file without moving the rows?
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", false);
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.Write(Bugs[i].BugsDesc);
}
sw.Close();
}
}
c#
add a comment |
I am able to read my text file however when I go and click my edit button it moves all the current rows in the text file to the top row and doesn't update anything. Also how would I go about adding a row to the text file without moving the rows?
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", false);
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.Write(Bugs[i].BugsDesc);
}
sw.Close();
}
}
c#
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing isNane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27
add a comment |
I am able to read my text file however when I go and click my edit button it moves all the current rows in the text file to the top row and doesn't update anything. Also how would I go about adding a row to the text file without moving the rows?
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", false);
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.Write(Bugs[i].BugsDesc);
}
sw.Close();
}
}
c#
I am able to read my text file however when I go and click my edit button it moves all the current rows in the text file to the top row and doesn't update anything. Also how would I go about adding a row to the text file without moving the rows?
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", false);
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.Write(Bugs[i].BugsDesc);
}
sw.Close();
}
}
c#
c#
edited Nov 21 at 2:53
TheGeneral
26.9k63163
26.9k63163
asked Nov 21 at 2:51
SunShine1976
122
122
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing isNane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27
add a comment |
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing isNane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing is
Nane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing is
Nane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27
add a comment |
2 Answers
2
active
oldest
votes
The StreamWriter
object you are creating is having wrong parameter value for append. You need to set that as true
OR just remove that parameter since it’s default value is true
.
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", true);
OR
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
Here is the link from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2#System_IO_StreamWriter__ctor_System_String_System_Boolean_
You are also not using the using
statement, which ensures the StreamWriter
object is removed from memory when no longer needed. Please go through this article to understand it better.
https://www.dotnetperls.com/streamwriter
Hope this helps!
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
|
show 3 more comments
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace BugTracker
{
struct BugTrackers
{
public string BugsName;
public string BugsDesc;
}
public partial class YoungKidsBugTracker : Form
{
// Field to hold a list of BugTrackers objects
private List<BugTrackers> Bugs = new List<BugTrackers>();
private int index; // index fo selected bugs in combobox
public YoungKidsBugTracker()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
//Declare a varialble to hold Bugs Name
StreamReader inputFile; // To Read the file
string line; // To hold a line from the file
// Create an instance of the Bug Accounts
BugTrackers entry = new BugTrackers();
// Create a delimeter array
char delim = { ',' };
// Open the file and get a StreamReader Object
inputFile = File.OpenText("smBugs.txt");
// Read the file's contents
while (!inputFile.EndOfStream)
{
// Read a line from the file
line = inputFile.ReadLine();
// Tokenize the line
string tokens = line.Split(delim);
// Stores the tokens in the entry object
entry.BugsName = tokens[0];
entry.BugsDesc = tokens[1];
// Add the entry object to the combobox
Bugs.Add(entry);
}
// Close the File
inputFile.Close();
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
private void lstBugs_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the index of the sselected item
index = lstBugs.SelectedIndex;
// Display Bug Information
DisplayBugs();
}
private void DisplayBugs()
{
//Show Data
txtBugsName.Text = Bugs[index].BugsName;
rtxtBugDesc.Text = Bugs[index].BugsDesc.ToString();
}
private void YoungKidsBugTracker_Load(object sender, EventArgs e)
{
// Read the Bugs.txt file
ReadFile();
// Display Bug Information
BugNameDisplay();
}
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.WriteLine(Bugs[i].BugsDesc);
// sw.Write(Environment.NewLine);
}
sw.Close();
}
}
private void BugNameDisplay()
{
// Display the list of Bug Names in the List Control
foreach (BugTrackers entry in Bugs)
{
lstBugs.Items.Add(entry.BugsName );
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
This is the code in its entirety. I have a list box with 2 text boxes to hold the bug name and description. I have 3 buttons Add, Edit and Delete. If an item is selected from the listbox it will display the bugname and description. If the entry needs updated changes are made and will change the information needed. If a new bug is added you would use the add button same for the delete button.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404642%2fediting-text-file-when-updating-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The StreamWriter
object you are creating is having wrong parameter value for append. You need to set that as true
OR just remove that parameter since it’s default value is true
.
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", true);
OR
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
Here is the link from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2#System_IO_StreamWriter__ctor_System_String_System_Boolean_
You are also not using the using
statement, which ensures the StreamWriter
object is removed from memory when no longer needed. Please go through this article to understand it better.
https://www.dotnetperls.com/streamwriter
Hope this helps!
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
|
show 3 more comments
The StreamWriter
object you are creating is having wrong parameter value for append. You need to set that as true
OR just remove that parameter since it’s default value is true
.
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", true);
OR
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
Here is the link from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2#System_IO_StreamWriter__ctor_System_String_System_Boolean_
You are also not using the using
statement, which ensures the StreamWriter
object is removed from memory when no longer needed. Please go through this article to understand it better.
https://www.dotnetperls.com/streamwriter
Hope this helps!
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
|
show 3 more comments
The StreamWriter
object you are creating is having wrong parameter value for append. You need to set that as true
OR just remove that parameter since it’s default value is true
.
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", true);
OR
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
Here is the link from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2#System_IO_StreamWriter__ctor_System_String_System_Boolean_
You are also not using the using
statement, which ensures the StreamWriter
object is removed from memory when no longer needed. Please go through this article to understand it better.
https://www.dotnetperls.com/streamwriter
Hope this helps!
The StreamWriter
object you are creating is having wrong parameter value for append. You need to set that as true
OR just remove that parameter since it’s default value is true
.
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt", true);
OR
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
Here is the link from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.7.2#System_IO_StreamWriter__ctor_System_String_System_Boolean_
You are also not using the using
statement, which ensures the StreamWriter
object is removed from memory when no longer needed. Please go through this article to understand it better.
https://www.dotnetperls.com/streamwriter
Hope this helps!
answered Nov 21 at 3:25
dj79
1,311213
1,311213
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
|
show 3 more comments
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
When the Edit Button is selected it doesn't update the text File. Instead it moves all rows of entries into one single line. Why is that?'code' UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) { System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); } sw.Close(); } }
– SunShine1976
Nov 21 at 11:02
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
That’s because you are not adding line break in the for block. You need to add "sw.Write(Environment.NewLine);" BELOW "sw.Write(Bugs[i].BugsDesc);" within the "for loop".
– dj79
Nov 21 at 11:14
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Still not working correctly. It saves the file but isn't updating the row
– SunShine1976
Nov 21 at 20:31
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
Not sure what you mean by this. Plz provide sample of your input, expected output and output by code.
– dj79
Nov 22 at 2:15
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
What it should do is update the entry in the file and save it. So if I have a misspelled word or need to update the row it will with a button.'code' private void UpdateBugsInfo() { if (lstBugs.SelectedIndex > -1) {System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt"); for (int i = 0; i <= Bugs.Count - 1; i++) { sw.Write(Bugs[i].BugsName); sw.Write(","); sw.Write(Bugs[i].BugsDesc); sw.Write(Environment.NewLine); } sw.Close(); } }
– SunShine1976
Nov 23 at 23:45
|
show 3 more comments
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace BugTracker
{
struct BugTrackers
{
public string BugsName;
public string BugsDesc;
}
public partial class YoungKidsBugTracker : Form
{
// Field to hold a list of BugTrackers objects
private List<BugTrackers> Bugs = new List<BugTrackers>();
private int index; // index fo selected bugs in combobox
public YoungKidsBugTracker()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
//Declare a varialble to hold Bugs Name
StreamReader inputFile; // To Read the file
string line; // To hold a line from the file
// Create an instance of the Bug Accounts
BugTrackers entry = new BugTrackers();
// Create a delimeter array
char delim = { ',' };
// Open the file and get a StreamReader Object
inputFile = File.OpenText("smBugs.txt");
// Read the file's contents
while (!inputFile.EndOfStream)
{
// Read a line from the file
line = inputFile.ReadLine();
// Tokenize the line
string tokens = line.Split(delim);
// Stores the tokens in the entry object
entry.BugsName = tokens[0];
entry.BugsDesc = tokens[1];
// Add the entry object to the combobox
Bugs.Add(entry);
}
// Close the File
inputFile.Close();
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
private void lstBugs_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the index of the sselected item
index = lstBugs.SelectedIndex;
// Display Bug Information
DisplayBugs();
}
private void DisplayBugs()
{
//Show Data
txtBugsName.Text = Bugs[index].BugsName;
rtxtBugDesc.Text = Bugs[index].BugsDesc.ToString();
}
private void YoungKidsBugTracker_Load(object sender, EventArgs e)
{
// Read the Bugs.txt file
ReadFile();
// Display Bug Information
BugNameDisplay();
}
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.WriteLine(Bugs[i].BugsDesc);
// sw.Write(Environment.NewLine);
}
sw.Close();
}
}
private void BugNameDisplay()
{
// Display the list of Bug Names in the List Control
foreach (BugTrackers entry in Bugs)
{
lstBugs.Items.Add(entry.BugsName );
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
This is the code in its entirety. I have a list box with 2 text boxes to hold the bug name and description. I have 3 buttons Add, Edit and Delete. If an item is selected from the listbox it will display the bugname and description. If the entry needs updated changes are made and will change the information needed. If a new bug is added you would use the add button same for the delete button.
add 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.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace BugTracker
{
struct BugTrackers
{
public string BugsName;
public string BugsDesc;
}
public partial class YoungKidsBugTracker : Form
{
// Field to hold a list of BugTrackers objects
private List<BugTrackers> Bugs = new List<BugTrackers>();
private int index; // index fo selected bugs in combobox
public YoungKidsBugTracker()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
//Declare a varialble to hold Bugs Name
StreamReader inputFile; // To Read the file
string line; // To hold a line from the file
// Create an instance of the Bug Accounts
BugTrackers entry = new BugTrackers();
// Create a delimeter array
char delim = { ',' };
// Open the file and get a StreamReader Object
inputFile = File.OpenText("smBugs.txt");
// Read the file's contents
while (!inputFile.EndOfStream)
{
// Read a line from the file
line = inputFile.ReadLine();
// Tokenize the line
string tokens = line.Split(delim);
// Stores the tokens in the entry object
entry.BugsName = tokens[0];
entry.BugsDesc = tokens[1];
// Add the entry object to the combobox
Bugs.Add(entry);
}
// Close the File
inputFile.Close();
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
private void lstBugs_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the index of the sselected item
index = lstBugs.SelectedIndex;
// Display Bug Information
DisplayBugs();
}
private void DisplayBugs()
{
//Show Data
txtBugsName.Text = Bugs[index].BugsName;
rtxtBugDesc.Text = Bugs[index].BugsDesc.ToString();
}
private void YoungKidsBugTracker_Load(object sender, EventArgs e)
{
// Read the Bugs.txt file
ReadFile();
// Display Bug Information
BugNameDisplay();
}
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.WriteLine(Bugs[i].BugsDesc);
// sw.Write(Environment.NewLine);
}
sw.Close();
}
}
private void BugNameDisplay()
{
// Display the list of Bug Names in the List Control
foreach (BugTrackers entry in Bugs)
{
lstBugs.Items.Add(entry.BugsName );
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
This is the code in its entirety. I have a list box with 2 text boxes to hold the bug name and description. I have 3 buttons Add, Edit and Delete. If an item is selected from the listbox it will display the bugname and description. If the entry needs updated changes are made and will change the information needed. If a new bug is added you would use the add button same for the delete button.
add 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.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace BugTracker
{
struct BugTrackers
{
public string BugsName;
public string BugsDesc;
}
public partial class YoungKidsBugTracker : Form
{
// Field to hold a list of BugTrackers objects
private List<BugTrackers> Bugs = new List<BugTrackers>();
private int index; // index fo selected bugs in combobox
public YoungKidsBugTracker()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
//Declare a varialble to hold Bugs Name
StreamReader inputFile; // To Read the file
string line; // To hold a line from the file
// Create an instance of the Bug Accounts
BugTrackers entry = new BugTrackers();
// Create a delimeter array
char delim = { ',' };
// Open the file and get a StreamReader Object
inputFile = File.OpenText("smBugs.txt");
// Read the file's contents
while (!inputFile.EndOfStream)
{
// Read a line from the file
line = inputFile.ReadLine();
// Tokenize the line
string tokens = line.Split(delim);
// Stores the tokens in the entry object
entry.BugsName = tokens[0];
entry.BugsDesc = tokens[1];
// Add the entry object to the combobox
Bugs.Add(entry);
}
// Close the File
inputFile.Close();
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
private void lstBugs_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the index of the sselected item
index = lstBugs.SelectedIndex;
// Display Bug Information
DisplayBugs();
}
private void DisplayBugs()
{
//Show Data
txtBugsName.Text = Bugs[index].BugsName;
rtxtBugDesc.Text = Bugs[index].BugsDesc.ToString();
}
private void YoungKidsBugTracker_Load(object sender, EventArgs e)
{
// Read the Bugs.txt file
ReadFile();
// Display Bug Information
BugNameDisplay();
}
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.WriteLine(Bugs[i].BugsDesc);
// sw.Write(Environment.NewLine);
}
sw.Close();
}
}
private void BugNameDisplay()
{
// Display the list of Bug Names in the List Control
foreach (BugTrackers entry in Bugs)
{
lstBugs.Items.Add(entry.BugsName );
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
This is the code in its entirety. I have a list box with 2 text boxes to hold the bug name and description. I have 3 buttons Add, Edit and Delete. If an item is selected from the listbox it will display the bugname and description. If the entry needs updated changes are made and will change the information needed. If a new bug is added you would use the add button same for the delete button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace BugTracker
{
struct BugTrackers
{
public string BugsName;
public string BugsDesc;
}
public partial class YoungKidsBugTracker : Form
{
// Field to hold a list of BugTrackers objects
private List<BugTrackers> Bugs = new List<BugTrackers>();
private int index; // index fo selected bugs in combobox
public YoungKidsBugTracker()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
//Declare a varialble to hold Bugs Name
StreamReader inputFile; // To Read the file
string line; // To hold a line from the file
// Create an instance of the Bug Accounts
BugTrackers entry = new BugTrackers();
// Create a delimeter array
char delim = { ',' };
// Open the file and get a StreamReader Object
inputFile = File.OpenText("smBugs.txt");
// Read the file's contents
while (!inputFile.EndOfStream)
{
// Read a line from the file
line = inputFile.ReadLine();
// Tokenize the line
string tokens = line.Split(delim);
// Stores the tokens in the entry object
entry.BugsName = tokens[0];
entry.BugsDesc = tokens[1];
// Add the entry object to the combobox
Bugs.Add(entry);
}
// Close the File
inputFile.Close();
}
catch (Exception ex)
{
// Display an error message
MessageBox.Show(ex.Message);
}
}
private void lstBugs_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the index of the sselected item
index = lstBugs.SelectedIndex;
// Display Bug Information
DisplayBugs();
}
private void DisplayBugs()
{
//Show Data
txtBugsName.Text = Bugs[index].BugsName;
rtxtBugDesc.Text = Bugs[index].BugsDesc.ToString();
}
private void YoungKidsBugTracker_Load(object sender, EventArgs e)
{
// Read the Bugs.txt file
ReadFile();
// Display Bug Information
BugNameDisplay();
}
private void btnEdit_Click(object sender, EventArgs e)
{
BugTrackers cs = Bugs[index];
// DisplayBugs();
// Update datafile
UpdateBugsInfo();
}
private void UpdateBugsInfo()
{
if (lstBugs.SelectedIndex > -1)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("smBugs.txt");
for (int i = 0; i <= Bugs.Count - 1; i++)
{
sw.Write(Bugs[i].BugsName);
sw.Write(",");
sw.WriteLine(Bugs[i].BugsDesc);
// sw.Write(Environment.NewLine);
}
sw.Close();
}
}
private void BugNameDisplay()
{
// Display the list of Bug Names in the List Control
foreach (BugTrackers entry in Bugs)
{
lstBugs.Items.Add(entry.BugsName );
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
}
}
This is the code in its entirety. I have a list box with 2 text boxes to hold the bug name and description. I have 3 buttons Add, Edit and Delete. If an item is selected from the listbox it will display the bugname and description. If the entry needs updated changes are made and will change the information needed. If a new bug is added you would use the add button same for the delete button.
answered Nov 28 at 1:17
SunShine1976
122
122
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404642%2fediting-text-file-when-updating-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you try explaining this again. Maybe say it out loud and listen to what you say? I can't quite figure out what you are asking. In particular, what kind of app this? If your question is about why all your text gets written to a single line, that's likely because you don't write a line separator between lines. What you are writing is
Nane1,Desc1Name2,Desc2Etc3,EtcDesc3
– Flydog57
Nov 21 at 3:27