Get Source Code from Webview (VB for Metro) - vb.net

I'm Making a Windows Phone's app that I can, From a webview called "DebWeb", get the ClassRoom of a specific class. The DebWeb load the site where is all the classRooms, but I want to make that my App search just my class.
Before I made an app with almost the same objetive (search the Name of a App from the Source Code), but it was made from VB for PC, now I'm working on VB for Metro (or for App Store) and I can't use the same code.
For example, On VB for PC I can use:
Dim EHTML = DebWeb.Document.All.Item(1)
Dim sourceString As String = EHTML.InnerHtml
'Use Regex Match to search from SourceString"
But on VB for Metro it's shows me the " 'Document' is not a member of 'Windows.UI.XAML.Controls.WebView' " error, so I can't get the Source Code from the page and I can't look for the ClassRoom.
I Looked on the MSDN page about Webview but the most close thing that I can do is to get the "DocumentTittle", but not the content.
This is my code, everything "works" except the "Source" variable:
Dim Source = DebWeb.[Control] 'Here is where I need the Control to get the SourceCode
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Source.ToString, _
"DERECHO CONSTITUCIONAL", _
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Edited with my Entire code:
Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim URL As String = "http://goo.gl/uqohKw"
Me.DebWeb.Navigate(New Uri(URL))
End Sub
Private Sub DebWeb_LoadCompleted(ByVal sender As Object, ByVal e As WebViewNavigationCompletedEventArgs)
LListo.Text = "Listo!"
Dim html As String = DebWeb.InvokeScriptAsync("eval", New String() {"document.documentElement.outerHTML;"}).ToString
Dim Source = html
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Source.ToString, _
"LECTURA CRÍTICA", _
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
If (m.Success) Then
Dim key As String = m.Groups(1).Value
End If
End Sub

Something like this?
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Try
Dim html As String = Await myWebView.InvokeScriptAsync("eval", New String() {"document.documentElement.outerHTML;"})
Catch ex As Exception
End Try
End Sub
More Info here

Related

I am trying to use clipboard object in class project of vb.net but I can't seem to be able to use it

I am trying to get clipboard text in vb.net class project but the clipboard object is not working
Dim clipText As String = String.Empty
If Clipboard.ContainsText Then
clipText = Clipboard.GetText()
End If
I am using this code but its giving me error
Dim clipboardText As String = System.Windows.Forms.Clipboard.GetText()
I am trying to use this but system. Windows doesn't have .forms but it has .input and .others what am I missing I tried referencing it from the references but it doesn't seem to work either
This is from a little project where i load code from a SQLite DB into a RichTextBox then click a button to copy that code to the clip board.
Private Sub btnCopySelCode_Click(sender As Object, e As EventArgs) Handles btnCopySelCode.Click
CopySelected()
End Sub
Public Sub CopySelected()
Dim start = rtbViewCode.SelectionStart
Dim substring = rtbViewCode.Text.Substring(0, start)
Dim words = substring.Split(New String() {" ", vbCrLf}, StringSplitOptions.None)
Dim gvW = rtbViewCode.SelectedText
Dim count = gvW.Length
If count <= 0 Then
gvalertType = "14"
frmAlert.ShowDialog()
rtbViewCode.Focus()
Return
End If
Clipboard.SetText(gvW)
ansQ()
End Sub

Print Custom Label with new Dymo Connect SDK with VB.NET Windows Forms

The following code will print a Label with default text & QR Code image from saved image path on computer, but I need to add replace the text & QR image with a custom text & QR Image. I cannot find any examples online of the new SDK, there is a little reference on the GitHub but cant piece anything together to work.
I Installed NuGet package Dynmo.Connect.SDK
Imports DymoSDK.Implementations
Dim dymoSDKLabel As DymoSDK.Implementations.DymoLabel
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim SelectedPrinter As String = "DYMO LabelWriter 450"
Dim copies As Integer = 1
Dim barcodeGraphsQuality As Boolean = True
dymoSDKLabel = New DymoLabel()
dymoSDKLabel.LoadLabelFromFilePath("C:\Users\User\Documents\QR.dymo")
DymoPrinter.Instance.PrintLabel(dymoSDKLabel, SelectedPrinter, copies, barcodeGraphsQuality)
End Sub
I wanted to say that I figured out the answer to my own problem in case someone is wondering how. I created a label in Dymo Connect software program called label.dymo. I added a TextObject and a ImageObject to the label, positioned in the middle and saved the label. I installed a Free NuGet package called QRCoder and referenced it in this sample code below. The String Variable called id is coming from my database and is the row id for what was just created(last insert row id). The QrCode generates a QRCode matching my id variable and then converts it to base64string because that what format the Dymo ImageObject supports. You can replace the id variable with any number or text as you need for your project.
Imports DymoSDK.Implementations
Imports QRCoder
Dim dymoSDKLabel As DymoSDK.Implementations.DymoLabel
Dim LabelTextObject As DymoSDK.Interfaces.ILabelObject
Dim LabelQRObject As DymoSDK.Interfaces.ILabelObject
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim gen As New QRCodeGenerator
Dim data = gen.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q)
Dim qrCode As New Base64QRCode(data)
Dim base64qrcode As String = qrCode.GetGraphic(20)
Dim SelectedPrinter As String = "DYMO LabelWriter 450"
Dim copies As Integer = 1
Dim barcodeGraphsQuality As Boolean = True
dymoSDKLabel = New DymoLabel()
dymoSDKLabel.LoadLabelFromFilePath("C:\Users\User\Documents\label.dymo")
LabelTextObject = dymoSDKLabel.GetLabelObject("TextObject")
dymoSDKLabel.UpdateLabelObject(LabelTextObject, id)
LabelQRObject = dymoSDKLabel.GetLabelObject("ImageObject")
dymoSDKLabel.UpdateLabelObject(LabelQRObject, base64qrcode)
DymoPrinter.Instance.PrintLabel(dymoSDKLabel, SelectedPrinter, copies, barcodeGraphsQuality)
End Sub

