By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Listbox error

P: n/a
Hi,

I have created a listbox called "dtlist1" on my VB.NET form. I call a
procedure as follows:

Private Sub openfile(flname As String)

dtlist1.Items.Clear()

etc..

End Sub

and I get the error message: "Exception System.IndexOutOfRangeException
was thrown in debugee: Index was outside the bounds of the array."

Why is this?

I even try adding:

dtlist1.Items.Add("this is the first line of the listbox")

before the .Items.Clear() and it doesn't help! I then get the same
error message on the .Items.Add line.

Can anyone suggest?

Thanks,
Alex

May 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Ali Chambers" <in**@alexchambers.co.uk> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi,

I have created a listbox called "dtlist1" on my VB.NET form. I call a
procedure as follows:

Private Sub openfile(flname As String)

dtlist1.Items.Clear()

etc..

End Sub

and I get the error message: "Exception System.IndexOutOfRangeException
was thrown in debugee: Index was outside the bounds of the array."

Why is this?

I even try adding:

dtlist1.Items.Add("this is the first line of the listbox")

before the .Items.Clear() and it doesn't help! I then get the same
error message on the .Items.Add line.

Can anyone suggest?

Thanks,
Alex

Several questions:
1. What version and edition of "VB.NET" are you using?
2. Is this a Windows app or a web app (use of the term "form" vs. "page"
implies a Windows app, but best to be sure)?
3. Where is your sub declared, in the form class or in a different class?
4. Are you referring to a list box added to a form in design mode, or to a
list box object created in code?

I tried scenarios similar to the one you describe on both VS 2003 and VS
2005, placing my sub in the form class in Windows apps, referencing a list
box created in the GUI designer. I'm unable to replicate your issue.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
May 17 '06 #2

P: n/a
Hi Peter,

I'm using SharpDevelop2.0 - a free open source IDE for VB.NET. My
project is a windows app and I'm referring to listboxes created in
design mode.

My code is as follows, if that would help:

** START **

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Collections

Namespace MA_tester

Public Class MainForm
Inherits System.Windows.Forms.Form
Private dtlist1 As System.Windows.Forms.ListBox
Private openlist1 As System.Windows.Forms.ListBox
Private lowlist1 As System.Windows.Forms.ListBox
Private groupBox3 As System.Windows.Forms.GroupBox
Private button3 As System.Windows.Forms.Button
Private groupBox1 As System.Windows.Forms.GroupBox
Private filelist1 As System.Windows.Forms.ListBox
Private textBox8 As System.Windows.Forms.TextBox
Private mult As System.Windows.Forms.ListBox
Private newhighlist As System.Windows.Forms.ListBox
Private textBox5 As System.Windows.Forms.TextBox
Private textBox4 As System.Windows.Forms.TextBox
Private newlowlist As System.Windows.Forms.ListBox
Private closelist1 As System.Windows.Forms.ListBox
Private highlist1 As System.Windows.Forms.ListBox
Private newcloselist As System.Windows.Forms.ListBox
Private newdatelist As System.Windows.Forms.ListBox
Private checkBox1 As System.Windows.Forms.CheckBox
Private label6 As System.Windows.Forms.Label
Private label5 As System.Windows.Forms.Label
Private newopenlist As System.Windows.Forms.ListBox
Private flnn As System.Windows.Forms.ListBox
Private spread As System.Windows.Forms.ListBox

Public sourcedir As String
Public destdir As String
Public fln As String
Public spreadpoint,multiplier As Single

' ARRAYS FOR PRICE DATA
Public high(1) As Single
Public low(1) As Single
Public open(1) As Single
Public close(1) As Single
Public pricedate(1) As String
Public priceitems as Integer

' DECLARE CONSTANTS
Public startdate As Integer
Public DMIShort As Integer = 4
Public DMILong as Integer = 14
Public Shared Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub

Public Sub New()
MyBase.New
'
' The Me.InitializeComponent call is required for Windows Forms
designer support.
'
Me.InitializeComponent
'
' TODO : Add constructor code after InitializeComponents
'
End Sub

