Mega Code Archive

 
Categories / VB.Net / Windows System
 

Display Your Web Favorite Collection

Imports System Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Printing Public Class MainClass     Shared Sub Main()         Dim myform As Form = New Form1()         Application.Run(myform)     End Sub End Class Public Class WebFavoriteCollection     Inherits CollectionBase     Public Sub Add(ByVal Favorite As WebFavorite)         'Add item to the collection         List.Add(Favorite)     End Sub     Public Sub Remove(ByVal Index As Integer)         'Remove item from collection         If Index >= 0 And Index < Count Then             List.Remove(Index)         End If     End Sub     Public ReadOnly Property Item(ByVal Index As Integer) As WebFavorite         Get             'Get an item from the collection by its index             Return CType(List.Item(Index), WebFavorite)         End Get     End Property End Class Public Class Favorites     Implements IDisposable     Private disposed As Boolean = False     ' IDisposable     Private Overloads Sub Dispose(ByVal disposing As Boolean)         If Not Me.disposed Then             If disposing Then                 ' TODO: put code to dispose managed resources             End If             ' TODO: put code to free unmanaged resources here         End If         Me.disposed = True     End Sub #Region " IDisposable Support "     ' This code added by Visual Basic to correctly implement the disposable pattern.     Public Overloads Sub Dispose() Implements IDisposable.Dispose         ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.         Dispose(True)         GC.SuppressFinalize(Me)     End Sub     Protected Overrides Sub Finalize()         ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.         Dispose(False)         MyBase.Finalize()     End Sub #End Region     'Public member     Public FavoritesCollection As WebFavoriteCollection     Public ReadOnly Property FavoritesFolder() As String         Get             'Return the path to the user's Favorites folder             Return Environment.GetFolderPath( _                 Environment.SpecialFolder.Favorites)         End Get     End Property     Public Sub ScanFavorites()         'Scan the Favorites folder         ScanFavorites(FavoritesFolder)     End Sub     Public Sub ScanFavorites(ByVal folderName As String)         'If the FavoritesCollection member has not been instantiated         'then instaniate it         If FavoritesCollection Is Nothing Then             FavoritesCollection = New WebFavoriteCollection         End If         'Process each file in the Favorites folder         For Each strFile As String In _             My.Computer.FileSystem.GetFiles(folderName)             'If the file has a url extension...             If strFile.EndsWith(".url", True, Nothing) Then                 Try                     'Create and use a new instanace of the                      'WebFavorite class                     Using objWebFavorite As New WebFavorite                         'Load the file information                         objWebFavorite.Load(strFile)                         'Add the object to the collection                         FavoritesCollection.Add(objWebFavorite)                     End Using                 Catch ExceptionErr As Exception                     'Return the exception to the caller                     Throw New Exception(ExceptionErr.Message)                 End Try             End If         Next     End Sub End Class Public Class WebFavorite     Implements IDisposable     Private disposed As Boolean = False     ' IDisposable     Private Overloads Sub Dispose(ByVal disposing As Boolean)         If Not Me.disposed Then             If disposing Then                 ' TODO: put code to dispose managed resources             End If             ' TODO: put code to free unmanaged resources here         End If         Me.disposed = True     End Sub #Region " IDisposable Support "     ' This code added by Visual Basic to correctly implement the disposable pattern.     Public Overloads Sub Dispose() Implements IDisposable.Dispose         ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.         Dispose(True)         GC.SuppressFinalize(Me)     End Sub     Protected Overrides Sub Finalize()         ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.         Dispose(False)         MyBase.Finalize()     End Sub #End Region     'Public Members     Public Name As String     Public Url As String     Public Sub Load(ByVal fileName As String)         'Declare variables         Dim strData As String         Dim strLines() As String         Dim strLine As String         Dim objFileInfo As New FileInfo(fileName)         'Set the Name member to the file name minus the extension         Name = objFileInfo.Name.Substring(0, _             objFileInfo.Name.Length - objFileInfo.Extension.Length)         Try             'Read the entire contents of the file             strData = My.Computer.FileSystem.ReadAllText(fileName)             'Split the lines of data in the file             strLines = strData.Split(New String() {ControlChars.CrLf}, _                 StringSplitOptions.RemoveEmptyEntries)             'Process each line looking for the URL             For Each strLine In strLines                 'Does the line of data start with URL=                 If strLine.StartsWith("URL=") Then                     'Yes, set the Url member to the actual URL                     Url = strLine.Substring(4)                     'Exit the For...Next loop                     Exit For                 End If             Next         Catch IOExceptionErr As IOException             'Return the exception to the caller             Throw New Exception(IOExceptionErr.Message)         End Try     End Sub End Class Public Class Form1     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _         Handles Me.Load         Try             'Create and use a new instanace of the Favorites class              Using objFavorites As New Favorites                 'Scan the Favorites folder                 objFavorites.ScanFavorites()                 'Process each objWebFavorite object in the                  'favorites collection                 For Each objWebFavorite As WebFavorite In _                     objFavorites.FavoritesCollection                     'Declare a ListViewItem object                     Dim objListViewItem As New ListViewItem                     'Set the properties of the ListViewItem object                     objListViewItem.Text = objWebFavorite.Name                     objListViewItem.SubItems.Add(objWebFavorite.Url)                     'Add the ListViewItem object to the ListView                     lstFavorites.Items.Add(objListViewItem)                 Next             End Using         Catch ExceptionErr As Exception             'Display the error             MessageBox.Show(ExceptionErr.Message, "Favorites Viewer", _                 MessageBoxButtons.OK, MessageBoxIcon.Warning)         End Try     End Sub     Private Sub lstFavorites_Click(ByVal sender As Object, _         ByVal e As System.EventArgs) Handles lstFavorites.Click         'Update the link label control Text property         lnkUrl.Text = "Visit " & lstFavorites.SelectedItems.Item(0).Text         'Clear the default hyperlink          lnkUrl.Links.Clear()         'Add the selected hyperlink to the LinkCollection         lnkUrl.Links.Add(6, lstFavorites.SelectedItems.Item(0).Text.Length, _             lstFavorites.SelectedItems.Item(0).SubItems(1).Text)     End Sub     Private Sub lnkUrl_LinkClicked(ByVal sender As Object, _         ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _         Handles lnkUrl.LinkClicked         'Process the selected link        ' Process.Start(e.Link.LinkData)     End Sub End Class <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Public Class Form1     Inherits System.Windows.Forms.Form     'Form overrides dispose to clean up the component list.     <System.Diagnostics.DebuggerNonUserCode()> _     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)         If disposing AndAlso components IsNot Nothing Then             components.Dispose()         End If         MyBase.Dispose(disposing)     End Sub     'Required by the Windows Form Designer     Private components As System.ComponentModel.IContainer     'NOTE: The following procedure is required by the Windows Form Designer     'It can be modified using the Windows Form Designer.       'Do not modify it using the code editor.     <System.Diagnostics.DebuggerStepThrough()> _     Private Sub InitializeComponent()         Me.lstFavorites = New System.Windows.Forms.ListView         Me.hdrName = New System.Windows.Forms.ColumnHeader         Me.hdrUrl = New System.Windows.Forms.ColumnHeader         Me.lnkUrl = New System.Windows.Forms.LinkLabel         Me.SuspendLayout()         '         'lstFavorites         '         Me.lstFavorites.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _                     Or System.Windows.Forms.AnchorStyles.Left) _                     Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)         Me.lstFavorites.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.hdrName, Me.hdrUrl})         Me.lstFavorites.Location = New System.Drawing.Point(0, 0)         Me.lstFavorites.Name = "lstFavorites"         Me.lstFavorites.Size = New System.Drawing.Size(457, 223)         Me.lstFavorites.TabIndex = 0         Me.lstFavorites.View = System.Windows.Forms.View.Details         '         'hdrName         '         Me.hdrName.Text = "Name"         Me.hdrName.Width = 250         '         'hdrUrl         '         Me.hdrUrl.Text = "URL"         Me.hdrUrl.Width = 250         '         'lnkUrl         '         Me.lnkUrl.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)         Me.lnkUrl.AutoSize = True         Me.lnkUrl.Location = New System.Drawing.Point(0, 231)         Me.lnkUrl.Name = "lnkUrl"         Me.lnkUrl.Size = New System.Drawing.Size(55, 13)         Me.lnkUrl.TabIndex = 1         Me.lnkUrl.TabStop = True         Me.lnkUrl.Text = "LinkLabel1"         Me.lnkUrl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft         '         'Form1         '         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font         Me.ClientSize = New System.Drawing.Size(456, 253)         Me.Controls.Add(Me.lnkUrl)         Me.Controls.Add(Me.lstFavorites)         Me.Name = "Form1"         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen         Me.Text = "My Favorites"         Me.ResumeLayout(False)         Me.PerformLayout()     End Sub     Friend WithEvents lstFavorites As System.Windows.Forms.ListView     Friend WithEvents hdrName As System.Windows.Forms.ColumnHeader     Friend WithEvents hdrUrl As System.Windows.Forms.ColumnHeader     Friend WithEvents lnkUrl As System.Windows.Forms.LinkLabel End Class