473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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!
Expand|Select|Wrap|Line Numbers
  1. namespace test
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         List<double> dataValues = new List<double>();
  6.  
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void Form1_Load(object sender, EventArgs e)
  13.         {
  14.  
  15.         }
  16.  
  17.         private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
  18.         {
  19.             if (ofd.ShowDialog(this) == DialogResult.OK)
  20.             {
  21.                 FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
  22.                 StreamReader sr = new StreamReader(fs);
  23.                 String nbr;
  24.  
  25.                 while ((nbr = sr.ReadLine()) != null)
  26.                 {
  27.                     double data = 0.0;
  28.                     if ((double.TryParse(nbr, out data)))
  29.                         dataValues.Add(data);
  30.                         //textBox1.Text += nbr.ToString() + "\r\n";
  31.                            //Console.WriteLine(data);  
  32.                 }
  33.                 sr.Close();
  34.                 fs.Close();       
  35.             }
  36.         }
  37.  
  38.         public double std(List<double> dataValues)
  39.         {
  40.             double average = 0.0;
  41.  
  42.             foreach (double d in dataValues)
  43.             {
  44.                 average += d;
  45.             }
  46.  
  47.             average /= (double)dataValues.Count;
  48.  
  49.             //Calculate standard deviation
  50.             List<double> stdDev = new List<double>();
  51.             foreach (double d in dataValues)
  52.             {
  53.                 stdDev.Add(Math.Pow((d - average), 2));
  54.             }
  55.  
  56.             //Sum of all values, divide by the average and take sqrt to get std
  57.             double standardDeviation = Math.Sqrt((stdDev.Sum() / average));
  58.  
  59.               return standardDeviation;
  60.  
  61.               textBox1.Text += string.Format("{0}", standardDeviation);
  62.               //textBox1.Text += standardDeviation.ToString();
  63.  
  64.         }
  65.  
  66.  
  67.  
  68.         private void textBox1_TextChanged(object sender, EventArgs e)
  69.         {
  70.  
  71.         }
  72.  
  73.         }
  74.  }
  75.  
  76.  
  77.  
Sep 14 '10 #1
1 1532
marcellus7
33 New Member
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)) ;
Sep 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
11997
by: John M. Lembo | last post by:
I am using Perl to parse a text file and output to another file. The text file has data on Unix virtual memory (vmstat) and I want to delete lines that I don't want and output lines that I want to a new file. The script I have is giving me blank lines in the new file. #!/usr/local/bin/perl #program to read systemdata file #and write to systemstats file # #
1
7043
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
14
4508
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most recent entries. When I read line using StreamReader object it starts from the top, so looping though lines and keeping track of the line is not helpful. Is there anyway to start reading from the bottom or if anyone could suggest some other...
8
18226
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
4
12788
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
0
1913
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted but this is probabally a reflection of my not clear understanding of the mechanics behind what i'm trying to achieve. Please bear with it I really could do with your help. I have a simple windows form. There are two controls on the form that I...
3
3168
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label = 'aeff5' state 0 = 'eee' state 1 = 'ffffff' . .and so on...
1
3451
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However when I output the file to a text file, all the data is wrapping and at the end of each block I am getting the text part of the header included but not all of it. The text that appears is: <br xmlns:msxsl="urn:schemas-microsoft-com:xslt"...
13
7032
by: jhamb | last post by:
Hi, This code is in Perl (just a trial, not tested) to parse a text file and output to another file. It is used to delete lines that are not required and output lines that the user wants, to a new file. The problems are: 1 This script is giving blank lines in the new file. 2 I have to remove a whole delimitter (#if FOR_LAB) from the text file. 3.The tool should be able to parse the file, and remove FOR_LAB’ed code, with out affecting the...
1
2849
by: ayyanki | last post by:
Hello all, I'm still learning about c# and I have a major function that I need to write (in a forms application), and I have no idea how to go about it. Here's the scenario: I have a text file that contains all the data needed for a database table (MS SQL Server). I know the location and length of each field within the text file. Can anyone explain to me (I know it may be quite involved) how I can read the text file, pull out the fields...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.