y s

y s

  • NA
  • 2.6k
  • 26.3k

error when browse data

Apr 16 2011 12:48 PM
when i browse data, will pop out an error which is index out of range.Must be non negative and less than the size of the collection. may i know wat error with my coding?
my text file data are: 11 21
11 21
11 21
below are coding:
 private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fileOpen = new OpenFileDialog())
            {
                try
                {
                    sortList.Clear();
                    sortList1.Clear();
                    fileOpen.Filter = "txt files (*.txt)|*.txt|All files(*.*)|*.*";

                    if (fileOpen.ShowDialog() == DialogResult.OK)
                    {

                        StreamReader fileReader = new StreamReader(fileOpen.FileName, Encoding.Default);
                        str = fileReader.ReadToEnd();
                        fileReader.Close();
                        ContentBox.Text = str;
                          textBox2.Text = str;
                        textBox1.Text = fileOpen.FileName;

                    }


                    SplitItems = (str).Split(new string[] { "\r\n\t", " " }, StringSplitOptions.None);

                    foreach (string listOut in SplitItems)
                    {
                        ListSplitItems += "\r\n" + listOut;
                    }
                }

                catch (Exception o)
                {
                    string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                    MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (File.Exists(fileOpen.FileName))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(fileOpen.FileName);

                        while ((line = file.ReadLine()) != null)
                        {

                            ListSplitLineByLine = "";

                            line = line.Replace("\r\n", " ");
                            String[] SplitItemLineByLine1 = (line).Split(new string[] { "\r\n\t", " " }, StringSplitOptions.None);
                            //     String[] SplitItemLineByLine1 = (line).Split(' ');
                            foreach (string lineByLine in SplitItemLineByLine1)
                            {
                                ListSplitLineByLine += "\r\n" + lineByLine;

                            }

                            txtCaseInputs.Text = ListSplitLineByLine.Trim();

                            GenCombItems();


                        }

                    }
                    catch (Exception o)
                    {
                        string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                        MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                }
            }


        }



        private void GenCombItems()
        {
            lbCombinations.Items.Clear();
            int n = txtCaseInputs.Lines.Length;
            for (int k = 1; k <= txtCaseInputs.Lines.Length; k++)
            {
               {

                txtNumCombinations.Text = Combination.Choose(n, k).ToString();

                Combination c = new Combination(n, k);

                string[] result = new string[k];
                while (c != null)
                {
                    result = c.ApplyTo(txtCaseInputs.Lines);

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.Length; ++i)
                    {
                        sb.AppendFormat("{0}{1}", result[i], " ");
                    }
                    lbCombinations.Items.Add(sb.ToString().Trim());

                    c = c.Successor();
                }

                }

Answers (2)