3
Answers

Combobox items delete - Error

MetalGearSolid

MetalGearSolid

12y
1.3k
1
        private void Form1_Load(object sender, EventArgs e)
        {
                string[] fp = File.ReadAllLines(@"filepath.txt"); //There are 3 lines in filepath.txt
                for (int i = 0; i < fp.Length; i++)
                {
                    comboBox1.Items.Add(fp[i]);
                }
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {

            try
            {
                if (e.KeyCode == Keys.Delete)

                        if (comboBox1.SelectedIndex != -1)
                            comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
            }
            catch { }
        }

Program gives an error in this line: (When I delete all items from combobox in debug mode, it gives an error)

    static class Program
    {
        [STAThread]
        static void Main()
        {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new form1()); //InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

        }
    }

What is my problem. I can't figure it out...
Answers (3)
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
Can you put breakpoint here and check error comes here?
  for (int i = 0; i < fp.Length; i++)
                {
                    comboBox1.Items.Add(fp[i]);
                }


If error is coming , Check fp[0] exists. If it not exists,
then start with i=1 in for loop
0
MetalGearSolid
NA 15 15.9k 12y
sorry but it didnt solve my problem :( it still gives the same error
0
Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
This is because there are no items inside combo box.

try this.

  if (comboBox1.Items.Count>0 && comboBox1.SelectedIndex != -1)
                            comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);