#Region " Windows Forms Designer generated code "
' This method is required for Windows Forms designer support.
' Do not change the method contents inside the source code editor. The
Forms designer might
' not be able to load this method if it was changed manually.
Private Sub InitializeComponent()
Me.spread = New System.Windows.Forms.ListBox
Me.flnn = New System.Windows.Forms.ListBox
Me.newopenlist = New System.Windows.Forms.ListBox
Me.label5 = New System.Windows.Forms.Label
Me.label6 = New System.Windows.Forms.Label
Me.checkBox1 = New System.Windows.Forms.CheckBox
Me.newdatelist = New System.Windows.Forms.ListBox
Me.newcloselist = New System.Windows.Forms.ListBox
Me.highlist1 = New System.Windows.Forms.ListBox
Me.closelist1 = New System.Windows.Forms.ListBox
Me.newlowlist = New System.Windows.Forms.ListBox
Me.textBox4 = New System.Windows.Forms.TextBox
Me.textBox5 = New System.Windows.Forms.TextBox
Me.newhighlist = New System.Windows.Forms.ListBox
Me.mult = New System.Windows.Forms.ListBox
Me.textBox8 = New System.Windows.Forms.TextBox
Me.filelist1 = New System.Windows.Forms.ListBox
Me.groupBox1 = New System.Windows.Forms.GroupBox
Me.button3 = New System.Windows.Forms.Button
Me.groupBox3 = New System.Windows.Forms.GroupBox
Me.openlist1 = New System.Windows.Forms.ListBox
Me.lowlist1 = New System.Windows.Forms.ListBox
Me.dtlist1 = New System.Windows.Forms.ListBox
Me.groupBox1.SuspendLayout
Me.groupBox3.SuspendLayout
Me.SuspendLayout
'
'spread
'
Me.spread.Location = New System.Drawing.Point(326, 201)
Me.spread.Name = "spread"
Me.spread.Size = New System.Drawing.Size(56, 17)
Me.spread.TabIndex = 47
Me.spread.Visible = false
'
'flnn
'
Me.flnn.Location = New System.Drawing.Point(104, 118)
Me.flnn.Name = "flnn"
Me.flnn.Size = New System.Drawing.Size(40, 4)
Me.flnn.TabIndex = 48
Me.flnn.Visible = false
'
'newopenlist
'
Me.newopenlist.Location = New System.Drawing.Point(80, 10)
Me.newopenlist.Name = "newopenlist"
Me.newopenlist.Size = New System.Drawing.Size(64, 4)
Me.newopenlist.TabIndex = 13
Me.newopenlist.Visible = false
'
'label5
'
Me.label5.Location = New System.Drawing.Point(8, 16)
Me.label5.Name = "label5"
Me.label5.Size = New System.Drawing.Size(168, 10)
Me.label5.TabIndex = 34
Me.label5.Text = "Start date (mmyyyy) - 0 = start"
'
'label6
'
Me.label6.Location = New System.Drawing.Point(8, 31)
Me.label6.Name = "label6"
Me.label6.Size = New System.Drawing.Size(160, 10)
Me.label6.TabIndex = 35
Me.label6.Text = "End date (mmyyyy) - 0 = end"
'
'checkBox1
'
Me.checkBox1.Location = New System.Drawing.Point(16, 102)
Me.checkBox1.Name = "checkBox1"
Me.checkBox1.Size = New System.Drawing.Size(168, 10)
Me.checkBox1.TabIndex = 49
Me.checkBox1.Text = "Convert data to weekly first"
'
'newdatelist
'
Me.newdatelist.Location = New System.Drawing.Point(8, 10)
Me.newdatelist.Name = "newdatelist"
Me.newdatelist.Size = New System.Drawing.Size(64, 4)
Me.newdatelist.TabIndex = 12
Me.newdatelist.Visible = false
'
'newcloselist
'
Me.newcloselist.Location = New System.Drawing.Point(152, 10)
Me.newcloselist.Name = "newcloselist"
Me.newcloselist.Size = New System.Drawing.Size(64, 4)
Me.newcloselist.TabIndex = 14
Me.newcloselist.Visible = false
'
'highlist1
'
Me.highlist1.Location = New System.Drawing.Point(232, 26)
Me.highlist1.Name = "highlist1"
Me.highlist1.Size = New System.Drawing.Size(16, 4)
Me.highlist1.TabIndex = 11
Me.highlist1.Visible = false
'
'closelist1
'
Me.closelist1.Location = New System.Drawing.Point(160, 26)
Me.closelist1.Name = "closelist1"
Me.closelist1.Size = New System.Drawing.Size(16, 4)
Me.closelist1.TabIndex = 10
Me.closelist1.Visible = false
'
'newlowlist
'
Me.newlowlist.Location = New System.Drawing.Point(296, 10)
Me.newlowlist.Name = "newlowlist"
Me.newlowlist.Size = New System.Drawing.Size(64, 4)
Me.newlowlist.TabIndex = 16
Me.newlowlist.Visible = false
'
'textBox4
'
Me.textBox4.Location = New System.Drawing.Point(168, 16)
Me.textBox4.Name = "textBox4"
Me.textBox4.Size = New System.Drawing.Size(64, 21)
Me.textBox4.TabIndex = 36
Me.textBox4.Text = "textBox4"
'
'textBox5
'
Me.textBox5.Location = New System.Drawing.Point(168, 31)
Me.textBox5.Name = "textBox5"
Me.textBox5.Size = New System.Drawing.Size(64, 21)
Me.textBox5.TabIndex = 37
Me.textBox5.Text = "textBox5"
'
'newhighlist
'
Me.newhighlist.Location = New System.Drawing.Point(224, 10)
Me.newhighlist.Name = "newhighlist"
Me.newhighlist.Size = New System.Drawing.Size(64, 4)
Me.newhighlist.TabIndex = 15
Me.newhighlist.Visible = false
'
'mult
'
Me.mult.Location = New System.Drawing.Point(56, 118)
Me.mult.Name = "mult"
Me.mult.Size = New System.Drawing.Size(40, 4)
Me.mult.TabIndex = 46
Me.mult.Visible = false
'
'textBox8
'
Me.textBox8.Location = New System.Drawing.Point(8, 159)
Me.textBox8.Multiline = true
Me.textBox8.Name = "textBox8"
Me.textBox8.Size = New System.Drawing.Size(384, 27)
Me.textBox8.TabIndex = 40
Me.textBox8.Text =
""&Global.Microsoft.VisualBasic.ChrW(13)&Global.Mi crosoft.VisualBasic.ChrW(10)&"destdir=""C:\Documen ts
and Settings\Alex
Chambers\Desktop\F_output"""&Global.Microsoft.Visu alBasic.ChrW(13)&Global.Microsoft.VisualBasic.ChrW (10)&"sourcedir="""&
_
"C:\Documents and Settings\Alex Chambers\Desktop\Forex historical"""
Me.textBox8.Visible = false
'
'filelist1
'
Me.filelist1.Location = New System.Drawing.Point(388, 201)
Me.filelist1.Name = "filelist1"
Me.filelist1.Size = New System.Drawing.Size(20, 17)
Me.filelist1.TabIndex = 51
Me.filelist1.Visible = false
'
'groupBox1
'
Me.groupBox1.Controls.Add(Me.dtlist1)
Me.groupBox1.Controls.Add(Me.button3)
Me.groupBox1.Controls.Add(Me.checkBox1)
Me.groupBox1.Controls.Add(Me.flnn)
Me.groupBox1.Controls.Add(Me.mult)
Me.groupBox1.Controls.Add(Me.groupBox3)
Me.groupBox1.Controls.Add(Me.newlowlist)
Me.groupBox1.Controls.Add(Me.newhighlist)
Me.groupBox1.Controls.Add(Me.newcloselist)
Me.groupBox1.Controls.Add(Me.newopenlist)
Me.groupBox1.Controls.Add(Me.newdatelist)
Me.groupBox1.Controls.Add(Me.highlist1)
Me.groupBox1.Controls.Add(Me.closelist1)
Me.groupBox1.Controls.Add(Me.openlist1)
Me.groupBox1.Controls.Add(Me.lowlist1)
Me.groupBox1.Location = New System.Drawing.Point(8, 6)
Me.groupBox1.Name = "groupBox1"
Me.groupBox1.Size = New System.Drawing.Size(400, 133)
Me.groupBox1.TabIndex = 0
Me.groupBox1.TabStop = false
'
'button3
'
Me.button3.Location = New System.Drawing.Point(304, 97)
Me.button3.Name = "button3"
Me.button3.Size = New System.Drawing.Size(80, 15)
Me.button3.TabIndex = 50
Me.button3.Text = "GO!"
AddHandler Me.button3.Click, AddressOf Me.Button3Click
'
'groupBox3
'
Me.groupBox3.Controls.Add(Me.textBox5)
Me.groupBox3.Controls.Add(Me.textBox4)
Me.groupBox3.Controls.Add(Me.label6)
Me.groupBox3.Controls.Add(Me.label5)
Me.groupBox3.Location = New System.Drawing.Point(8, 41)
Me.groupBox3.Name = "groupBox3"
Me.groupBox3.Size = New System.Drawing.Size(384, 51)
Me.groupBox3.TabIndex = 44
Me.groupBox3.TabStop = false
Me.groupBox3.Text = "Universal settings"
'
'openlist1
'
Me.openlist1.Location = New System.Drawing.Point(88, 26)
Me.openlist1.Name = "openlist1"
Me.openlist1.Size = New System.Drawing.Size(16, 4)
Me.openlist1.TabIndex = 6
Me.openlist1.Visible = false
'
'lowlist1
'
Me.lowlist1.Location = New System.Drawing.Point(304, 26)
Me.lowlist1.Name = "lowlist1"
Me.lowlist1.Size = New System.Drawing.Size(24, 4)
Me.lowlist1.TabIndex = 4
Me.lowlist1.Visible = false
'
'dtlist1
'
Me.dtlist1.Location = New System.Drawing.Point(254, 26)
Me.dtlist1.Name = "dtlist1"
Me.dtlist1.Size = New System.Drawing.Size(24, 17)
Me.dtlist1.TabIndex = 52
Me.dtlist1.Visible = false
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
Me.ClientSize = New System.Drawing.Size(416, 230)
Me.Controls.Add(Me.filelist1)
Me.Controls.Add(Me.groupBox1)
Me.Controls.Add(Me.textBox8)
Me.Controls.Add(Me.spread)
Me.MaximizeBox = false
Me.Name = "MainForm"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Moving Average Tester"
AddHandler Closed, AddressOf Me.Form1OnClosing
AddHandler Load, AddressOf Me.Form1Load
Me.groupBox1.ResumeLayout(false)
Me.groupBox3.ResumeLayout(false)
Me.groupBox3.PerformLayout
Me.ResumeLayout(false)
Me.PerformLayout
End Sub
#End Region