VB.NET Program is always reading last created textfile

Trying to create a login form,
My coding is currently:
Imports System
Imports System.IO
Public Class frmLogin
Dim username As String
Dim password As String
Dim fileReader As String
Dim folderpath As String
Dim files As Integer
Dim filepath As String
Public Structure info
Dim U As String
Dim P As String
End Structure
Dim details As info
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
If txtusername.Text = details.U And txtpassword.Text = details.P Then
MessageBox.Show("Correct!")
frmmenu.Show()
Me.Hide()
Else
MessageBox.Show("wrong")
txtusername.Clear()
txtpassword.Clear()
End If
End Sub
Private Sub btncreate_Click(sender As Object, e As EventArgs) Handles btncreate.Click
frmcreate.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
files = files + 1
filepath = "C:\Users\TheGlove\Desktop\Alex's Program\loginfile" & files & ".txt"
Dim di As DirectoryInfo = New DirectoryInfo("C:\Users\TheGlove\Desktop\Alex's Program")
folderpath = "C:\Users\TheGlove\Desktop\Alex's Program"
files = System.IO.Directory.GetFiles(folderpath, "*.txt").Count
For Each fi In di.GetFiles()
MsgBox(fi.Name)
Dim FILE = System.IO.File.ReadAllLines("C:\Users\TheGlove\Desktop\Alex's Program\loginfile" & files & ".txt")
Dim myArray As String() = FILE
details.U = myArray(0)
details.P = myArray(1)
Next
End Sub
End Class
Button 1 will be merged with btnlogin when i get it working and for now is currently just a seperate button to read each textfile.
When each button is pressed (Button 1 -> btnlogin), only the last created textfile is correct.
By the looks of things, your code does read all the text files, but keeps overwriting details.u and details.p with the value retrieved from each file. So, when the loop gets to the last file, those values are what ends up in the details object.
I'm assuming that you want to read all the usernames and passwords into a list and check the details in the TextBoxes against that list, so .. Your code should probably be something like the code below (see the code comments for an explanation of some of the differences.
Before we get to the code, can give you a couple of pointers.
Firstly, always try to use names that are meaningful. Defining your structure as Info is not as meaningful as it could be. For example, you would be better calling it UserInfo and rather than use P and U, you would be better using Password and UserName. It may not matter so much right now, but when you start writing larger more complex programs, and have to come back to them in 6 months time to update them, info.P or details.P aren't as informative as the suggested names.
Secondly, as #ajd mentioned. Don't use magic strings. Create one definition at the beginning of your code which can be used throughout. Again it makes maintenance much easier if you only have to change a string once instead of multiple times, and reduces the chance of mistakes.
Finally, several of the variables you have defined aren't used in your code at all. Again, at this level, it isn't a major issue, but with large programs, you could end up with a bigger memory footprint than you want.
Dim username As String
Dim password As String
Dim fileReader As String
Dim folderpath As String = "C:\Users\TheGlove\Desktop\Alex's Program"
Dim files As Integer
Dim filepath As String
Public Structure UserInfo
Dim Name As String
Dim Password As String
End Structure
'Change details to a list of info instead of a single instance
Dim userList As New List(Of UserInfo)
Private Sub Btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
'Iterate through the list of details, checking each instance against the textboxes
For Each tempUserInfo As UserInfo In userList
If txtusername.Text = tempUserInfo.Name And txtpassword.Text = tempUserInfo.Password Then
MessageBox.Show("Correct!")
frmmenu.Show()
Me.Hide()
'This is here, because after your form has opened an closed, the loop
'that checks usernames and passwords will continue. The line below exits the loop safely
Exit For
Else
MessageBox.Show("wrong")
txtusername.Clear()
txtpassword.Clear()
End If
Next
End Sub
Private Sub Btncreate_Click(sender As Object, e As EventArgs) Handles btncreate.Click
frmcreate.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'clear the list of user details otherwise, if the files are loaded a second time,
'you'll get the same details added again
userList.Clear()
'This line replaces several lines in your code that searches the folder for files
'marching the search pattern
Dim fileList() As FileInfo = New DirectoryInfo(folderpath).GetFiles("loginfile*.txt")
For Each fi As FileInfo In fileList
MsgBox(fi.Name)
Dim userDetails() As String = System.IO.File.ReadAllLines(fi.FullName)
Dim tempInfo As New UserInfo With {.Name = userDetails(0), .Password = userDetails(1)}
'An expanded version of the above line is
'Dim tempInfo As New info
'tempInfo.U = userDetails(0)
'tempInfo.P = userDetails(1)
userList.Add(tempInfo)
Next
files = fileList.Count
End Sub

