vb.net read string after certain character - vb.net

I have an listbox which has some random URLs + basic info, eg:
[search website] - http://www.google.com/
[games website] - http://www.miniclip.com/
Each line is an item.
When I try to use this code, it crashes:
Private Sub doubleclickitem(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim url As String = ListBox1.SelectedItem
Process.Start(url)
End Sub
The error is the first characters are unknown for Process.Start.
How can I start the url? Someone told me I have to read the lines after the first " - ". Is this right? If so, how can I do so?

This should do it:
url = url.Substring(url.LastIndexOf(" - ") + 3)

you should be assigning a value to each text item in the list, then retrieving the value, and feeding that to process.start, for example if you want process.start to open google, then it would be like so....(provided you assign a value of http://www.google.com to whatever the selected item is)
Process.Start(ListBox1.SelectedValue)
this is for c#, but the same ideals will still apply http://forums.asp.net/t/1199141.aspx/1

Try this:
YourTextBox.Text = YourString.Substring(0, YourString.Text.LastIndexOf(" - "))

Related

Reading Numbers from Webbrowser Vb.net

In my WebBrowser I got text like this: Remaining balance: 10$
I would like to convert it to another currency, I want just to read number from my browser, then after that I will send it to a Label or TextBox with the new converted currency. I am stuck here.
A screenshot of it
Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
WebBrowser2.Navigate(TextBox9.Text + TextBox2.Text)
End Sub
Private Sub WebBrowser2_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
Label1.Text = (WebBrowser2.Document.Body.InnerText)
End Sub
I have this suggestion (I know this is probably not the perfect solution):
First, try loading that same webpage in a normal web browser such as Google Chrome or Firefox or any browser which has the feature to "inspect elements", meaning to view their HTML code.
Find out the element which displays the price you want.
Note down the ID of the element (usually written something like id="someID")
Back to your program's code, include the following Function, which will get the text displayed and convert it to another currency:
Public Function ConvertDisplayedCurrency() As Decimal
Dim MyCurrencyElement As HtmlElement = WebBrowser2.Document.GetElementById("theIdYouGotInStep3") 'This will refer to the element you want.
Dim TheTextDisplayed As String = MyCurrencyElement.InnerText 'This will refer to the text that is displayed.
'Assuming that the text begins like "Remaining balance: ###", you need to strip off that first part, which is "Remaining balance: ".
Dim TheNumberDisplayed As String = TheTextDisplayed.Substring(19)
'The final variable TheNumberDisplayed will be resulting into a String like only the number.
Dim ParsedNumber As Decimal = 0 'A variable which will be used below.
Dim ParseSucceeded As Boolean = Decimal.TryParse(TheNumberDisplayed, ParsedNumber)
'The statement above will TRY converting the String TheNumberDisplayed to a Decimal.
'If it succeeds, the number will be set to the variable ParsedNumber and the variable
'ParseSucceeded will be True. If the conversion fails, the ParseSucceeded will be set
'to False.
If Not ParseSucceeded = True Then Return 0 : Exit Function 'This will return 0 and quit the Function if the parse was a failure.
'Now here comes your turn. Write your own statements to convert the number "ParsedNumber"
'to your new currency and finally write "Return MyFinalVariableName" in the end.
End Function
Call that Function probably when the document of the WebBrowser2 loads, that is, in the WebBrowser2_DocumentCompleted sub.
I hope it helps!

How can I count files in multiple listbox items

The purpose...
How can I read how many files in multiple folders.
So within the program I'm supposed to add and remove folders that I wanna monitor. So I'm adding folders to a listbox. The listbox will eventually contain a few items that are paths like \\server\parent directory\directory
The issue
Now this all works, adding the specified paths as items to the listbox, but now I want to count files in all the folders that are in the listbox and output a number to a textbox.
I've figured out how to do this if I have a textbox that contains a single path;
Dim counter = My.Computer.FileSystem.GetFiles(tbchannel1.Text)
tbCount1.Text = ("" & CStr(counter.Count))
But I can't figure out how to twist this around to work with all the items in a listbox instead.
...And btw, this is going to happen at the press of a button. Eventuelly I'll hook up a timer that button.performclick
Thanks!
This should do it for you
Private Sub CountFilesButton_Click(sender As Object, e As EventArgs) Handles CountFilesButton.Click
Try
Dim fileTotal As Integer
For Each item As String In DirListBox.Items
fileTotal += My.Computer.FileSystem.GetFiles(item.ToString).Count
Next
FileCountLabel.Text = String.Format("File count: {0}", fileTotal.ToString)
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred ", ex.Message))
End Try
End Sub
It'll be up to you to validate the path exists and handle other errors.

How to show a partial message from a result?

I have a textbox, button and an alertbox. Whenever someone types something into the textbox and clicks the button, it requests from a web page url and changes the alertbox text to a message of up to 43 letters/numbers (mixed). How do I change it so that it only shows the first 25 letters/numbers (mixed)?
It's currently showing something like this:
SUCCESS: 127.0.0.1 works - webpage.com
I want it to only show:
SUCCESS: 127.0.0.1 works
Either that or just to grab the numbers with dots and show them as a result. For example: 127.0.0.1. Note, the requests change every time, so the IP isn't the same.
Code:
Private Sub FlatButton2_Click(sender As System.Object, e As System.EventArgs) Handles FlatButton2.Click
Dim Website2 As New WebClient
Dim WebsiteIW As String = Website2.DownloadString("http://website.com/" + FlatTextBox1.Text)
FlatAlertBox1.Text = WebsiteIW
Threading.Thread.Sleep(1000)
FlatLabel2.Visible = True
FlatAlertBox1.Visible = True
FlatLabel3.Visible = True
You can use the MaxLength property of the textbox to limit the length of the input string. Otherwise, you can use the Substring string method to cut the string size before you communicate with the web page.

Why is this index out of range? And how do I solve it?

I have the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not TextBox1.Text = "" Then
If TextBox1.Text.Contains("ping") Then
PingSplit = TextBox1.Text.Split(" ")
End If
Select Case True
Case TextBox1.Text.Contains("ping")
' I get an IndexOutOfRange Exception was unhandled on below line
If PingSplit(0) Is Nothing Or PingSplit(1) Is Nothing Then
MsgBox("Invalid Ping IP!")
Else
ListBox1.Items.Add(GetPingMs(PingSplit(1)))
PingSplit(0) = vbNullString
PingSplit(1) = vbNullString
End If
End Sub
However, I cannot get the code to work when I simply type in "ping", "ping www.google.com[1] " works perfectly, however it will not work with just ping. The idea is that I type in ping "x", the code splits the ping and gets the address (x), and then uses the address in GetPingMs function, (getpingMsg(pingsplit(1)), however I get an IndexOutOfRange Exception.
You're getting an exception because you're trying to de-reference an array element that doesn't exist. If all you type is "ping" then your PingSplit array is only going to have one element, at index 0. But you're trying to reference a second element:
PingSplit(1)
You need to check the length of the array before you try to reference elements which may not be there. Something like this might work (my VB is a bit rusty, I'm not 100% sure that this is how you check the length):
If PingSplit.Length < 2 Then
MsgBox("Invalid Ping IP!")
End If
Basically, any time you're going to reference items in a collection, you should always do some checking on the collection first to make sure those items exist at all.

Basic URI (Parsing?)

I have been helped by the good people of stackoverflow many times before, so here's my problem...
I haven't been coding for a GOOD while, and for class, we are going to be starting Visual Basic. Visual Basic is really not that hard, but I am not familiar with it, and can't think of a proper way to do this.
As an exercise, I'm coding a very simple web browser. Here's my issue...
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
Dim input As String = TextBox1.Text
Me.WebBrowser1.Navigate(New Uri(input))
If the user types "www.youtube.com" in the address bar, they throw an exception (I presume because there is no http:// at the beginning) However, I can't simply add "http://" to the beginning of the string, because then there is the chance for a double up.
How can I check the string for "http://" and add it accordingly?
You can use regular expression to validate the URL/URI.
Dim pattern = "http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
Dim Inputurl = "http://www.abc.com/aa"
If Regex.IsMatch(Inputurl, pattern) Then
'
Else
'
End If
Or use String.StartsWith() method,
If Inputurl.StartsWith("http://") Then
'
End If
You need to do something like this:
Dim value As String = Mid(input, 1, 7)
if value = "http://" then
'you don't need to modifie the url
else
'you add your http:// string normaly
EndIf
Hope this can help you
PS: sory I corrected some mistakes I maid