I'm stuck on a problem, I can not execute my calculation on all lines of my text file. The calculation only applies to the first line. I want to see my result in richTextBox2
.
Here is my code :
using (StreamReader sr1 = new StreamReader(fileName))
{
while ((line = sr1.ReadLine()) != null)
{
commands = line.Split(' ');
if ((commands.Length > 1) && (commands[1].Contains('X')))
{
double X = Convert.ToDouble(commands[1].Substring(1, commands[1].Length - 1).Replace(".", ""));
double Y = Convert.ToDouble(commands[2].Substring(1, commands[2].Length - 1).Replace(".", ""));
Un = ((X * 100) / 1.57) / 1000;
Deux = ((Y * 100) / 1.57) / 1000;
richTextBox2.Text = "VM " + "M1=" + Un.ToString(".0") + " M2=" + Deux.ToString(".0");
}
}
}
You are assigning to your richTextBox
new text in each iteration instead of appending it. Try this.
richTextBox2.Text += "VM " + "M1=" + Un.ToString(".0") + " M2=" + Deux.ToString(".0");
It is better to first read file and then assign it to rictTextBox
. Like this.
string fileData = "";
using (StreamReader sr = new StreamReader(fileName))
{
fileData = sr.ReadToEnd();
}
StringBuilder sb = new StringBuilder();
if (!String.IsNullOrEmptry(fileData))
{
string[] splitedData = fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); // Suppose file has delimited by newline
foreach (string line in splitedData)
{
commands = line.Split(' ');
if ((commands.Length > 1) && (commands[1].Contains('X')))
{
double X = Convert.ToDouble(commands[1].Substring(1, commands[1].Length - 1).Replace(".", ""));
double Y = Convert.ToDouble(commands[2].Substring(1, commands[2].Length - 1).Replace(".", ""));
Un = ((X * 100) / 1.57) / 1000;
Deux = ((Y * 100) / 1.57) / 1000;
sb.AppendLine("VM " + "M1=" + Un.ToString(".0") + " M2=" + Deux.ToString(".0"));
}
}
richTextBox2.Text = sb.ToString();
}