Download VB.NET example code / PDF
Download VB.NET example code / PDF
Introduction
This
document provides a number of examples of VB.Net code that I designed and developed.
See
Notes and Caveats at the end of this document.
Example(s):
Description
This document contains 2 examples:
Example 1. This example is part of an
application (network application) for a smart device (Pocket PC). The
application uses the .Net Compact Framework (CF).
Example 2. This example is part of an
application (desktop application) to extract email addresses from Microsoft
Outlook 2007 folders. It uses automation to interact with Outlook.
Example(s):
Code
Example
1:
' Function: Event handler for 'Submit'
button. (Submit collection details)
Private Sub FR_ButSub_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles FR_ButSub.Click
Dim curColl As Collection
If Not FR_House.Text = "" Then
If Not FR_Street.Text =
"" Then
If Not FR_Weight.Text =
"" Then
curColl = New
Collection(FR_House.Text, FR_Street.Text, FR_PostCode.Text, FR_Weight.Text)
oCollections.AddEntry(curColl)
FX_NCollRec.Text =
oCollections.CountEntries
FR_MsgOut.Text =
"Collection details recorded."
oCollections.Send()
FX_NCollSent.Text =
oCollections.CountSent
ShowOutfile()
Else
FR_MsgOut.Text =
"Error: Weight not specified."
End If
Else
FR_MsgOut.Text = "Error:
Street/road not specified."
End If
Else
FR_MsgOut.Text = "Error:
House not specified."
End If
End Sub
' Function: ReadIn. Purposes: Reads in work schedule from main office
system.
' Description: The work schedule is provided in the form of an XML
file.
' This function (a) reads in the file, (b) searches for the relevent
' schedule (for this vehicle), and (c) stores the schedule in this
object.
Public Function ReadIn() As Boolean
Dim bFound As Boolean : bFound = False
Dim sVehNo, sDate, sStreetName, sPostcode As String
Dim sCurElement As String
sClassErrMsg = "No schedule for this vehicle."
' first, clear out the existing work schedule...
scheditems.Clear()
' Read and parse XML data...
Try
Dim xr As New
XmlTextReader(sSchedulesFile)
While xr.Read()
Select Case xr.NodeType
Case XmlNodeType.Element
If xr.Name =
"schedule" Then
sVehNo =
""
sStreetName =
""
sPostcode =
""
End If
If xr.Name = "veh_no"
Then
sCurElement =
xr.Name
End If
If xr.Name =
"date" Then
sCurElement =
xr.Name
End If
If xr.Name =
"street" Then
End If
If xr.Name =
"name" Then
sCurElement =
xr.Name
End If
If xr.Name =
"postcode" Then
sCurElement =
xr.Name
End If
Case XmlNodeType.Text
If sCurElement =
"veh_no" Then
sVehNo = xr.Value
End If
If sCurElement =
"date" Then
sDate = xr.Value
End If
If sCurElement =
"name" Then
sStreetName =
xr.Value
End If
If sCurElement =
"postcode" Then
sPostcode =
xr.Value
End If
Case XmlNodeType.CDATA
Case
XmlNodeType.ProcessingInstruction
Case
XmlNodeType.Comment
Case XmlNodeType.Document
Case
XmlNodeType.Whitespace
Case
XmlNodeType.SignificantWhitespace
Case
XmlNodeType.EndElement
If xr.Name = "street" Then
If sVehNo =
sVehicleNo And Not sStreetName = "" And Not sPostcode =
"" Then
' Add
schedule item (street) to WorkSchedule object
curSchedItem
= New SchedItem(sStreetName, sPostcode)
AddEntry(curSchedItem)
' Update of
list view not done here
bFound = True
End If
End If
End Select
End While
xr.Close()
Catch e As System.IO.FileNotFoundException
sClassErrMsg = "Work
schedule file not found."
Catch e As XmlException
sClassErrMsg = "XmlException
occured"
End Try
Return bFound
End Function
|
Example
2:
'****************************************
' Program: WEED - (Windows) Extract Email
Details
' Program type: Windows application
(.exe)
' Version: 0.8
' Date: 21 Feb 2009
'----------------------------------------
' Author: Andrew Fry
' Copyright: Copyright (c) 2009 A.R.Fry.
All rights reserved.
'****************************************
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Public Addrs1 As New ArrayList()
'Array of addresses
Public aIgnores() As String
' Array of ignore strings
Public Ignores As String
Public SkipJunk As Boolean
Public CheckSubFolders As Boolean
'****************************************
' Function: ButGo_Click
' Purpose: Handles Go button, on click
'****************************************
Private Sub ButGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButGo.Click
' Initialisation...
Addrs1.Clear()
SkipJunk = True
CheckSubFolders = True
If Not Me.CBIncSF.Checked Then
CheckSubFolders = False
End If
' Get addresses...
GetEmailAddresses()
MsgBox("Done!")
Application.Exit()
End Sub
'****************************************
' Function: GetEmailAddresses
'****************************************
Private Sub GetEmailAddresses()
Dim oOLApp As Microsoft.Office.Interop.Outlook.Application
Dim oOLNS As Microsoft.Office.Interop.Outlook.NameSpace
Dim oOLFolders As Microsoft.Office.Interop.Outlook.Folders
Dim oOLFolder As Microsoft.Office.Interop.Outlook.MAPIFolder ' Top
level folder
Dim InFileIgn As String : InFileIgn = Me.TBInF.Text
Dim OutFile As String : OutFile = Me.TBOutF.Text
Dim DoSort As Boolean : DoSort = True
Dim fIn As IO.StreamReader
Dim fOut As IO.StreamWriter
Dim N As Integer
oOLApp = CType(CreateObject("Outlook.Application"),
Microsoft.Office.Interop.Outlook.Application)
' Setup Namespace
oOLNS = oOLApp.GetNamespace("MAPI")
'oOLNS = ThisOutlookSession.Session
If InFileIgn <> "" Then
If
System.IO.File.Exists(InFileIgn) Then
If FileLen(InFileIgn) > 0
Then
fIn =
File.OpenText(InFileIgn)
Ignores = fIn.ReadToEnd
fIn.Close()
aIgnores = Split(Ignores,
vbCrLf)
End If
End If
End If
' All folders or specific folder ?
If Me.RBFoldA.Checked Then
oOLFolders = oOLNS.Folders
For Each oOLFolder In oOLFolders
'FolderName = oOLFolder.Name
'Addrs1.Add("Folder:
" + FolderName)
'Addrs1.Add("DIT: "
+ CStr(oOLFolderL0.DefaultItemType))
ProcessFolder(oOLFolder)
Next
Else
oOLFolder = oOLNS.PickFolder
ProcessFolder(oOLFolder)
End If
oOLApp = Nothing
If Me.CBSort.Checked Then
Me.TBStat.Text = "Sorting
results..."
Application.DoEvents()
Addrs1.Sort()
End If
' Write results to output file...
fOut = File.CreateText(OutFile)
For N = 0 To Addrs1.Count - 1
fOut.WriteLine(CStr(Addrs1(N)))
Next
fOut.Close()
Addrs1.Clear()
End Sub
'****************************************
' Function: ProcessFolder
' Purpose: ...
' Returns: Array of addresses
'****************************************
Private Sub ProcessFolder(ByVal StartFolder As
Microsoft.Office.Interop.Outlook.MAPIFolder)
Dim oFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim oItem As Object
Dim FolderName As String
Dim FolderPath As String
Dim bProceed As Boolean : bProceed = True
On Error Resume Next
FolderName = StartFolder.Name
FolderPath = StartFolder.FolderPath
Me.TBStat.Text = FolderPath
Application.DoEvents()
If SkipJunk = True And (FolderName = "Junk E-mail" Or
FolderName = "Spam") Then
bProceed = False
End If
If bProceed Then
' process all the items in this
folder...
If StartFolder.Items.Count > 0
Then
For Each oItem In
StartFolder.Items
'ItemName =
oItem.FullName
'Addrs1.Add("Item:
" + ItemName)
' Is it a mail item ?
If TypeOf oItem Is
Microsoft.Office.Interop.Outlook.MailItem Then
GetAddrs1(CType(oItem, Microsoft.Office.Interop.Outlook.MailItem))
Me.TBStat.Text = FolderPath + "
(" + CStr(Addrs1.Count) + ")"
Application.DoEvents()
End If
Next
End If
' process all the subfolders in
this folder...
If CheckSubFolders Then
If StartFolder.Folders.Count
> 0 Then
For Each oFolder In
StartFolder.Folders
'FolderName =
objFolder.Name
'Addrs1.Add("Folder: " + oFolder.FolderPath)
'Addrs1.Add("Folder: " + FolderName)
'Addrs1.Add("DIT: " + CStr(objFolder.DefaultItemType))
ProcessFolder(oFolder)
Next
End If
End If
End If
End Sub
(NB. Remainder of code not shown)
|
Explanation
Example 1.
The function FR_ButSub_Click is an event
handler. It is tied to the ‘Submit’ button on a form on the portable device,
and is executed when the dustcart team has completed a collection, entered
details of it on the device and then pressed ‘Submit’ to record the details and
send them to HQ.
It performs the following steps:.....
Download VB.NET example code / PDF
0 commentaires: