Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi, I'm stuck in a way I don't know how to describe..
 
I have a form with buttons that link to other forms
 
for example:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Form2.Show()
    End Sub
Now when I click on Button1 it pops up Form2 and hides Form1 as expected
This works for all Forms except one. When I click the button for that one I get an error message:
InvalidOperationException was unhandled
An error occurred creating the form. See Exception.InnerException for details.  The error is: Collection index must be in the range 1 to the size of the collection.
 
Usually when I get an error message I can figure out what to do with it, but this time I really don't understand how to fix it. I don't even know what collection it is trying to work with.
I was editing the code on the Sub that handles the Form.Load and also some other subs that might be linked to it. I've never before had the error message pop up on the Form.Show() before unless I had made a typo in the name of the form, so this is new to me.
 
I've tried disabling the Form.Load Sub altogether but that didn't fix the error, so it seems to me it's not the code there that's wrong.
I've even tried a line "Stop" right at the start of the Form.Load Sub but it doesn't even seem to get there.
 
I also get the same error when I try to close the Form with a button like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Close()
        Form2.Close()
    End Sub
 
I'm using Microsoft Visual Basic 2010 Express.
Posted 9-Dec-12 23:05pm
Edited 9-Dec-12 23:08pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to look at the form you are trying to show - somewhere, it is accessing a collection before it is fully ready. Unfortunately, this could be anywhere - constructor, Load event, Shown event, individual control constructor or load, SelectedValue changed event for some controls, and so forth.
 
Use the debugger. Run it, press the button, and wait for the exception. Then look at the inner exception and the call stack, to work out exactly which code is throwing the problem. We can't be more specific - we can't even see your screen, much less your code!
  Permalink  
Comments
OneInNineMillion at 10-Dec-12 5:49am
   
Thanks for the Reply. I don't know anything about the constructor and I have a question. Could it be that the constructor (I suppose it 'constructs' the form before the Load event is even initiated) triggers a SelectedValue or CheckedChanged event?
 
I'm driving myself mad with this sites controls lol! I just hope I put it in the right place this time.. gosh..
OriginalGriff at 10-Dec-12 6:03am
   
Yes. If you have any controls that signal a SelectedValue change, then there is a good chance that the initial value setting from the call to InitializeComponent in the form constructor will cause an event pretty much immediately. As I said, you ned to look at the call stack in the debugger to work out exactly where the problem is occurring from.
 
You got the message there in the end - there are a few, but they do make sense and makes things easier when you are used to them. The right control means the right emails get sent only to the relevant people, so we aren't swamped any more than normal! :laugh:
OneInNineMillion at 10-Dec-12 6:35am
   
Thanks again, I learned something today... I now looked at the the error message's exception detail, kind of understand it. so I have another question: I saw a line in the stackTrace which ended like this: ...\Form1.vb:line 75
another line in the stackTrace ended like this: ...\Form1.Designer.vb:line 103
I don't know where to look for that second (Designer) line in my files/wherever
 
Also, I 'fixed' my problem with a boolean and a if-statement so the sub triggered by the SelectedValue changed event doesn't do anything.
 
I now understand why I was getting the error message, and can start trying to further fix things. Thanks a lot for your help!
OriginalGriff at 10-Dec-12 6:48am
   
You're welcome!
 
Have a look in the Solution Explorer pane. There is a small toolbar at the top: the third one from the left (probably) is "show all files" and it defaults to "false" - click it, and your form .VB files will turn into a small tree view, allowing you to access the Designer files.
OneInNineMillion at 10-Dec-12 7:20am
   
Awesome, I'm so going to mess with the order of things there :P
I've run out of questions at the moment, so ...
A final thanks to you and goodbye for now
OriginalGriff at 10-Dec-12 7:22am
   
I wouldn't! :laugh: It'll all end in tears... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 370
1 Prakriti Goyal 177
2 Sergey Alexandrovich Kryukov 155
3 jlopez788 134
4 _Amy 110
0 OriginalGriff 6,762
1 Sergey Alexandrovich Kryukov 5,539
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100