Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's OpenFileDialog.FilterIndex? 2

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,
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 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.

20200413_WhatIsFilterIndex_oeapvc.jpg


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

TIA!

Regards,

Ilya
 
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)
 
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 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:

Code:
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", "\")

Try
   Using loFileDlg As New OpenFileDialog()
      loFileDlg.InitialDirectory = lcPath2SrcXML
      loFileDlg.Filter = "XML Files|*.XML"
      loFileDlg.RestoreDirectory = True
      loFileDlg.ShowDialog()
'      loFileDlg.FilterIndex = 0 ' 1
      loFileDlg.OpenFile()
      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?

Regards,

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

How about:

Code:
...
loFileDlg.ShowDialog()
[blue]
If loFileDlg.FileName.Length = 0 Then
    MessageBox.Show("Select a file, or we are not going anywhere...")
    Exit Sub
End If
[/blue]
loFileDlg.OpenFile()
...

[ponder]


---- Andy

There is a great need for a sarcasm font.
 
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?
Filenames

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:

MsgBox(loFileDlg.FileNames(0))

>the code here:
Much better! Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top