-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClassAsyncHelpers.vb
188 lines (151 loc) · 6.54 KB
/
ClassAsyncHelpers.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Public Class ClassAsyncHelpers
Public Shared Function GetCheckedListBox(ByRef CB As CheckedListBox) As List(Of String)
Dim str As New List(Of String)
For Each item In CB.CheckedItems
str.Add(item)
Next
Return str
End Function
Public Shared Async Function GetCheckedListBoxAsync(ByVal CB As CheckedListBox) As Task(Of List(Of String))
Dim str As List(Of String) = Await Task.Run(Function() GetCheckedListBox(CB))
Return str
End Function
Public Shared Function GetListBox(ByRef CB As ListBox) As List(Of String)
Dim str As New List(Of String)
For Each item In CB.Items
str.Add(item)
Next
Return str
End Function
Public Shared Async Function GetListBoxAsync(ByVal CB As ListBox) As Task(Of List(Of String))
Dim str As List(Of String) = Await Task.Run(Function() GetListBox(CB))
Return str
End Function
Public Shared Sub LoadCheckedListBox(ByRef CB As CheckedListBox, ByRef LstStr As List(Of String))
For Each item In LstStr
CB.Items.Add(item)
Next
End Sub
Public Shared Async Sub LoadCheckedListBoxAsync(ByVal CB As CheckedListBox, ByVal LstStr As List(Of String))
Await Task.Run(Sub() LoadCheckedListBox(CB, LstStr))
End Sub
Public Shared Sub LoadListBox(ByRef CB As ListBox, ByRef LstStr As List(Of String))
For Each item In LstStr
CB.Items.Add(item)
Next
End Sub
Public Shared Async Sub LoadListBoxAsync(ByVal CB As ListBox, ByVal LstStr As List(Of String))
Await Task.Run(Sub() LoadListBox(CB, LstStr))
End Sub
Public Function ExtractLinks(ByVal url As String) As DataTable
Dim dt As New DataTable
dt.Columns.Add("LinkText")
dt.Columns.Add("LinkUrl")
Dim wc As New WebClient
Dim html As String = wc.DownloadString(url)
Dim links As MatchCollection = Regex.Matches(html, "<a.*?href=""(.*?)"".*?>(.*?)</a>")
For Each match As Match In links
Dim dr As DataRow = dt.NewRow
Dim matchUrl As String = match.Groups(1).Value
'Ignore all anchor links
If matchUrl.StartsWith("#") Then
Continue For
End If
'Ignore all java script calls
If matchUrl.ToLower.StartsWith("javascript:") Then
Continue For
End If
'Ignore all email links
If matchUrl.ToLower.StartsWith("mailto:") Then
Continue For
End If
'For internal links, build the url mapped to the base address
If Not matchUrl.StartsWith("http://") And Not matchUrl.StartsWith("https://") Then
matchUrl = MapUrl(url, matchUrl)
End If
'Add the link data to datatable
dr("LinkUrl") = matchUrl
dr("LinkText") = match.Groups(2).Value
dt.Rows.Add(dr)
Next
Return dt
End Function
Public Function MapUrl(ByVal baseAddress As String, ByVal relativePath As String) As String
Dim u As New System.Uri(baseAddress)
If relativePath = "./" Then
relativePath = "/"
End If
If relativePath.StartsWith("/") Then
Return u.Scheme + Uri.SchemeDelimiter + u.Authority + relativePath
Else
Dim pathAndQuery As String = u.AbsolutePath
' If the baseAddress contains a file name, like ..../Something.aspx
' Trim off the file name
pathAndQuery = pathAndQuery.Split("?")(0).TrimEnd("/")
If pathAndQuery.Split("/")(pathAndQuery.Split("/").Count - 1).Contains(".") Then
pathAndQuery = pathAndQuery.Substring(0, pathAndQuery.LastIndexOf("/"))
End If
baseAddress = u.Scheme + Uri.SchemeDelimiter + u.Authority + pathAndQuery
'If the relativePath contains ../ then
' adjust the baseAddress accordingly
While relativePath.StartsWith("../")
relativePath = relativePath.Substring(3)
If baseAddress.LastIndexOf("/") > baseAddress.IndexOf("//" + 2) Then
baseAddress = baseAddress.Substring(0, baseAddress.LastIndexOf("/")).TrimEnd("/")
End If
End While
Return baseAddress + "/" + relativePath
End If
End Function
Private Sub browserImageLoad_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
Dim browser As WebBrowser = CType(sender, WebBrowser)
Dim collection As HtmlElementCollection
Dim imgListString As List(Of HtmlElement) = New List(Of HtmlElement)()
If browser IsNot Nothing Then
If browser.Document IsNot Nothing Then
collection = browser.Document.GetElementsByTagName("img")
If collection IsNot Nothing Then
For Each element As HtmlElement In collection
Dim wClient As WebClient = New WebClient()
Dim urlDownload As String = element.GetAttribute("src")
wClient.DownloadFile(urlDownload, urlDownload.Substring(urlDownload.LastIndexOf("/"c)))
Next
End If
End If
End If
End Sub
Private Sub ExportDatasetToCsv(ByVal MyDataSet As DataSet, ByRef filname As String)
Dim myWriter As New System.IO.StreamWriter(filname)
Try
'Declaration of Variables
Dim dt As DataTable
' Dim dr As DataRow
Dim myString As String = ""
Dim bFirstRecord As Boolean = True
myString = ""
For Each dt In MyDataSet.Tables
For Each dr As DataRow In dt.Rows
bFirstRecord = True
For Each field As Object In dr.ItemArray
If Not bFirstRecord Then
myString &= ","
End If
myString &= field.ToString
bFirstRecord = False
Next
'New Line to differentiate next row
myString &= Environment.NewLine
Next
Next
'Write the String to the Csv File
myWriter.WriteLine(myString)
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Clean up
myWriter.Close()
End Sub
End Class