Lesezeichen mit Xml(Webbrowser)

    • VB.NET

    Es gibt 20 Antworten in diesem Thema. Der letzte Beitrag () ist von filmee24.

      Lesezeichen mit Xml(Webbrowser)

      Hey Ho Leute,

      Wie ihr sicher wisst wollen viele einen eigenen Webbrowser machen.Aber
      es gibt fast immer das gleiche Problem: Lesezeichen? Wie?

      Jetzt habe ich mal überall gekuckt und was gefunden.

      Lesezeichen mit Titel der Website und Url.

      Das alles wird in einer Xml datei abgespeichert.

      Dann wollen wir mal anfangen.


      Wir brauchen:

      Eine Form1 und eine Form2


      Form1:

      Einen Toolstrip
      mit einer ToolstripCombobox
      dann noch einen Toolstripbutton mit dem Text : Lesezeichen Manager

      Unteranderem noch einen Webbrowser, einen weiteren ToolStrip und noch einen MenuStrip mit dem Item: Lesezeichen

      Form2:

      Der Name ist Manager
      wir fügen 2 TextBoxen hinzu (Name und URL) und 2 Buttons (Hinzufügen und Löschen)
      und einen ListView mit 2 Colums(Website und URL)


      So das wars schonmal mit dem Design.

      Jetzt zum Code:


      Form1:


      VB.NET-Quellcode

      1. Imports System.Xml
      2. Public Class Form1
      3. Friend WithEvents tsbWebSite1 As System.Windows.Forms.ToolStripButton
      4. Dim xmldoc As New XmlDocument
      5. Private Sub AddTSButton(ByVal buttonName As String, ByVal webSiteName As String)
      6. Dim newbutton As New System.Windows.Forms.ToolStripButton
      7. Dim newle As New System.Windows.Forms.ToolStripMenuItem
      8. newle.Name = buttonName
      9. newle.Text = webSiteName
      10. newle.DisplayStyle = ToolStripItemDisplayStyle.Text
      11. newbutton.Name = buttonName
      12. newbutton.Text = webSiteName
      13. newbutton.DisplayStyle = ToolStripItemDisplayStyle.Text
      14. ToolStrip2.Items.Add(newbutton)
      15. LesezeichenToolStripMenuItem.DropDownItems.Add(newle)
      16. AddHandler newbutton.Click, AddressOf tsbutton_Click
      17. AddHandler newle.Click, AddressOf tsbutton_Click
      18. End Sub
      19. Public Sub AddToolStripButtons()
      20. xmldoc.Load(My.Application.Info.DirectoryPath & "\Bookmarks.xml")
      21. ToolStrip2.Items.Clear()
      22. LesezeichenToolStripMenuItem.DropDownItems.Clear()
      23. Dim websiteNodes As XmlNodeList
      24. Dim webSiteNode As XmlNode
      25. websiteNodes = xmldoc.GetElementsByTagName("WebSite")
      26. Dim iCount As Integer = 1
      27. For Each webSiteNode In websiteNodes
      28. Dim eachSiteNodes As XmlNodeList
      29. Dim eachSiteNode As XmlNode
      30. eachSiteNodes = webSiteNode.ChildNodes
      31. For Each eachSiteNode In eachSiteNodes
      32. If eachSiteNode.Name = "Name" Then
      33. AddTSButton("tsWebsite" & iCount.ToString, eachSiteNode.InnerText)
      34. iCount += 1
      35. End If
      36. Next
      37. Next
      38. End Sub
      39. Private Sub WebBrowser1_NewWindow(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
      40. e.Cancel = True
      41. End Sub
      42. Private Sub tsbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
      43. Try
      44. Dim aButton As ToolStripButton = CType(sender, ToolStripButton)
      45. GoToUrl(aButton.Text)
      46. Catch ex As Exception
      47. Dim a As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
      48. GoToUrl(a.Text)
      49. End Try
      50. End Sub
      51. Private Sub GoToUrl(ByVal sWebsite As String)
      52. Dim xPath As String = "//WebSite[Name='" & sWebsite & "']/URL"
      53. Dim xn As XmlNode = xmldoc.SelectSingleNode(xPath)
      54. WebBrowser1.Navigate(xn.InnerText)
      55. 'ctype(tabcontrol1.selectedtab.controls(0), webbrowser).navigate(xn.innertext)
      56. End Sub
      57. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      58. AddToolStripButtons()
      59. WebBrowser1.Navigate("http://www.google.de/")
      60. End Sub
      61. Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
      62. Manager.Show()
      63. AddToolStripButtons()
      64. End Sub
      65. Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
      66. WebBrowser1.Navigate(ToolStripComboBox1.Text)
      67. ToolStripComboBox1.Items.Add(ToolStripComboBox1.Text)
      68. End Sub
      69. Private Sub ToolStripComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ToolStripComboBox1.KeyDown
      70. If e.KeyCode = Keys.Enter Then
      71. ToolStripButton1_Click(Nothing, Nothing)
      72. End If
      73. End Sub
      74. Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel1.Click
      75. End Sub
      76. End Class



      Jetzt zur Manager Form:


      VB.NET-Quellcode

      1. Imports System.Xml
      2. Public Class Manager
      3. Private Sub LoadList()
      4. ListView1.Items.Clear()
      5. Dim rdrXML As New XmlTextReader(My.Application.Info.DirectoryPath & "\Bookmarks.xml")
      6. rdrXML.MoveToContent()
      7. Dim ElementName As String = ""
      8. Dim NextItem As Boolean = True
      9. Dim objListViewItem As ListViewItem = Nothing
      10. Do While rdrXML.Read
      11. If NextItem Then
      12. objListViewItem = New ListViewItem
      13. NextItem = False
      14. End If
      15. Select Case rdrXML.NodeType
      16. Case XmlNodeType.Element
      17. ElementName = rdrXML.Name
      18. Case XmlNodeType.Text
      19. If ElementName = "Name" Then
      20. objListViewItem.Text = rdrXML.Value
      21. End If
      22. If ElementName = "URL" Then
      23. objListViewItem.SubItems.Add(rdrXML.Value)
      24. ListView1.Items.Add(objListViewItem)
      25. NextItem = True
      26. End If
      27. End Select
      28. Loop
      29. rdrXML.Close()
      30. End Sub
      31. Private Sub savelist()
      32. Dim wtrXML As New XmlTextWriter(My.Application.Info.DirectoryPath & "\Bookmarks.xml", System.Text.Encoding.UTF8)
      33. With wtrXML
      34. .Formatting = Formatting.Indented
      35. .WriteStartDocument()
      36. .WriteStartElement("WebSites")
      37. Dim objListViewItem As New ListViewItem
      38. For Each objListViewItem In ListView1.Items
      39. .WriteStartElement("WebSite")
      40. .WriteElementString("Name", objListViewItem.Text)
      41. .WriteElementString("URL", objListViewItem.SubItems(1).Text)
      42. .WriteEndElement()
      43. Next
      44. .WriteEndElement()
      45. .WriteEndDocument()
      46. .Flush()
      47. .Close()
      48. End With
      49. End Sub
      50. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      51. Dim objListViewItem As New ListViewItem
      52. objListViewItem.Text = TextBox1.Text
      53. objListViewItem.SubItems.Add(TextBox2.Text)
      54. ListView1.Items.Add(objListViewItem)
      55. TextBox1.Clear()
      56. TextBox2.Clear()
      57. savelist()
      58. End Sub
      59. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      60. Dim indexes As ListView.SelectedIndexCollection = ListView1.SelectedIndices
      61. Dim index As Integer
      62. For Each index In indexes
      63. ListView1.Items.RemoveAt(index)
      64. Next
      65. savelist()
      66. End Sub
      67. Private Sub Manager_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      68. Form1.AddToolStripButtons()
      69. End Sub
      70. Private Sub Manager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      71. LoadList()
      72. TextBox1.Text = Form1.WebBrowser1.DocumentTitle
      73. TextBox2.Text = Form1.WebBrowser1.Url.AbsoluteUri
      74. End Sub
      75. End Class



      Achtung: Die Xml Datei muss im gleichen Ordner sein wie euer Programm.



      So das wars.

      Ich stelle noch das ganze Projekt rein.

      Viel spaß!!!!!

      Download Source: Klick

      Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „One“ ()

      Ich frage mich grade ob man evt eine Ordner Struktur in diesen Lesezeichen Code basteln könnte.
      Das würde den Webbrowser schon fast an Perfektion grenzen lassen :)