Private Sub Button3Click(sender As System.Object, e As
System.EventArgs)

Dim sa,sb,sc,arinput(),a As String
dim rr as integer
filelist1.Items.clear()
destdir="C:\Documents and Settings\Alex Chambers\Desktop\F_output"
sourcedir="C:\Documents and Settings\Alex Chambers\Desktop\Forex
historical"
flnn.Items.clear()
mult.Items.clear()
spread.Items.clear()
Dim oFile as System.IO.File
Dim oRead As System.IO.StreamReader
oRead = oFile.OpenText(sourcedir+"\MA_config.txt")
a = oRead.ReadLine()

a = oRead.ReadLine()
a = oRead.ReadLine()
a = oRead.ReadLine()
a = oRead.ReadLine()

While oRead.Peek <> -1

a = oRead.ReadLine()
arinput = a.Split(",")

' Convert weird date to recognised

flnn.Items.Add(arinput(0))
mult.Items.Add(arinput(1))
spread.Items.Add(arinput(2))
End While
oRead.Close()
' Now read in files

Dim fso
fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(destdir) = True then

' Delete all files in destination folder, then create it

fso.DeleteFolder(destdir)
fso.CreateFolder(destdir)

' Now get a list of all files in the folder

If fso.FolderExists(sourcedir) = True then

