How to debug "Type initializer for 'WindowsFormsAutoClick.Form1' threw an exception."?

635 Views Asked by At

I am trying to add a new Dictionary value and add a new User Control to a Flow Layout. But pressing the add control button throws an initializer exception with an inner exception such as "Key has already been added". I don't know the full exception because I am having difficulties with finding how to open the popup error that usually shows in Visual Studio.

Already set a loop to check if the key already is added to the dictionary and if so change the key (as shown in the code blocK). Also tried clearing the flow layout controls list. (made no changes to the error)

Function were the error is thrown:

/// <summary>
    /// Create a new key propertie set.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
private void buttonAddKey_Click(object sender, EventArgs e)
{
    // Create new UI key component in the flow layout.
    KeyPropertiesCtrl inputKeyCtrl = new KeyPropertiesCtrl();
    flowLayoutKeys.Controls.Add(inputKeyCtrl);

    Console.WriteLine("inputKeyCtrl " + inputKeyCtrl.Name + " parent " + inputKeyCtrl.Parent.Name);

    // Create new data input key.
    CustomInputKey newKey = new CustomInputKey();
    newKey.Activation.InputKey = new Interception();

    Console.WriteLine("newKey " + newKey.Activation.InputKey);

    // Get a key binding from the user.
    try
    {
        Form1.Context = InterceptionDriver.CreateContext();

        InterceptionDriver.SetFilter(Form1.Context, InterceptionDriver.IsKeyboard, (Int32)KeyboardFilterMode.All);
        InterceptionDriver.SetFilter(Form1.Context, InterceptionDriver.IsMouse, (Int32)MouseFilterMode.All);

        Form1.InterceptOnce(Form1.Context, out newKey.Activation.InputKey.DeviceId, out newKey.Activation.InputKey.TheStroke);

        InterceptionDriver.DestroyContext(Form1.Context);


        // Change the key bind in UI text.
        if (newKey.Activation.InputKey.TheStroke.Key.State != 0)
            inputKeyCtrl.button1.Text = newKey.Activation.InputKey.TheStroke.Key.Code.ToString();
        else
            inputKeyCtrl.button1.Text = newKey.Activation.InputKey.TheStroke.Mouse.State.ToString();
    }
    catch (Exception excp)
    { Console.WriteLine(excp); }

    // Set the key bind in data list.
    string name = inputKeyCtrl.Name;
    byte attempts = 0;
    while (Form1.CurrentSelections.SelectedPreset.GetCustomKeys.ContainsKey(name))
    {
        if (attempts > 50)
        {
            break;
        }
        attempts++;
        name += attempts;
    }

    Console.WriteLine(this.Name + " name Attempts " + attempts + " name " + name);

    if (attempts < 50)
        Form1.CurrentSelections.SelectedPreset.AddKey(Form1.CurrentSelections.SelectedPresetName, name, newKey);
}

So What I actually expect is after pressing the button control. It creates a new User Control in a Flow Layout and then it freezes the application when it hits the function InterceptOnce() because there it will wait for me to press a input to bind that input. Finally store the new key bind and save the dictionary on to a file.

But it throws the exception "key has already been added" on the line of while(Form1.CurrentSelections...){} and before I added that while loop and the try{} the exception was thrown on Form1.Context = InterceptionDriver.CreateContext(); which is weird, because with the try{}catch(Exception excp){} it doesn't throw an exception. I think the exception has to do with new CustomInputKey(); and have no clue why...

Error: screenshot

1

There are 1 best solutions below

0
On BEST ANSWER

Initializer error information is also shown in Class[Design]. Making the variable non-static will move when the exception is thrown which helped me finding the cause of the exception.

Turns out I was assigning a value while in an initializing phase of a control component to a variable which wasn't created yet.