How to wrtite multiple returned values in textBox without Append.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Greenwood
    New Member
    • Sep 2010
    • 7

    How to wrtite multiple returned values in textBox without Append.

    Need big help, tried almost everything, I need to put a string together, that contains all my returned calculated functions in a textBox. As you can see I have a menuToolStrip option that when I click, it will show the list of my calculated functions, I want it to show them one by one verticaly on different lines. It does not work and I dont know why. Please advise, thanks

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace test
    {
        public partial class Form1 : Form
        {
    //A list to put all the data values(.txt file) opened 
            List<double> dataValues = new List<double>();
               
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    //Choose the data file to open for calculation
            private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                    StreamReader sr = new StreamReader(fs);
                    String nbr;
    
                    while ((nbr = sr.ReadLine()) != null)
                    {
                        double data = 0.0;
                        if ((double.TryParse(nbr, out data)))
                            dataValues.Add(data);
                            //textBox1.Text += nbr.ToString() + "\r\n";
                               //Console.WriteLine(data);  
                    }
                    sr.Close();
                    fs.Close();       
                }
            }
            
            public double std(List<double> dataValues)
            {
                double average = 0.0;
    
                foreach (double d in dataValues)
                {
                    average += d;
                }
    
                average /= (double)dataValues.Count;
    
                //Calculate standard deviation
                List<double> stdDev = new List<double>();
                foreach (double d in dataValues)
                {
                    stdDev.Add(Math.Pow((d - average), 2));
                }
    
                //Sum of all values, divide by the average and take sqrt to get std
                double standardDeviation = Math.Sqrt((stdDev.Sum() / average));
    
                return standardDeviation;
            }
    
    //Calculate the average of the data in the list
            private double getAverage(List<double> dataValues)
            {
                double total = 0;
                foreach (double value in dataValues)
                {
                    total += value;
                }
                double average = total / dataValues.Count;
                return average;
            }
    
    public void sommeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                textBox1.Text += "ecartType = " + std(dataValues) + "Moyenne = " + getAverage(dataValues);
            }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What part doesn't work?
    Textboxes use "\r\n" to create new lines.
    (Are you on a webpage? Did you set your textbox to multiline ?)

    Comment

    • Greenwood
      New Member
      • Sep 2010
      • 7

      #3
      Im in a windows App. Form, it gives me an error that std(dataValues) is a method or function and can not be concanated with operand +. yes my textbox is multilign.
      Please advise how to display the methods returned values all together on seprate lines.
      I want ti show like this in my textbox:

      Ecart-type = 2553453454
      Moyenne = 22345342

      Thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Hmm I don't know why it wouldn't implicitly convert it ToString() in that context, but you should be able to do this:

        textBox1.Text += "ecartType = " + std(dataValues) .ToString() + "\r\nMoyenn e = " + getAverage(data Values).ToStrin g();

        Comment

        • Greenwood
          New Member
          • Sep 2010
          • 7

          #5
          Thank you so much, everything is working now. Again big thanks

          Comment

          Working...