CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Program help

Hybrid View

  1. #1
    Join Date
    May 2013

    Program help

    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!

    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;
    using System.Net;
    using System.Xml;
    using System.Runtime;
    using System.Xml.Serialization;

    namespace assignment4
    public partial class Form1 : Form
    List<double> numbers;
    public Form1()
    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)
    string filenumbers;
    System.IO.StreamReader sr = new
    filenumbers = (sr.ReadToEnd());
    listBox1.Items.Add(filenumbers.Split(' '));


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

  2. #2
    Join Date
    Feb 2011
    United States

    Re: Program help

    Just your listbox problem should be fixed by:
    listBox1.Items.AddRange(filenumbers.Split(' '));
    To do the math calculations you want though, you'll probably want to parse them from strings into doubles:
    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
    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

    Also, please surround code snippets with [code] and [/code] tags, to preseve formatting and increase the probability of a reply.
    Best Regards,

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.