שתף באמצעות


InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

Question

Wednesday, March 11, 2015 7:44 AM

All,

I got a new workstation.  I installed VS 2012 (same version as my old workstation v4.5).  I copied the project to the new machine and opened it.  I ran it (before compile) with seemingly no issues.  So, I compiled it.  I tried to open it on the same machine and I received the following unhandled exception error...

InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

I have attached the entire unhandled exception error that I am getting on the new machine, as well as the problem code.  This did not happen on my old workstation.  So, I went to see if there were any version differences between my old workstation and the new one and low and behold Microsoft updated .net from 4.5.1 to 4.5.2.

What I am trying to do is set every combo box to selected index of 0 when the form loads.  The funny thing is that even after the unhandled error (which never happened in v4.5.1) all the combo boxes are set to index 0.  So, the code works perfectly.  Why?  Is there a way to code around this problem in v4.5.2?  Also, if I keep developing using 4.5.2 won't all my users have to update to .net 4.5.2 as well?

James

    Private Sub frmConfigTool_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      'Users cannot input to the text box
        'I do this to select cmb index on form load istead of leaving all cmb boxes blank
        For Each cmb As ComboBox In GetControlsOfType(Of ComboBox)(Me, True)
            cmb.SelectedIndex = 0
        Next
    End Sub
    Function GetControlsOfType(Of ctrlType As Control)(parent As Control, searchContainers As Boolean) As IEnumerable(Of ctrlType)
        Dim ctrls As New List(Of ctrlType)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is ctrlType Then ctrls.Add(DirectCast(ctrl, ctrlType))
            If searchContainers AndAlso ctrl.Controls.Count > 0 Then ctrls.AddRange(GetControlsOfType(Of ctrlType)(ctrl, searchContainers))
        Next
        Return ctrls
    End Function

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
Parameter name: SelectedIndex
   at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
   at Configuration_Tool.frmDefenseProConfigTool.frmLoad(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase:

DefensePro Configuration Tool
    Assembly Version: 2.0.0.8
    Win32 Version: 2.0.0.8
    CodeBase:

Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 12.0.51209.34209 built by: FX452RTMGDR
    CodeBase:

System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
    CodeBase:

System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase:

System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase:

System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase:

System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34245 built by: FX452RTMGDR
    CodeBase:

System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase:

System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
    CodeBase:

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

All replies (14)

Wednesday, March 11, 2015 9:32 AM ✅Answered | 1 vote

You cannot set the SelectedIndex property to 0 for ComboBoxes that don't contain any items. Just put an if-case around the assignment and you should be fine:

Private Sub frmConfigTool_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   'Users cannot input to the text box
        'I do this to select cmb index on form load istead of leaving all cmb boxes blank
        For Each cmb As ComboBox In GetControlsOfType(Of ComboBox)(Me, True)
            If cmb.Items.Count > 0 Then
  cmb.SelectedIndex = 0
     End If
        Next
    End Sub

 

 

Please remember to mark all helpful posts as answer to close your threads and please start a new thread if you have a new question.

                           


Wednesday, March 11, 2015 9:10 PM ✅Answered

I have never used the "activated" method before.  It works with the code I wrote, but resets every combo box back to index 0 every time the program regains focus.  Not desired.

That's why I suggested you may need a global flag to indicate that the reset should not happen, depending on when you want the indexes reset.

Dim Init As Boolean = False

    Private Sub Form_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If Not Init Then
            'Set the indexes
            Init = True
        End If
    End Sub

The difference between Load and Activate is:
   Load can occur before initialisation of the controls is complete (your problem)
   Activate occurs each time the form is activated (so requires a test for first time)
 

Using that code in the form load event creates a risk that something in the control initialisation runs after that code, and undoes what you are trying to do. Worst case is that the code will error because the control you are trying to set the value for doesn't exist. Or, in your case, it will get missed because it isn't a part of the collection at the time that code runs. You should consider why you need to test for no items in the combo when you know there are items.


Wednesday, March 11, 2015 8:54 AM

I tried to open it on the same machine and I received the following unhandled exception error...

InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

Did your operating system version also change?

Try moving the code in the form load event to some other event, such as Activated.  You may need a global flag to indicate whether the code should be repeated on later activations.


Wednesday, March 11, 2015 9:32 AM

Acamar,

Same OS on both machines.  Moving the code to the "activated" event fixed the problem.  Will the end users be forced to upgrade to .net 4.5.2 now???

James


Wednesday, March 11, 2015 10:17 AM

Will the end users be forced to upgrade to .net 4.5.2 now???

Not if they are using the updated version of your code.


Wednesday, March 11, 2015 4:17 PM

Magnus,

The combo box does have items in it.  Every combo box has items in the collection list.  So, setting the index to zero shouldn't be an issue.  This sounds like a bug in .net 4.5.2 to me.

I have never used the activation method on form load before but it appears to have fixed the issue.

James


Wednesday, March 11, 2015 6:47 PM

Mangus,

I have never used the "activated" method before.  It works with the code I wrote, but resets every combo box back to index 0 every time the program regains focus.  Not desired.

So, I used your code on the load event and it works perfectly on both machines.  I'm still don't quite understand why the error happened to begin with but hey, it's fixed!

Thanks everyone for your help.  I learned something new about activating and loading form events.

Thanks!


Wednesday, March 11, 2015 9:31 PM

Excellent info!  Makes perfect sense...

I will try it out...thanks Acamar!

James


Wednesday, March 11, 2015 9:44 PM

What about positioning controls, like setting location?  Would that code be best served under the activation event or form load event?


Wednesday, March 11, 2015 9:50 PM

The first situation for the combobox (even if it is empty) is index = -1

Success
Cor


Wednesday, March 11, 2015 10:05 PM

Cor,

If that is true then why did everything work fine with .net 4.5.1?  Why didn't the problem happen until upgrade to .net 4.5.2?  None of my users complained about the error before.

James


Wednesday, March 11, 2015 10:28 PM

If that is true then why did everything work fine with .net 4.5.1?  Why didn't the problem happen until upgrade to .net 4.5.2?  None of my users complained about the error before.

That hasn't changed.  The value of the index property is -1 by default.  Your code is trying to change that so it is set to 0 at startup.  But that can only happen if there are items in the combobox, or the system throws an error.  That's why you have to ensure it gets set to 0 after the items are loaded.

The order in which things happen to controls during form load is not guaranteed.  The original code was always susceptible to the error.  Something changed (I would have thought the OS, but apparently not) that caused a possible error to become probable.


Wednesday, March 11, 2015 10:31 PM

What about positioning controls, like setting location?  Would that code be best served under the activation event or form load event?

I have never had a problem with control position, as that is not dependant on any internal status of the control - if it exists, it can be positioned.  However, location is usually set in the form's resize event, because you are only setting the position at run time if it is dependant on the form size, and as that can change during runtime you need that code to execute whenever the size changes, not just at startup.


Friday, July 5, 2019 12:45 PM

Thanks that worked a treat