3 Replies - 4357 Views - Last Post: 26 December 2012 - 09:58 PM Rate Topic: -----

#1 NoAlphanumerics  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-December 12

Textbox lines to listbox? "Index was outside the bounds of the arr

Posted 26 December 2012 - 08:15 PM

Hi, I'm making a program and a small part of it includes a multi-line textbox and a listbox.
After entering text into the textbox the user would click a button and each line in the textbox will become an item in the listbox.

Here's the code I have:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ii As Integer
        Dim a As String = ""
        ii = TextBox1.Lines.Count()
        ListBox1.Items.Clear()
        For i As Integer = 1 To ii
            a = TextBox1.Lines(i)
            ListBox1.Items.Add(a)
        Next i
End Sub



When I run it I get the following error "Index was outside the bounds of the array."

I'd very much apritiate it if someone could help me

Is This A Good Question/Topic? 0
  • +

Replies To: Textbox lines to listbox? "Index was outside the bounds of the arr

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Textbox lines to listbox? "Index was outside the bounds of the arr

Posted 26 December 2012 - 08:41 PM

The loop should be 0 To Textbox1.Lines.Count - 1. It is a zero based array.
Was This Post Helpful? 2
  • +
  • -

#3 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Textbox lines to listbox? "Index was outside the bounds of the arr

Posted 26 December 2012 - 09:32 PM

Mr. HAWK is right..IT IS A ZERO BASED ARRAY

INSTEAD OF WRITING

 For i As Integer = 1 To ii 



U SHOULD WRITE

 For i As Integer = 0 To ii - 1 


Was This Post Helpful? 1
  • +
  • -

#4 NoAlphanumerics  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-December 12

Re: Textbox lines to listbox? "Index was outside the bounds of the arr

Posted 26 December 2012 - 09:58 PM

Thanks _HAWK_ and kai_itz me, its working now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1