Application.Run(new Form1()) throws System.ArgumentException: 'Value does not fall within the expected range.'

1

I am stumped.

I started implementing a version of my Forms app that takes command line arguments, but started getting errors. I reverted my changes - but this exception remains:

The project compiles fine, but when I start the app (debugging), just after my main Form shows, the app halts throwing the following exception:

System.ArgumentException occurred HResult=0x80070057 Message=Value does not fall within the expected range. Source= StackTrace: at Accessibility.IAccessible.get_accChild(Object varChild) at System.Windows.Forms.AccessibleObject.Accessibility.IAccessible.get_accChild(Object childID) at System.Windows.Forms.InternalAccessibleObject.System.Windows.Forms.UnsafeNativeMethods.IAccessibleInternal.get_accChild(Object childID)

Edit/FYI: I have "Enable just my code" turned off - this doesn't happen when it is turned on.
I still somehow haven't found the offending code. Stepping through everything in Form1() returns me to the call in Program.cs, where the Exception suddenly happens.
I'll manage getting my project to run in the end, one way or another - I'd still really like to know where and what the problem is (for me and posterity) - any hints and help is still very much appreciated

I have since whittled down the app to the bare bones, only showing the main form, without doing anything. I still get this weird message. Error Shown

program.cs:

using System;
using System.Windows.Forms;

namespace Move_Stuff
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs:

using System;
using System.Windows.Forms;

namespace Move_Stuff
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Fine so far");
        }
    }
}

So those are probably not the culprit.

Here is my complete project folder: https://mega.nz/#!9mQVWLqI!MN1tYS4cQk5-ZolosDvvIme2_Lk9rjSogPhHzrJvPt0

Where did things go wrong?

c#
.net
winforms
visual-studio
asked on Stack Overflow Feb 11, 2018 by Ben Philipp • edited Feb 11, 2018 by Ben Philipp

1 Answer

1

I ran into the same stacktrace and did some digging. For reference I'm using .Net Framework 4.7.1. The root cause seems to be an issue with Winforms that only happens inside the listbox controls when calling an unmanaged method in the Accessibility class AND the listbox has no items. I inspected the source with dotpeek and wanted to step through, but I'm not sure how/if its possible with unmanaged code. This is the culprit method disassembled.

// Method get_accChild with token 06000023
[/*Attribute with token 0C000041*/DispId(-5002)]
[/*Attribute with token 0C000042*/TypeLibFunc(TypeLibFuncFlags.FHidden)]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
[return: MarshalAs(UnmanagedType.IDispatch)]
object get_accChild(/*Parameter with token 08000098*/[MarshalAs(UnmanagedType.Struct), In] object varChild); 

The easiest way to reproduce this is to create a new Winforms project, add a listbox through the designer making sure no items are added to its collection. Debug with "Just My Code" turned off. It seems like each iteration of the Winforms message loop triggers this exception. Adding one or more items makes it go away. A weird quirk I found is that if I select a different control in between iterations the exception stops getting triggered until I click onto the empty listbox again.

A potential fix would be to add a single empty string item to the listbox through the designer and handle accordingly when adding the first real item/ setting a datasource. I haven't noticed any aberrant behavior when just disregarding this exception, so I think its better to let the framework swallow it than to add extra workaround logic.

answered on Stack Overflow Mar 8, 2019 by Riggeot

User contributions licensed under CC BY-SA 3.0