Dim d() As String
d = System.IO.Directory.GetFiles(addslashtoend(sourced ir))
Dim en As System.Collections.IEnumerator
en = d.GetEnumerator
While en.MoveNext
filelist1.Items.Add(CStr(en.Current))
End While

' Now loop for each file in directory found

If filelist1.Items.Count = 0 Then
MsgBox ("No files found!", vbExclamation, "NOT PROCEEDED")
Else

For rr = 0 To filelist1.Items.Count - 1

sb = filelist1.Items(rr)
sc = instrrev(sb,".")
sa = instrrev(sb,"\")
fln = mid(sb,sa+1,(sc-(sa+1)))

if fln<>"MA_config" then openfile (sb)

Next rr

End If
End If
End If
End Sub

Function addslashtoend (a As String)
If mid(a, len(a),1)<> "\" Then
addslashtoend=a+"\"
Else
addslashtoend=a
End If
End Function
Private Sub openfile(flname As String)

Dim i,ed,sd,j As Integer
Dim a, arinput() As String

dtlist1.Items.Clear
closelist1.Items.clear()
openlist1.Items.clear()
highlist1.Items.clear()
lowlist1.Items.clear()

' Open the file for reading

'************************************************* ****************
' Note FOREX ASCII data file format: "Date","Time","O","H","L","C"
' And date is wierd format, so need so convert
'************************************************* ****************

Dim oFile as System.IO.File
Dim oRead As System.IO.StreamReader
oRead = oFile.OpenText(flname)
a = oRead.ReadLine()

While oRead.Peek <> -1
a = oRead.ReadLine()
arinput = a.Split(",")

If a <> "" Then

' Date should be in YYYYMMDD format

dtlist1.Items.Add(arinput(0))

' Then O,H,L,C

' MA formula from FXCHARTS for Forex is calculated as follows:
' Close price for each day
' Count current day then back - 1
' ie. for MA(2), do sum(today and yesterday close prices) / 2

openlist1.Items.Add(arinput(1)) ' Add OPENING price
highlist1.Items.Add(arinput(2))
lowlist1.Items.Add(arinput(3))
closelist1.Items.Add(arinput(4)) ' Add CLOSING price

End If

End While
oRead.Close()

' Finished reading in file - now closed

spreadpoint = 0
multiplier = 0

For i = 0 To flnn.Items.count -1
If fln = flnn.items(i) Then
spreadpoint = val(spread.items(i))
multiplier = val(mult.items(i))
End If
Next

' NOW OPTIMISE FOR SPEED - READ PRICES INTO AN ARRAY

startdate = DMILong

If trim(textbox4.Text) = "0" Then
sd = 0
Else
sd = 0
End If

If trim(textbox5.Text) = "0" Then
ed = closelist1.Items.Count-1
Else
ed = 0
End If

priceitems = (ed - sd)-1

ReDim high(priceitems)
ReDim low(priceitems)
ReDim open(priceitems)
ReDim close(priceitems)
ReDim pricedate(priceitems)

j=0
For i = sd To ed
high(j) = highlist1.Items(i)
low(j) = lowlist1.Items(i)
open(j) = openlist1.Items(i)
close(j) = closelist1.Items(i)
pricedate(j) = dtlist1.Items(i)
j = j + 1
Next i

dtlist1.Items.clear()
closelist1.Items.clear()
openlist1.Items.clear()
highlist1.Items.clear()
lowlist1.Items.clear()

End Sub
Private Sub Form1OnClosing(sender As System.Object, e As
System.EventArgs)
End Sub

Private Sub Form1Load(sender As System.Object, e As System.EventArgs)
End Sub

End Class
End Namespace

May 18 '06 #3

P: n/a
Alex,

I looked through the code you submitted but don't see a line for
dtList.Items.Clear. Where is it? I didn't see it in the constructor.
I know from experience if you try to reference a control in a
constructor before the InitializeComponent sub is called you'll get an
error because the component doesn't exist yet.

Dave, MCAD

May 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.