Question

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");
        }
    }
}
Was it helpful?

Solution

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();
}

OTHER TIPS

You may want to use StringBuilder to build your Text. As others pointed out, you are overwriting the value of RichTextBox's Text property every line. You may want to do something like this.

var sb = new StringBuilder();
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;

            sb.AppendLine("VM " + "M1=" + Un.ToString(".0") + " M2=" + Deux.ToString(".0"));
        }
    }
}
richTextBox2.Text = sb.ToString();

If by "apply a calculation to all lines" you mean, log the calculation results for each line in the richBox, then you need to concatenate the new string, not assign a new one.

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 += String.Format("VM M1={0} M2={1}", Un.ToString(".0"), Deux.ToString(".0"));
        }
    }
}

Also, you should use String.Format in cases such as this, it makes it more readable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top