Multiple open file question

tbonejo

Member
Joined
Apr 22, 2005
Messages
9
Programming Experience
3-5
How can I make an if statement to determine whether I have 1 file or more than one? Im doing this but it does not work correctly.


VB.NET:
Dim OpenFileDialog1 As New OpenFileDialog
		Dim i As Integer

		' Set properties as appropriate.
		OpenFileDialog1.CheckFileExists = True
		OpenFileDialog1.DefaultExt = ".txt,.asc"
		'myOpenFileDialog.InitialDirectory = "C:\"
		OpenFileDialog1.Filter() = "Track Test Files (*.txt)|*.txt|" & _
		 "Track Test Files(*.asc)|*.asc"
		OpenFileDialog1.Title = "Select Track Test Files."
		OpenFileDialog1.Multiselect = True
		OpenFileDialog1.RestoreDirectory = True
		OpenFileDialog1.ShowDialog()
		If UBound(OpenFileDialog1.FileNames) > 1 Then
			For i = 1 To UBound(OpenFileDialog1.FileNames)
			    Dim fi As New FileInfo(OpenFileDialog1.FileNames(i))
				fg1.Rows.Add()
				fg1(i, 0) = i
				fg1(i, 1) = fi.Name
			    fg1(i, 2) = OpenFileDialog1.FileNames(i)

			Next
		Else
			Dim fi As New FileInfo(OpenFileDialog1.FileName)
			fg1.Rows.Add()
			fg1(1, 0) = 1
			fg1(1, 1) = fi.Name
			fg1(1, 2) = OpenFileDialog1.FileName
		End If


Thanks.

Tom
 
Hey,

try changing:

VB.NET:
If UBound(OpenFileDialog1.FileNames) > 1 Then
			For i = 1 To UBound(OpenFileDialog1.FileNames)

to:

VB.NET:
If OpenFileDialog1.FileNames.Length > 1 Then
For i = 0 To OpenFileDialog1.FileNames.Length - 1
 
Just to clarify, the UBound should work fine too, but the array is 0 based.
UBound (or the length property) returns the total number of elements, to get the last index you have to subtract 1, and the first element is at 0.
 
Back
Top