Specified cast is not valid
-
I am working on an application where user needs to select a folder. I am using dirListBox and DriveListBox from VB6. Reference's are Microsoft.Visualbasic.Compatibality and Microsoft.VisualBasic. It's populates the combo box for DriveListBox, but when i try to scroll down to select a drive , application bombs on me giving error An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.compatibility.dll "Specified Cast is not Valid" Any idea why it is doing that? Here is the code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblStatus.Text = "Not Running" FindAllDrives() DriveListBox.SelectedIndex = 1 End Sub Private Sub FindAllDrives() Dim strDrives() As String strDrives = Directory.GetLogicalDrives Dim strDrive As String For Each strDrive In strDrives DriveListBox.Items.Add(strDrive) Next DriveListBox.SelectedIndex = 1 End Sub Private Sub DriveListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox.SelectedIndexChanged ' Dim folders() As String Try DirListBox.Path = DriveListBox.Text Catch MsgBox("Can't access selected drive") End Try End Sub Private Sub DirListBox_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox.SelectedIndexChanged lblFolder.Text = DirListBox.Path.ToString strFolder = lblFolder.Text End Sub Please help if you have any idea! Thank you. Zulfikar Ali
-
I am working on an application where user needs to select a folder. I am using dirListBox and DriveListBox from VB6. Reference's are Microsoft.Visualbasic.Compatibality and Microsoft.VisualBasic. It's populates the combo box for DriveListBox, but when i try to scroll down to select a drive , application bombs on me giving error An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.compatibility.dll "Specified Cast is not Valid" Any idea why it is doing that? Here is the code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblStatus.Text = "Not Running" FindAllDrives() DriveListBox.SelectedIndex = 1 End Sub Private Sub FindAllDrives() Dim strDrives() As String strDrives = Directory.GetLogicalDrives Dim strDrive As String For Each strDrive In strDrives DriveListBox.Items.Add(strDrive) Next DriveListBox.SelectedIndex = 1 End Sub Private Sub DriveListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox.SelectedIndexChanged ' Dim folders() As String Try DirListBox.Path = DriveListBox.Text Catch MsgBox("Can't access selected drive") End Try End Sub Private Sub DirListBox_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox.SelectedIndexChanged lblFolder.Text = DirListBox.Path.ToString strFolder = lblFolder.Text End Sub Please help if you have any idea! Thank you. Zulfikar Ali
I figure out the problem. I am using DriveListBox and in the same time using a function to populate the DriveListBox combobox. I eliminated the FinDAllDrives and woolaaa. Thanks.;) Zulfikar Ali