Reputation: 21
I have a WinForm of 15 textboxes in a row and each one has a save and show button next to it. The user can write text in these boxes and when they open the form they can click show and view the text they wrote and then save it and close the app and its still there to be shown.
For the code I thought I could use it individually on each textbox and button
namespace UniversityProject
{
public partial class managementsystem : Form
{
private const string fileName = @"C:\txtBoxdata.txt";
public managementsystem()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = File.ReadAllText(fileName);
}
private void button2_Click(object sender, EventArgs e)
{
File.WriteAllText(fileName, textBox3.Text);
}
I thought it would work by changing the textBox numbers but I think it might have something to do with the read all. As all the textboxes show the same data which is incorrect.
Upvotes: 0
Views: 9220
Reputation: 3123
Let me try to clarify your question:
You have a bunch of textboxes where each represents a single line in a text file. Each time the user clicks on a button next to a textbox you want to replace the corresponding line.
Replacing a single line in a text file is quite easy. Just read all the lines into an array, replace the line and write everything back to the file:
private static void ReplaceLineInFile(string path, int lineNumber, string newLine)
{
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);
lines[lineNumber] = newLine;
File.WriteAllLines(path, lines);
}
}
The only thing left is to know which line should be replaced. You can add a handler for each button (notice that the line numbers start with 0):
private void button1_Click(object sender, EventArgs e)
{
ReplaceLineInFile(fileName, 0, textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
ReplaceLineInFile(fileName, 1, textBox2.Text);
}
etc.
This is not very elegant because it duplicates the same code. It would be better to use a single event handler for all buttons and then figure out which textbox it adresses and which line should be replaced. I would recommend to have arrays for the textboxes and buttons and build them in the constructor:
private TextBox[] textBoxes;
private Button[] buttons;
public managementsystem()
{
InitializeComponent();
textBoxes = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5 };
buttons = new Button[] { button1, button2, button3, button4, button5 };
}
Your single event handler would be:
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
int lineNumber = Array.IndexOf(buttons, button);
if (lineNumber >= 0)
{
ReplaceLineInFile(fileName, lineNumber, textBoxes[lineNumber].Text);
}
}
}
At some point you might want to save all values and/or create the file. Also you might want to load existing values into your textboxes when the form is loaded:
private void Form1_Load(object sender, EventArgs e)
{
LoadFile();
}
private void LoadFile()
{
if (!File.Exists(fileName))
{
WriteAllLines();
return;
}
string[] lines = File.ReadAllLines(fileName);
if (lines.Length != textBoxes.Length)
{
// the number of lines in the file doesn't fit so create a new file
WriteAllLines();
return;
}
for (int i = 0; i < lines.Length; i++)
{
textBoxes[i].Text = lines[i];
}
}
private void WriteAllLines()
{
// this will create the file or overwrite an existing one
File.WriteAllLines(fileName, textBoxes.Select(tb => tb.Text));
}
Notice that this will still work when you add new textboxes. The only thing you have to change is the creation of the arrays in the constructor. However, if you change the number of textboxes this will delete the existing file. To avoid this you can add or remove new lines manually.
Upvotes: 1
Reputation: 13207
Get a hold of StreamWriter
and StreamReader
.
Open a new stream with either stream-writer or reader in your OnClick-events. Then use something like
using(StreamWriter sw = new StreamWriter("C:\\Path\\to\\file.txt")){
sw.WriteLine("TEXTBOX1: " + textbox1.text);
sw.Close();
}
Do this for all your textboxes. And eventually use the StreamReader to read from your file
using(StreamReader sr = new StreamReader("C:\\Path\\to\\file.txt")){
string s = sr.ReadToEnd();
//find value for your textbox;
textbox1.Text = s;
sw.Close();
}
Give it a shot!
Upvotes: 0
Reputation: 1896
The basic problem is you are trying to save different textbox values into one txt file. Change the txt file-names along with the textBox names, and you will be good to go .. :)
Upvotes: 0