May 4th, 2013, 04:17 AM
Im trying to take the numbers from a file display them in a text box and then do calculations with each one which are finding the biggest number out of them, the smallest number and the average. right now i havecreated the form and added the open file to the menu strip and have gotten to load the numbers from the text file into my list box but i cant figure out how to seperate each number individually on their own line in the list box.. here's the code if in anyone is interested in helping. Thanks!
public partial class Form1 : Form
numbers = new List<double>();
private void Form1_Load(object sender, EventArgs e)
private void openToolStripMenuItem_Click(object sender, EventArgs e)
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
System.IO.StreamReader sr = new
filenumbers = (sr.ReadToEnd());
string text = "";
foreach (var item in listBox1.Items)
text += item.ToString();
textBox1.Text = text;
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
public double getAverage()
double avg = 0.0;
foreach (double temp in numbers)
avg += temp;
return (avg / numbers.Count());
May 5th, 2013, 02:08 AM
Re: Program help
Just your listbox problem should be fixed by:
To do the math calculations you want though, you'll probably want to parse them from strings into doubles:
That parse method also works with other types (e.g. integers, using Int32.Parse(...)). An error checking version is Double.TryParse(string, out double), but to use it you'll need to read about out parameters if you want to use it http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
string numberStrings = filenumbers.Split(' ');
double numbers = new double[numberStrings.Length];
for(int i = 0; i < numberStrings.Length; i++)
numbers[i] = Double.Parse(numberStrings[i]); //Convert the string to a double
Also, please surround code snippets with [code] and [/code] tags, to preseve formatting and increase the probability of a reply.
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!