-2

In my visual studio form, anytime I want to call a function based on a certain TextBox change of input, it throws an exception that "Index was outside the bounds of the array", even though my code doesn't use that input to access any array.

If (TextBox8.Text <> "" And TextBox9.Text <> "") Then
  If (ComboBox9.Text = "DC / Single Phase") Then
    voltageDrop = CInt(TextBox9.Text) * (2 * CInt(TextBox8.Text) * resLen(ComboBox8.SelectedIndex) / 1000)
  Else
    voltageDrop = 1.732 * CInt(TextBox9.Text) * (CInt(TextBox8.Text) * resLen(ComboBox8.SelectedIndex) / 1000)
  End If
  Label54.Text = CStr(voltageDrop)
End If

TextBox8 is causing the problem.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Tom Evans
  • 1
  • 1
  • 1
    Duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Jul 27 '15 at 16:12
  • 2
    it looks to me like the value in ComboBox.SelectedIndex is larger than the length of the resLen array. – Jeremy Jul 27 '15 at 16:12
  • @Jeremy Yea that seems like the obvious answer, but I'm not sure why it would be, considering it's initialized to a valid value within the combobox list. – Tom Evans Jul 27 '15 at 16:14
  • It's just weird that the TextBox input makes the exception get thrown, not the ComboBox input. – Tom Evans Jul 27 '15 at 16:15
  • I'm assuming this code is inside a text_changed event, so when textbox8 changes, it references the selectedindex property of combobox 8. If nothing is selected in the combobox, selectedindex will be -1, which is out of bounds. Again, just psychic debugging. Don't know if this is your actual problem. – Jeremy Jul 27 '15 at 16:16
  • If I change the code to resLen(1) it works, so that must be the problem. Maybe I am not initializing the selected index correctly. – Tom Evans Jul 27 '15 at 16:25
  • You need an if statement of `If ComboBox8.SelectedIndex > -1 Then`. – LarsTech Jul 27 '15 at 16:26
  • I'd suggest performing a Debug.WriteLine(Combo8.SelectedIndex) and Debug.WriteLine(resLen.Length) in there somewhere so you can see your values. also remember that the array is zero based, so you may be giving it a value one larger than the end of the array allows. – Jeremy Jul 27 '15 at 16:27
  • @LarsTech Yep that did it. Thank you. – Tom Evans Jul 27 '15 at 16:55

1 Answers1

-2

I think in your if condition you have use wrong condition. may be you should use

textbox8.textchanged method()

Or you can use

textbox8.text!=""