C# adding a text file to a Form

Apr 7 2014 12:18 PM
Hello, Its my first time taking a C# course. I'm adding a text file to my form. Can someone tell me whats wrong with my code?
 Thanks in advance!
1  using System;
2  using System.Collections.Generic;
3  using System.ComponentModel;
4  using System.Data;
5  using System.Drawing;
6  using System.Linq;
7  using System.Text;
8  using System.Threading.Tasks;
9  using System.Windows.Forms;
10 using System.IO;
11 namespace P6PH
12    {
13          public partial class Form1 : Form
14           {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void Form1_Load(object sender, EventArgs e)
21 {
22
23}
24
25 private void button1_Click(object sender, EventArgs e)
26 {
27 try
28 {
29 // Create an array to hold items read from the file.
30 const int SIZE = 5;
31 int[] letters = new int[SIZE];
32
33// Counter variable to use in the loop
34 int index = 0; // Declare a StreamReader variable.
35 StreamReader inputFile;
36
37 // Open the file and get a StreamReader object.
38 inputFile = File.OpenText(“TextFile1.txt”);
39
40 // Read the file’s contents into the array.
41 while (index < letters.Length && !inputFile.EndOfStream)
42 {
43 letters[index] = int.Parse(inputFile.ReadLine());
44 index++;
45 }
46
47 // Close the file.
48 inputFile.Close();
49
50 // Display the array elements in the list box.
51 foreach (int value in letters)
52 {
53 listBox1.Items.Add(value);
54 }
55 }
56 catch (Exception ex)
57 {
58 // Display an error message.
59 MessageBox.Show(ex.Message);
60 }
61}
62
63 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
64 {
65 }
66 }
67}
 

Answers (3)