Back button in a vb.net program

I am trying to write the code for a back button in my VB.NET app. But it is not working. What is wrong with it?
Private Sub BackBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackBtn.Click
Dim returntext As String = Clipboard.GetText()
lblDictword.Text = returntext
Dim returnHtmlText As String = Nothing
If (Clipboard.ContainsText(TextDataFormat.UnicodeText)) Then
returnHtmlText = Clipboard.GetText(TextDataFormat.UnicodeText)
'Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
Clipboard.SetText(returnHtmlText, TextDataFormat.UnicodeText)
End If
lblDictword.Text = returnHtmlText
'Return returnHtmlText
Dim count As Integer = myStrings.Length
If count > 1 Then
Dim s As String = myStrings.ElementAt(count - xd)
xd = Array.IndexOf(myStrings, s)
lblDictword.Text = s
End If
End Sub
I am not an VB.NET expert, but usually getting access to clipboard isn't as easy as it looks. Check this:
Problem with clipboard class
It can be only a tip, not really solution. Look into your clipboard functionallity

WP7 - Updating a listbox

I've finally got around to starting windows phone dev. I'm not very good at it yet, but anyway, I hope you guys understand what I want to do here.
From what I've learnt from other programmers, an ObservableCollection can be updated in live time whilst it is databound to an object, such as a listbox. All changes to the ObservableCollection will cause the UI of the object it's databound to update it's items.
So what I'm trying to do, is download a file from my server, parse it with json, then update the ObservableCollection with the new data. However, the webclient doesn't seem to be downloading the new data until the app is re opened!
Here's a gif showing how the app works at the moment:
And here's my code (cut down a bit):
Dim aList As New ObservableCollection(Of classes.consoles)
Private Sub PhoneApplicationPage_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
checkforconsoles()
End Sub
Public Sub checkforconsoles()
Dim wc As New WebClient()
AddHandler wc.DownloadStringCompleted, AddressOf downloaded
wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&userid=" & id))
End Sub
Private Sub downloaded(sender As Object, e As DownloadStringCompletedEventArgs)
aList.Clear()
'MessageBox.Show(e.Result)
Dim o As JObject = JObject.Parse(e.Result)
Dim jarray As JArray = DirectCast(o("results"), JArray)
Try
Dim i As Integer = jarray.Count()
For i = 0 To jarray.Count() - 1 Step 1
Dim id As String = jarray(i)("id").ToString
Dim name As String = jarray(i)("name").ToString
Dim image As String = jarray(i)("image").ToString
MessageBox.Show(name)
Dim c As classes.consoles = New classes.consoles()
c.categoryimage = New Uri(image)
c.categoryname = name
c.categoryid = id
aList.Add(c)
Next
listBoxview.ItemsSource = aList
StackPanel1.Visibility = Windows.Visibility.Collapsed
StackPanel2.Visibility = Windows.Visibility.Visible
Catch ex As Exception
StackPanel2.Visibility = Windows.Visibility.Collapsed
StackPanel1.Visibility = Windows.Visibility.Visible
End Try
End Sub
Private Sub ApplicationBarIconButton_Click_1(sender As System.Object, e As System.EventArgs)
checkforconsoles()
End Sub
Does anybody have any clue what's wrong? :(
Thanks in advance.
It's a cachine issue with the WebClient. You can append a random query string to ensure that the URL is always unique so that the WebClient doesn't cache the results. One way to do this is to add a random GUID value since it's very unlikely to generate two of the same GUIDs in a short time frame.
wc.DownloadStringAsync(New Uri("http://localhost/api/?function=GetConsolesForUser&
userid=" & id & "&random=" + Guid.NewGuid().ToString()))