Problème InvalidCastException

truffeb Messages postés 8 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 15 août 2012 - 14 août 2012 à 19:50
truffeb Messages postés 8 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 15 août 2012 - 15 août 2012 à 19:13
Bonjour

Je suis en train de faire un WebBrowser youtube.

Sur la form "youtube", j'ai un bouton qui envoie sur une autre form "listeYoutub" avec une listBox.

J'ai écrit ce code pour sauvegarder la listeBox à la fermeture, mais cela coince à ce niveau. La fenêtre ne se ferme pas, j'ai l'erreur "InvalidCastExeption n'a pas été gérée la conversion du type 'uri' en type 'string' n'est pas valide."

Je ne comprends pas, j'ai utilisé le même code pour sauvegarder la listebox d'un Media Player avec succès.

Pouvez- vous m'aider à corriger mon code?

Merci d'avance.

[*] Form "youtube"

'Liste
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
ListeYoutube.Show()
Try
ListeYoutube.ListBox1.Items.Add(WebBrowser1.Url)
Catch ex As Exception

End Try

End Sub

[*] Form "listeYoutub"

Public Class ListeYoutube


'Naviguer sur le web
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Youtube.WebBrowser1.Navigate("https://www.google.fr/ ")
End Sub

'clear
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
ListBox1.Items.Remove(ListBox1.SelectedItem)
Catch ex As Exception

End Try
End Sub


' Lit l'item selectionné dans webbrowser
Private Sub ListeYoutube_DoubleClick(sender As Object, e As System.EventArgs) Handles Me.DoubleClick
Try
Youtube.WebBrowser1.Navigate(ListBox1.SelectedItem)
Catch ex As Exception

End Try
End Sub


'enregistrer automatiquement
Private Sub ListeYoutube_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If System.IO.File.Exists("MesFavorisYoutube.txt") = True Then
Dim oLoadListBox As New System.IO.StreamReader("MesFavorisYoutube.txt")
Do Until oLoadListBox.Peek = -1
Dim oLoad As String = oLoadListBox.ReadLine.ToString
ListBox1.Items.Add(oLoad)
Loop
oLoadListBox.Close()
Else
ListBox1.Items.Add("X")

End If
End Sub


Private Sub ListeYoutube_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Using oSauvListBox1 As New System.IO.StreamWriter("MesFavorisYoutube.txt")
For Each oItemListBox As String In ListBox1.Items
oSauvListBox1.WriteLine(oItemListBox)
Next
End Using
End Sub

'fermer
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Me.Close()

End Sub
End Class

4 réponses

Utilisateur anonyme
15 août 2012 à 07:44
Bonjour,

Je n'ai pas lu ton code (balises de coloration syntaxique absentes).
Fais un tour sur msdn pour apprendre les caractéristiques d'un objet de type 'string' et la différence qu'il y a avec un objet de type 'uri'.
Vérifie que tu alimentes bien ta listbox avec des 'string'.
0
truffeb Messages postés 8 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 15 août 2012
15 août 2012 à 12:17
Bonjour et merci pour ta réponse .

La listBox, je l'alimente par Mes liens de favoris web, surtout youtube.

Je vous remercie pour votre aide.

Je vais essayer de mettre les balises de coloration :


Form "youtube"

'Liste

Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click 
ListeYoutube.Show() 
Try 
ListeYoutube.ListBox1.Items.Add(WebBrowser1.Url) 
Catch ex As Exception 

End Try 

End Sub 



Form "listeYoutub"

Public Class ListeYoutube 


'clear 

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
Try 
ListBox1.Items.Remove(ListBox1.SelectedItem) 
Catch ex As Exception 

End Try 
End Sub 


' Lit l'item selectionné dans webbrowser 

Private Sub ListeYoutube_DoubleClick(sender As Object, e As System.EventArgs) Handles Me.DoubleClick 
Try 
Youtube.WebBrowser1.Navigate(ListBox1.SelectedItem) 
Catch ex As Exception 

End Try 
End Sub 


'enregistrer automatiquement 

Private Sub ListeYoutube_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
If System.IO.File.Exists("MesFavorisYoutube.txt") = True Then 
Dim oLoadListBox As New System.IO.StreamReader("MesFavorisYoutube.txt") 
Do Until oLoadListBox.Peek = -1 
Dim oLoad As String = oLoadListBox.ReadLine.ToString 
ListBox1.Items.Add(oLoad) 
Loop 
oLoadListBox.Close() 
Else 
ListBox1.Items.Add("X") 

End If 
End Sub 


Voila, où est le problème:

Private Sub ListeYoutube_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 
Using oSauvListBox1 As New System.IO.StreamWriter("MesFavorisYoutube.txt") 
For Each oItemListBox As String In ListBox1.Items 
oSauvListBox1.WriteLine(oItemListBox) 
Next 
End Using 
End Sub 


'fermer 

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click 
Me.Close() 

End Sub 
End Class
0
Utilisateur anonyme
15 août 2012 à 17:17
Je suis personnellement allé consulter msdn ici et j'ai constaté (sans grande surprise) que la propriété URL d'un composant webbrowser est de type 'uri'.
Et, plus bas sur la page, il y a même un exemple décrivant la façon de convertir simplement un objet 'uri' en un objet de type 'string'.
toolStripTextBox1.Text = webBrowser1.Url.ToString()

Donc, lorsque tu alimentes ta listbox, il te suffit de convertir simplement ton objet comme ceci :
ListeYoutube.ListBox1.Items.Add(WebBrowser1.Url.ToString) 


A bientôt.
0
truffeb Messages postés 8 Date d'inscription mercredi 2 avril 2003 Statut Membre Dernière intervention 15 août 2012
15 août 2012 à 19:13
Merci beaucoup, j'ai suivi tes conseils et le problème est résolue.
0
Rejoignez-nous