Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

What's OpenFileDialog.FilterIndex?

What's OpenFileDialog.FilterIndex?

What's OpenFileDialog.FilterIndex?

I guess many things have changed since I used Common Dialog/FileOpen control in VB (ver. 6) last time (in 2006).
For instance, I do not remember having there FilterIndex property... so, when I tried to use it in VS 2012's VB .NET, I followed the sample on the https://docs.microsoft.com/en-us/dotnet/api/system..., e.g. made my Dialog.FilterIndex = 2.
Got index out of range exception.
Since I had just one extension listed in the Filter property, changed it to 1 - same thing, see the screenshot.

Could anybody explain to me (coz MS Docs' description is incomprehensible for yours truly) what's this FilterIndex property does?




RE: What's OpenFileDialog.FilterIndex?

Er, you need to have shown the dialog to select a file before you try opening a file ...

(By the way, it would be a LOT easier for us if you copied and pasted your code here, rather than just providing screenshots)

RE: What's OpenFileDialog.FilterIndex?

Like I've said many a times: old senile sclerotic me! Forgot about Dialog.ShowDialog method!
To my defense, ShowDialog method wasn't listedon the treeview menu on the page https://docs.microsoft.com/en-us/dotnet/api/system......
Moreover: if after ShowDialog the User clicks on "Cancel" without selecting any file - this error pops up again.
Now, per your request, the code here:


Dim lcPath2SrcXML As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim lcRetFilNam As String = "", lcSrcXML_Hdr As String = "", lcSrcXML_Body As String = "", lcSrcXML_Ftr As String = ""
Dim llGoOn As Boolean = True

lcPath2SrcXML = lcPath2SrcXML.Replace("\bin\Debug", "\")

   Using loFileDlg As New OpenFileDialog()
      loFileDlg.InitialDirectory = lcPath2SrcXML
      loFileDlg.Filter = "XML Files|*.XML"
      loFileDlg.RestoreDirectory = True
'      loFileDlg.FilterIndex = 0 ' 1
      lcPath2SrcXML = loFileDlg.FileName
      lcPath2SrcXML = Path.GetDirectoryName(lcPath2SrcXML) & "\"

   End Using
Catch loErr As Exception
   MsgBox("Error " & Chr(34) & loErr.Message & Chr(34) & " occurred")
   llGoOn = False
End Try

If Not llGoOn Then End 

Now, what's that "array" that error's talking about?



RE: What's OpenFileDialog.FilterIndex?

>if after ShowDialog the User clicks on "Cancel" without selecting any file - this error pops up again.

How about:



If loFileDlg.FileName.Length = 0 Then
    MessageBox.Show("Select a file, or we are not going anywhere...")
    Exit Sub
End If



---- Andy

There is a great need for a sarcasm font.

RE: What's OpenFileDialog.FilterIndex?

Thank you!



RE: What's OpenFileDialog.FilterIndex?

Yes, you can do it that way - or consider that ShowDialog returns a result ...

>To my defense, ShowDialog method wasn't listedon the treeview menu
Very true - because it is an inherited method from FileDialog which in turn inherits from CommonDialog, but does not override it - so it is ultimately documented under CommonDialog ...

>Now, what's that "array" that error's talking about?

Ok, so although not explicitly documented, OpenFile is frankly just a convenience, and tries to use the first element of OpenFileDialog's FileNames array. Which, if you've cancelled, clearly does not exist ...

You can replicate the effect with a simple:


>the code here:
Much better! Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close