Create your own web browser on your SmartPhone
Windows Mobile 5.0 comes with a Web Browser (v6 is due out any day now). It runs on Pocket PCs and SmartPhones. That browser only allows one web page to show at a time: to show another page, you have to navigate away from the current page. I created a web browser for my first SmartPhone over a year ago that allows you to open multiple web pages.
If you have Visual Studio 2008 (or 2005), you can do it too, with the few hundred lines of code below.
This sample WebBrowser features:
- Go Forward/Back
- adding and editing of favorites
- full screen view
- multiple web pages open
- address bar editing.
Start VS 2008 (or 2005). Choose File->New->Project->Visual Basic->Smart Device Project->Ok.
(You can also run this same sample as a standard VB Windows Form application to see it running on your PC. File->New->Project->Visual Basic->Windows->Windows Forms Application. You can actually have 2 projects in one solution: one for the SmartPhone, one for your PC, both sharing the same code: PhoneBrowser.vb. Then you can right click on the desired project and Set As Startup Project. Be careful of having 2 copies of the code: One copy actually references the Compact .Net Framework, and is in a different project type).
In the wizard that shows, choose Target Platform: Windows Mobile 5.0 Smartphone SDK, and Templates: Device Application
In Solution Explorer, Delete Form1, then add a new Class called "PhoneBrowser.vb"
Paste the code below.
In Project Properties, choose Application->Startup Object: PhoneBrowser
Hit F5 to build and run the program. A dialog comes up: choose either smart phone emulator. The first time, it takes a few seconds to deploy the .Net Framework 3.5.
Notes: use Tools->Device Emulation Manager to hook up the emulator to your network, so it can browse web sites. Right click on the emulator you chose and choose "Cradle" to attach the emulator electronically to your PC (like putting the phone into a cradle that's attached via cable). Otherwise you'll get "Your Internet connection is not configured properly. Please verify your settings in Data Connections"
Install Microsoft ActiveSync 4.5, run it, and choose File->Connection Settings->Connect to sync the emulator to the PC. This is not required, but will allow you to have your emulator connect to the web while it's cradled.
You can change the Output File folder where the application is stored: Project ->Properties->Devices->Output File Folder. It defaults to %CSIDL_PROGRAM_FILES%\SmartDeviceProject1
The sample doesn't show History: if you figure that out, let me know. Otherwise, I may just create a SQL Server CE database and store the links in there….
If you're using VS 2005, you'll get several VB compile errors that you can fix due to new features in VB.Net 2008
- Change due to Type Inference:
- Old: Dim NewWindow = New PhoneBrowser
- New: Dim NewWindow As New PhoneBrowser
- Change For loops to declare vars
- Old: For i = 1 To oBrowserForms.Count
- New: For i As Integer = 1 To oBrowserForms.Count
- Relaxed Delegates in 2008 means the method signature doesn't have to exactly match. To fix this, change the event handler method parameters to match.
- Old: ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs
- New: ByVal o As MyMenuItem, ByVal e As System.Windows.Forms.KeyEventArgs
- Now that the method signatures match, you'll have to cast some of the Objects to things like "MyMenuItem" or "MyListViewItem" :
- Old: Select Case o.Text
- New: Select Case CType(o, MyMenuItem).Text
- The sample uses Linq in a few places: just replace with the commented code below if Linq is not available (as in VS 2005)
The F1,F2 buttons on your keyboard map to the left and right soft keys of the phone.
To deploy on a real SmartPhone, connect via ActiveSync and choose Project->Properties->Devices->Target Device: Windows Mobile 5.0 Smartphone Device, then hit F5. You'll get some confirmation dialogs on your phone.
You can use Linq on your SmartPhone in VS 2008: if you get an error creating a simple query, Project->Properties: add a reference to c:\Program Files\Microsoft.Net\SDK\CompactFramework\V3.5\WindowsCE\System.Core.dll, and import System.Core and System.Xml
You may have to unlock your phone: https://blogs.conchango.com/stuartpreston/archive/2005/11/10/2376.aspx
See also:
Customize your Windows Mobile SmartPhone home screen and Start Menu
How to: Use the WebBrowser Control in the .NET Compact Framework
Smartphone Development and the .NET Compact Framework
Windows Mobile 5.0 Application Security
Windows Mobile-based Smartphone Applications Deployment Demystified
Device Security Manager Powertoy for Windows Mobile 5.0
Remove double spaces from pasted code samples in blog
Visual Studio->Tools menu->Device Security Manager
Lots of goodies under Start Menu->Visual Studio 2008->Remote Tools, like Remote Registry Editor, Remote Process Viewer, Remote File Viewer
Start of Code
Public Class PhoneBrowser
Inherits Form
#If Smartphone = True Then 'auto set: Project->Properties->Compile->Advanced Compile Options->Custom Constants
Dim fSmartPhone = True
#Else
Dim fSmartPhone = False
#End If
Friend WithEvents WebBrowserCtrl As System.Windows.Forms.WebBrowser
Shared oBrowserForms As New System.Collections.Generic.List(Of PhoneBrowser)
Private Sub PhoneBrowser_Load() Handles MyBase.Load
Me.WebBrowserCtrl = New Windows.Forms.WebBrowser
Me.WebBrowserCtrl.Dock = DockStyle.Fill
Me.WebBrowserCtrl.DocumentText = "<html>Custom Web Browser for Smartphone by Calvin Hsia</html>" ' put your name here
Me.Controls.Add(Me.WebBrowserCtrl)
Me.Text = "PhoneBrowser"
If fSmartPhone Then
Me.WindowState = FormWindowState.Maximized
Else
Me.Size = New Size(1280, 1024)
End If
oBrowserForms.Add(Me) ' add ourselves to the collection
If oBrowserForms.Count = 1 Then ' if it's the first instance
Me.WebBrowserCtrl.Navigate(New System.Uri("https://www.msn.com"))
'ShowFavorites()
End If
If Not fSmartPhone Then
ShowMenu()
End If
End Sub
Private Sub PhoneBrowser_FormClosing(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closing
For i = 1 To oBrowserForms.Count
If oBrowserForms.Item(i - 1) Is Me Then ' remove ourselves from the list
oBrowserForms.RemoveAt(i - 1)
Exit For
End If
Next
End Sub
Private Sub PhoneBrowser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.F1 OrElse _
e.KeyCode = System.Windows.Forms.Keys.F2 Then
ShowMenu()
Else
If e.KeyCode = Keys.Back Then
If WebBrowserCtrl.CanGoBack Then
WebBrowserCtrl.GoBack()
End If
End If
End If
End Sub
Sub ShowMenu()
Me.Menu = New MainMenu
Dim mItem As MyMenuItem
Dim mItemBar As MyMenuItem 'the 2 softkeys
Dim mItemNav As MyMenuItem
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
mItemBar = New MyMenuItem("Stop Request", Me.Menu.MenuItems, AddressOf Me.BrowserMenuItem_Click)
Else
mItemBar = New MyMenuItem("Favorites", Me.Menu.MenuItems, AddressOf Me.BrowserMenuItem_Click)
End If
mItemBar = New MyMenuItem("Menu", Me.Menu.MenuItems)
mItem = New MyMenuItem("Address Bar", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Add to Favorites", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItemNav = New MyMenuItem("Navigate", mItemBar.MenuItems)
mItem = New MyMenuItem("Favorites", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Go Forward", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem.Enabled = Me.WebBrowserCtrl.CanGoForward
mItem = New MyMenuItem("Go Back", mItemNav.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem.Enabled = Me.WebBrowserCtrl.CanGoBack
mItem = New MyMenuItem("Cancel Menu", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Refresh", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click) ' keep in pos 5
mItem = New MyMenuItem("New Window", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
mItem = New MyMenuItem("Switch Windows", mItemBar.MenuItems)
If oBrowserForms.Count = 1 Then
mItem.Enabled = False
Else
For i = 1 To oBrowserForms.Count
If Not oBrowserForms(i - 1) Is Me Then ' don't add ourself
Dim mText As String
Dim oUri = oBrowserForms(i - 1).WebBrowserCtrl.Url
If oUri Is Nothing Then
mText = "Form " + i.ToString
Else
If oUri.ToString.ToLower.StartsWith("https://") Then
mText = oUri.ToString.Substring(7)
Else
mText = oUri.ToString
End If
End If
Dim MenuItemBrowserInstance = New MyMenuItem(mText, mItem.MenuItems, AddressOf MenuItemNewFormHandler_Click)
MenuItemBrowserInstance.oBrowser = oBrowserForms(i - 1)
End If
Next
End If
mItem = New MyMenuItem("Exit", mItemBar.MenuItems, AddressOf Me.BrowserMenuItem_Click)
End Sub
Sub BrowserMenuItem_Click(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
If fSmartPhone Then
Me.Menu = Nothing ' make menu disappear
End If
Select Case o.Text
Case "Stop Request"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
ShowMenu() ' refresh menu
Case "Favorites"
ShowFavorites()
Case "Address Bar"
Dim oAddrBarForm = New FormAddressBar(Me)
If oAddrBarForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
Me.WebBrowserCtrl.Navigate(New System.Uri(oAddrBarForm.Result))
Catch ex As Exception
MsgBox(oAddrBarForm.Result, MsgBoxStyle.OkOnly, "Invalid URL")
End Try
End If
Case "Add to Favorites"
ShowAddFavoriteForm(Me, o.Text)
Case "Go Forward"
Me.WebBrowserCtrl.GoForward()
Case "Go Back"
Me.WebBrowserCtrl.GoBack()
Case "Refresh"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Me.WebBrowserCtrl.Refresh()
Case "Cancel Menu"
'do nothing
Case "New Window"
Dim NewWindow = New PhoneBrowser
Dim cUrl = ""
If Not Me.WebBrowserCtrl.Url Is Nothing Then
cUrl = Me.WebBrowserCtrl.Url.ToString
End If
NewWindow.Show()
NewWindow.WebBrowserCtrl.Navigate(New System.Uri(cUrl)) ' new instance goes to same page
Case "Exit"
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Me.Close()
Case Else
MsgBox("Menu item " + o.Text + " not handled")
End Select
End Sub
Sub ShowFavorites()
If Me.WebBrowserCtrl.ReadyState <> WebBrowserReadyState.Complete Then
Me.WebBrowserCtrl.Stop()
End If
Dim oFavForms As New FormFavorites(Me)
If oFavForms.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso oFavForms.Result.Length > 0 Then
Try
Me.WebBrowserCtrl.Navigate(New System.Uri(oFavForms.Result))
Catch ex As Exception
MsgBox(oFavForms.Result, MsgBoxStyle.OkOnly, "Error in URL")
End Try
End If
End Sub
Shared Sub ShowAddFavoriteForm(ByVal oPhoneBrowser As PhoneBrowser, _
ByVal cTitle As String, _
Optional ByVal cName As String = "", _
Optional ByVal cUrlSpecific As String = "", _
Optional ByVal cOrigFileName As String = "")
'SmartPhone webbrowser doesn't have Document property
Dim cHost = cName
Dim cUrl = cUrlSpecific
If Not oPhoneBrowser.WebBrowserCtrl.Url Is Nothing AndAlso cTitle <> "Edit" Then
cHost = oPhoneBrowser.WebBrowserCtrl.Url.Host
cUrl = oPhoneBrowser.WebBrowserCtrl.Url.ToString
End If
Dim oAddFav As New FormAddToFavorites(cHost, cUrl, cTitle, cOrigFileName)
If oAddFav.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
End Sub
Sub MenuItemNewFormHandler_Click(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
If fSmartPhone Then
Me.Menu = Nothing
End If
o.oBrowser.Show()
End Sub
Class MyMenuItem ' same as MenuItem, except a place to put a reference to the instance
Inherits MenuItem
Public oBrowser As PhoneBrowser
Sub New(ByVal cText As String, ByRef mi As MenuItemCollection, _
Optional ByVal handler As System.EventHandler = Nothing)
Me.Text = cText
mi.Add(Me)
If Not handler Is Nothing Then
AddHandler Me.Click, handler
End If
End Sub
End Class
Private Sub WebBrowserCtrl_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowserCtrl.DocumentCompleted
If Not Me.WebBrowserCtrl.Url Is Nothing Then
Me.Text = Me.WebBrowserCtrl.Url.ToString
End If
End Sub
Shared Function GetFavoritesFolder() As String 'called from outside the class, so "shared"
Try
GetFavoritesFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites)
Catch ex As Exception
GetFavoritesFolder = "\Windows\Favorites"
End Try
End Function
Class FormAddressBar
Inherits Windows.Forms.Form
Dim WithEvents txtAddr As New TextBox
Dim WithEvents lvAddr As New ListView
Public Result As String = ""
Dim IsListBoxChanging As Boolean = False
Dim oPhoneBrowser As PhoneBrowser
Sub New(ByVal oPhoneBrowser As PhoneBrowser)
Me.oPhoneBrowser = PhoneBrowser
Dim MenuMain As Menu
Dim MItem As MyMenuItem
MenuMain = New MainMenu
MItem = New MyMenuItem("Go", MenuMain.MenuItems, AddressOf AddrBarMenuItem_Click)
MItem = New MyMenuItem("Cancel", MenuMain.MenuItems, AddressOf AddrBarMenuItem_Click)
Me.Menu = MenuMain
If Not PhoneBrowser.WebBrowserCtrl.Url Is Nothing Then
txtAddr.Text = PhoneBrowser.WebBrowserCtrl.Url.ToString
End If
txtAddr.Left = 10
txtAddr.Width = 300
txtAddr.Visible = True
Me.Controls.Add(txtAddr)
txtAddr.SelectionStart = txtAddr.Text.Length ' put cursor at end
lvAddr.View = View.List
lvAddr.Visible = True
lvAddr.Top = 30
lvAddr.Width = 300
Me.Controls.Add(Me.lvAddr)
RefreshListbox()
End Sub
Sub AddrBarMenuItem_Click(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
Select Case o.Text
Case "Go"
GoAddrBar()
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Result = IIf(Me.txtAddr.Text.StartsWith("https://"), "", "https://") + Me.txtAddr.Text
Case "Cancel"
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Select
Me.Close()
End Sub
Sub RefreshListbox()
Me.lvAddr.Items.Clear()
Try
lvAddr.Items.Add(New MyListViewItem(Me.txtAddr.Text + ".com"))
lvAddr.Items.Add(New MyListViewItem(Me.txtAddr.Text + ".org"))
Catch ex As Exception
End Try
End Sub
Private Sub txtAddr_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAddr.TextChanged
If Not IsListBoxChanging Then
RefreshListbox()
End If
End Sub
Private Sub lvAddr_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAddr.Click
GoAddrBar()
End Sub
Private Sub lvAddr_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvAddr.KeyDown
Select Case e.KeyCode
Case Keys.Up
If lvAddr.Items(0).Selected Then
lvAddr.Items(0).Selected = False
Me.txtAddr.Focus()
End If
Case Keys.Down
If lvAddr.Items(lvAddr.Items.Count - 1).Selected Then
lvAddr.Items(lvAddr.Items.Count - 1).Selected = False
Me.txtAddr.Focus()
End If
Case Keys.Enter
GoAddrBar()
End Select
End Sub
Sub GoAddrBar()
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Result = IIf(Me.txtAddr.Text.StartsWith("https://"), "", "https://") + Me.txtAddr.Text
Me.Close()
End Sub
Private Sub lvAddr_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAddr.SelectedIndexChanged
If Me.lvAddr.SelectedIndices.Count = 1 Then
IsListBoxChanging = True
Me.txtAddr.Text = Me.lvAddr.Items(Me.lvAddr.SelectedIndices.Item(0)).Text
IsListBoxChanging = False
End If
End Sub
End Class
Public Class FormFavorites
Inherits Form
Dim WithEvents ListView As Windows.Forms.ListView
Public Result As String = ""
Dim oPhoneBrowser As PhoneBrowser
Sub New(ByVal oPhoneBrowser As PhoneBrowser)
Me.oPhoneBrowser = oPhoneBrowser
Me.ListView = New Windows.Forms.ListView
Me.ListView.Dock = DockStyle.Fill
Me.ListView.View = View.List
FillFavList(GetFavoritesFolder())
Me.Menu = New MainMenu
Dim mItem As MyMenuItem
Dim mItemBar As MyMenuItem 'the 2 softkeys
mItemBar = New MyMenuItem("Go", Me.Menu.MenuItems, AddressOf Me.FavMenuItem_Click)
mItemBar = New MyMenuItem("Menu", Me.Menu.MenuItems)
mItem = New MyMenuItem("Address Bar", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
mItem = New MyMenuItem("Add Favorite", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
mItem = New MyMenuItem("Add Folder", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
mItem = New MyMenuItem("Edit", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
mItem = New MyMenuItem("Delete", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
mItem = New MyMenuItem("Cancel", mItemBar.MenuItems, AddressOf Me.FavMenuItem_Click)
Me.ListView.Visible = 1
Me.Controls.Add(Me.ListView)
End Sub
Sub FillFavList(ByVal BaseFolder As String) 'todo: add icons
Me.ListView.Clear()
Dim FavoriteFiles = From FileName In IO.Directory.GetFiles(BaseFolder) Order By FileName
Dim FavoriteFolders = From FolderName In IO.Directory.GetDirectories(BaseFolder) Order By FolderName
'Dim FavoriteFolders = IO.Directory.GetDirectories(BaseFolder)
'Dim FavoriteFiles = IO.Directory.GetFiles(BaseFolder)
'System.Array.Sort(FavoriteFolders) 'ListView.Sort not available
'System.Array.Sort(FavoriteFiles) ' could use Linq
For Each File In FavoriteFiles
Dim FavTxt = File.Substring(BaseFolder.Length + 1).Replace(".url", "")
Me.ListView.Items.Add(New MyListViewItem(FavTxt, File))
Next
For Each FolderName In FavoriteFolders
Dim FavTxt = "Folder: " + FolderName.Substring(BaseFolder.Length + 1)
Me.ListView.Items.Add(New MyListViewItem(FavTxt, FolderName, True))
Next
If GetFavoritesFolder() <> BaseFolder Then
Me.ListView.Items.Add(New MyListViewItem("..[Parent]", System.IO.Path.GetDirectoryName(BaseFolder), True))
End If
If Me.ListView.Items.Count > 0 Then ' if there are any
Me.ListView.Items(0).Selected = True ' select 1st one
End If
End Sub
Sub FavMenuItem_Click(ByVal o As MenuItem, ByVal e As System.EventArgs)
Select Case o.Text
Case "Go"
GoFavorite()
Case "Address Bar"
Dim oAddrBarForm = New FormAddressBar(oPhoneBrowser)
If oAddrBarForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.Result = oAddrBarForm.Result
Me.DialogResult = Windows.Forms.DialogResult.OK
End If
Me.Close()
Case "Add Favorite"
ShowAddFavoriteForm(oPhoneBrowser, o.Text)
FillFavList(GetFavoritesFolder) ' refresh list
Case "Add Folder"
Dim oAddNewFolder = New FormAddNewFavFolder
If oAddNewFolder.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim cNewFolder = GetFavoritesFolder() + IO.Path.DirectorySeparatorChar + oAddNewFolder.txtName.Text
Try
IO.Directory.CreateDirectory(cNewFolder)
FillFavList(GetFavoritesFolder) ' refresh list
Catch ex As Exception
MsgBox(cNewFolder, MsgBoxStyle.OkOnly, "Error Creating Folder")
End Try
End If
Case "Edit"
Dim nIndex = Me.ListView.SelectedIndices.Item(0)
Dim lvItem = CType(Me.ListView.Items(nIndex), MyListViewItem)
If Not lvItem.fFolder Then
ShowAddFavoriteForm(Me.oPhoneBrowser, o.Text, lvItem.Text, GetUrlFromFavoriteFile(lvItem.Tag), lvItem.Tag)
End If
Case "Delete"
Dim nIndex = Me.ListView.SelectedIndices.Item(0)
Dim lvItem = CType(Me.ListView.Items(nIndex), MyListViewItem)
Try
If lvItem.fFolder Then
IO.Directory.Delete(lvItem.Tag)
Else
IO.File.Delete(lvItem.Tag)
End If
Catch ex As Exception
MsgBox(lvItem.Tag, MsgBoxStyle.OkOnly, "Error Deleting Folder")
End Try
FillFavList(GetFavoritesFolder) ' refresh list
Case "Cancel"
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Select
End Sub
Function GetUrlFromFavoriteFile(ByVal cFile As String)
'[InternetShortcut]
'URL=https://mobile.msn.com/pocketpc
Dim cUrl = ""
Dim fHandle = System.IO.File.OpenText(cFile)
Do While Not fHandle.EndOfStream
Dim cLine = fHandle.ReadLine
If cLine.StartsWith("URL=") Then
cUrl = cLine.Substring(4)
Me.Close()
Me.DialogResult = Windows.Forms.DialogResult.OK
Exit Do
End If
Loop
fHandle.Close()
Return cUrl
End Function
Sub GoFavorite()
If Me.ListView.SelectedIndices.Count = 1 Then
Dim nIndex = Me.ListView.SelectedIndices.Item(0)
Dim lvItem = CType(Me.ListView.Items(nIndex), MyListViewItem)
If lvItem.fFolder Then
FillFavList(lvItem.Tag)
Else
Result = GetUrlFromFavoriteFile(lvItem.Tag)
End If
End If
End Sub
Private Sub ListView_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView.Click
GoFavorite()
End Sub
Private Sub ListView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView.KeyDown
If e.KeyCode = Keys.Enter Then
GoFavorite()
End If
End Sub
Class FormAddNewFavFolder
Inherits Form
Dim lblName As New Label
Public txtName As New TextBox
Sub New()
AddCtrl(Me, lblName, 0, "Name:")
AddCtrl(Me, txtName, 30, "")
Me.Menu = New MainMenu
Me.Text = "Add Folder to Favorites"
Dim mItem = New MyMenuItem("Add", Me.Menu.MenuItems, AddressOf AddFolderMenuItemClick)
mItem = New MyMenuItem("Cancel", Me.Menu.MenuItems, AddressOf AddFolderMenuItemClick)
End Sub
Sub AddFolderMenuItemClick(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
Select Case o.Text
Case "Add"
If Me.txtName.Text.Length > 0 Then
Me.DialogResult = Windows.Forms.DialogResult.OK
End If
Case "Cancel"
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Select
Me.Close()
End Sub
End Class 'FormAddNewFavFolder
End Class 'FormFavorites
Shared Function AddCtrl(ByVal oForm As Form, ByVal oCtrl As Windows.Forms.Control, ByVal nTop As Integer, ByVal cText As String) As Control
oCtrl.Top = nTop
oCtrl.Left = 10
oCtrl.Width = 300
oCtrl.Text = cText
oCtrl.Visible = True
oForm.Controls.Add(oCtrl)
Return oCtrl
End Function
Class FormAddToFavorites
Inherits Form
Dim lblName As New Label
Dim txtName As New TextBox
Dim lblAddr As New Label
Dim txtAddr As New TextBox
Dim lblFolder As New Label
Dim cboFolder As New ComboBox
Dim cOrigFileName As String = ""
Sub New(ByVal FavTitle As String, ByVal cUrl As String, ByVal cTitle As String, _
ByVal cOrigFileName As String)
Me.cOrigFileName = cOrigFileName
AddCtrl(Me, lblName, 0, "Name:")
AddCtrl(Me, txtName, 30, FavTitle)
AddCtrl(Me, lblAddr, 60, "Address(URL):")
AddCtrl(Me, txtAddr, 90, cUrl)
AddCtrl(Me, lblFolder, 120, "Folder:")
cboFolder.Items.Add("Favorites")
Me.Text = cTitle
Dim FavoriteFolder = GetFavoritesFolder()
Dim FavoriteSubFolders = IO.Directory.GetDirectories(FavoriteFolder)
For Each FolderName In FavoriteSubFolders
cboFolder.Items.Add(FolderName.Substring(FavoriteFolder.Length + 1))
Next
AddCtrl(Me, cboFolder, 150, "Favorites")
txtAddr.Width = 300
txtAddr.SelectionStart = cUrl.Length
Me.Menu = New MainMenu
Dim mItem = New MyMenuItem("Done", Me.Menu.MenuItems, AddressOf AddFavMenuItemClick)
mItem = New MyMenuItem("Cancel", Me.Menu.MenuItems, AddressOf AddFavMenuItemClick)
End Sub
Sub AddFavMenuItemClick(ByVal o As MyMenuItem, ByVal e As System.EventArgs)
Select Case o.Text
Case "Done"
If cOrigFileName.Length > 0 Then
IO.File.Delete(cOrigFileName)
End If
Dim cPath = GetFavoritesFolder()
If cboFolder.Text <> "Favorites" Then ' only 1 level deep supported
cPath += IO.Path.DirectorySeparatorChar + cboFolder.Text
End If
cPath += IO.Path.DirectorySeparatorChar + Me.txtName.Text + ".url"
Dim fs = IO.File.CreateText(cPath)
fs.WriteLine("[InternetShortcut]")
fs.WriteLine("URL=" + Me.txtAddr.Text)
fs.Close()
Me.DialogResult = Windows.Forms.DialogResult.OK
Case "Cancel"
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Select
Me.Close()
End Sub
End Class
Class MyListViewItem
Inherits ListViewItem
Public fFolder As Boolean
Sub New(ByVal cItem As String, Optional ByVal oTag As Object = Nothing, Optional ByVal IsFolder As Boolean = False)
Me.Tag = oTag
Me.Text = cItem
fFolder = IsFolder
End Sub
End Class
End Class
End of Code
Comments
Anonymous
October 01, 2007
PingBack from http://www.artofbam.com/wordpress/?p=4348Anonymous
February 29, 2012
Good article ! But can you share attachment for a demo ?