Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have the code for reading a file and writing it to a list box;
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace Simple_File_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string line;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "txt";
            ofd.ShowDialog();

            StreamReader sr = new StreamReader(ofd.FileName);

            while (sr.Peek() > 0)
            {
                line = sr.ReadLine();
                listBox1.Items.Add(line);
            }
            sr.Close();
        }



I also have the code for how to count the characters from a text box and sort them, outputting it into a listbox.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//David Crandall
//October 22nd, 2015
//This program counts the letters for each inputed phrase

namespace Letter_Counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCount_Click(object sender, EventArgs e)
        {

            lstbxCounted.Items.Clear();

            //establishes variables that will be used later
            string myData = txtPhrase.Text.ToUpper();
            string Letters = "";
            string temp;
            int Count = 0;

            //This array will allow the counting of letters
            int [] intz = new int[26];

            //Removes everything that is a non-letters
            for (int i = 0; i < 26; i++)
            {
                intz[i] = 0;
            }
            
            for (int i = 0; i < myData.Length; i++)
            {
                temp = myData.Substring(i, 1);
                if (temp.CompareTo("A") >= 0 && temp.CompareTo("Z") <= 0)
                {
                    Letters = Letters + temp;
                }
            }

            //For each of the 
            for (int l = 0; l < 26; l++)
            {
                Count = 0;
                for (int i = 0; i < Letters.Length; i++)
                {
                    string sub = Letters.Substring(i, 1);
                    int subz = char.Parse(sub) - 'A';
                    if(subz == l)
                    {
                        Count++;
                    }
                }
                intz[l] = Count;

                //Outputs the counted letters to listbox
                lstbxCounted.Items.Add(Convert.ToChar(l + 'A') + "\t" + intz[l]);
            }
        }
    }
}



How could I make a project that is able to read the file, but at the same time count the letters, outputting the final result into a listbox? Any help would be much appreciated. Thanks
Posted
Updated 19-Nov-15 6:38am
v2
Comments
Richard MacCutchan 19-Nov-15 12:30pm    
What is wrong with what you already have?
BillWoodruff 19-Nov-15 12:32pm    
" a project that is able to read the file, but at the same time count the letters, outputting the final result into a listbox?" and the final result is ... what ? a table letters and frequencies.

File is Unicode ? or ISOxxx ? or ASCII ?
Patrice T 19-Nov-15 12:46pm    
Have you done something yourself ? or have you just found the 2 pieces of code and you are unable to put them together ?

1 solution

This is a pretty straightforward combination of the two projects.
This looks like a homework assignment so I'm not going to give you a full implementation in code, but here is some direction and suggestions.
(For example I will not tell you all the variables to declare.)

Create a new project and set up the Form components (listbox, buttons, etc.)
Then in the button Click handler method,
first you take the code that selects the file with the OpenFileDialog,
don't forget to check if the user Cancelled the dialog!
Then setup a using statement to open the StreamReader on the file (it will automatically Close() the stream when done).
Then inside the using statement, add the code that looped through Letters, just the "inner" loop, except instead of first building and using the string Letters, just loop (a while loop) using the StreamReader.Read() method which returns the integer code for the next character. You don't need the .Peek() either since .Read() returns -1 for end of stream.
You can cast that value to a char, use char.ToUpper() to force it to collapse lower to upper case, and check that it is in your A-Z range; ignore it if it isn't.
Now just increment the counter corresponding to that character.
(You can just cast the currentLetter - 'A' to int to use as the index.)
When you have read through the whole file, after the end of the using statement, then loop through the array and populate the listbox.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 19-Nov-15 17:46pm    
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900