Home > Net >  I get an exception System.ArgumentOutOfRangeException
I get an exception System.ArgumentOutOfRangeException

Time:09-29

I have a combobox and I want it to automatically find and suggest a phrase with any word from the dictionary. I found this solution - link, but I noticed that if the phrase is not found, then the application may crash - Invalid argument value of 0 is not valid for index. Tried to get around this in every possible way, but in the end the application completely closes. What is the error in this solution and is it possible to somehow fix it, and if the phrase is not found, display a messagebox about this, for example ..

The dictionary consists of the same phrases as in the first question on link:
Doors opening elements
exterior doors
interior doors
sliding doors

The application does not always crash and I think that this is due to the presence of these letters entered in the combobox in the initial dictionary
I get crush when write:
orser
erot and etc.
and click on window form,

but nothing happens when i try to search:
abc
zui and etc.

I couldn't fix the code, I hope you can. 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;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            listOriginal.Add("Doors opening elements");
            listOriginal.Add("exterior doors");
            listOriginal.Add("interior doors");
            listOriginal.Add("sliding doors");

            this.comboBx.Items.AddRange(listOriginal.ToArray());
        }

        // Bind default keywords
        List<string> listOriginal = new List<string>();
        // save new keywords
        List<string> listNew = new List<string>();
        private void comboBx_TextUpdate(object sender, EventArgs e)
        {
            //clear combobox
            this.comboBx.Items.Clear();
            //clear listNew
            listNew.Clear();
            foreach (var item in listOriginal)
            {
                // call ToLower() .. not case sensitive
                if (item.ToLower().Contains(this.comboBx.Text))
                {
                    //add to ListNew
                    listNew.Add(item);
                }
            }
            this.comboBx.Items.AddRange(listNew.ToArray());
            this.comboBx.SelectionStart = this.comboBx.Text.Length;
            Cursor = Cursors.Default;
            // Automatically pop up drop-down
            this.comboBx.DroppedDown = true;
        }
    }
}

After compile code get exception in line 15:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());// <-------- this
        }
    }
}

StackTrace:

System.ArgumentOutOfRangeException
  HResult=0x80131502
  Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)
   at System.Windows.Forms.ComboBox.get_Text()
   at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
   at System.Windows.Forms.ComboBox.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at WindowsFormsApp.Program.Main() in C:\Users\denis\source\repos\WindowsFormsApp\WindowsFormsApp\Program.cs:line 15

CodePudding user response:

The exception implies there are no items in the collection. In this scenario, the TextUpdate event clears the ComboBox items and the exception occurs when you click off the ComboBox while it has no items.

A workaround to this problem would be to keep the ComboBox populated at the end of the TextUpdate event and only manipulating the dropdown if text matches. If listOriginal contains the updated text, add newList to the ComboBox and set ComboBox.DroppedDown to true. Else, add the listOriginal to the ComboBox and set ComboBox.DroppedDown to false.

        private void comboBx_TextUpdate(object sender, EventArgs e)
    {
        //clear combobox
        this.comboBx.Items.Clear();
        //clear listNew
        listNew.Clear();

        foreach (var item in listOriginal)
        {
            // call ToLower() .. not case sensitive
            if (item.ToLower().Contains(this.comboBx.Text.ToString()))
            {
                //add to ListNew
                listNew.Add(item);
            }
        }

        this.comboBx.SelectionStart = this.comboBx.Text.Length;
        Cursor = Cursors.Default;

        // Check if ListNew has been populated.

        if (listNew.Count != 0)
        {

            this.comboBx.Items.AddRange(listNew.ToArray());
            // Automatically pop up drop-down
            this.comboBx.DroppedDown = true;

        }
        else
        {
            this.comboBx.Items.AddRange(listOriginal.ToArray());
            // Automatically close dropdown
            this.comboBx.DroppedDown = false;
            
        }
    }

There are definitely better ways to go about fixing this error but this is the simplest fix I guess. Hope it helps!

  • Related