Reading from text file, return the output of my function in a textbox

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

    #1

    Reading from text file, return the output of my function in a textbox

    Im writting a program which read a file choosen by the user, it is a txt. file with some numbers in it like :
    98.09809
    09.9879
    95.35445
    after the file has been read, I do some mathematical calculation, the one Im doing here is the standard deviation of all the data in the file. But
    my problem is that I return the result of the function and try to output it to a textbox, nothing worked my textbox is empty. Please advise. here is my code!
    Code:
    namespace test
    {
        public partial class Form1 : Form
        {
            List<double> dataValues = new List<double>();
               
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            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;
                  
                  textBox1.Text += string.Format("{0}", standardDeviation);
                  //textBox1.Text += standardDeviation.ToString();
                  
            }
    
            
            
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
            
            }
     }
  • marcellus7
    New Member
    • Oct 2008
    • 33

    #2
    You have to set the Standard Deviation to the text box field before you return Standard Deviation, because when it returns it will exit the function;

    Or when you call the std function you can set it to the textbox value like:

    [code]textBox1.Text+= CStr(std(List)) ;

    Comment

    Working...