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.
Related
I'm trying to make it so I can have several text boxes in my form show pieces of information from a specific webpage. For example, would there be a way I would be able to retrieve the title of this question to a variable with the click of a button in Visual Basic?
It not hard but you have to look at the source page, and identify the elements.
In nicely formed pages, usually the div elements have a tag ID, but often they don't, so you have to grab say by a attribute name - often you can use the class name of the div in question.
So, to grab the Title, and you question text of this post?
This works:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xDoc As New Xml.XmlDocument
Dim strURL As String = "https://stackoverflow.com/questions/55753982"
Dim xWeb As New WebBrowser
xWeb.ScriptErrorsSuppressed = True
xWeb.Navigate(strURL)
Do Until xWeb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim HDoc As HtmlDocument = xWeb.Document
Debug.Print(HDoc.GetElementById("question-header").FirstChild.InnerText)
Debug.Print(FindClass(HDoc, "post-text"))
End Sub
Function FindClass(Hdoc As HtmlDocument, strClass As String) As String
' get all Divs, and search by class name
Dim OneElement As HtmlElement
For Each OneElement In Hdoc.GetElementsByTagName("div")
If OneElement.GetAttribute("classname") = strClass Then
Return OneElement.InnerText
End If
Next
' we get here, not found, so return a empty stirng
Return "not found"
End Function
OutPut:
(first part is title question)
Is there a way to retrieve some text from a webpage to a textbox in VB?
(second part is question text)
I'm trying to make it so I can have several text boxes in my form show pieces of
information from a specific webpage. For example, would there be a way I would be
able to retrieve the title of this question to a variable with the click of a button
in Visual Basic?
I have an application which has a server and a client file. On the server side, the server's camera (in my case, this is my camera), get's streamed to the client file, and then the clients camera (in my case, my camera because I'm using this for educational purposes only) get's streamed to the server file.
Right now I'm in the process of writing this program, but I'm getting an error when I'm trying to stream the client's camera.
This is what the client program looks like:
Now, on the Form_Load sub, this code is declared
Public Class videocallfinal
Public xxx As Integer
Public Yy As String = "|U|"
Dim pic As New Drawing.Bitmap(500, 400)
Dim Touchless As New TouchlessLib.TouchlessMgr
Dim camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
Private Sub videocallfinal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
q.Comet.Send("camerastream" & Yy & PictureBox2.Image)
PictureBox2.Image = camera1.GetCurrentImage
Touchless.CurrentCamera = camera1
Touchless.CurrentCamera.CaptureHeight = 130
Touchless.CurrentCamera.CaptureWidth = 130
Me.BringToFront()
Me.Text = "You're currently in a video call with " + videocall.Label6.Text
Label6.Text = videocall.Label6.Text
End Sub
So, as you can see, I'm trying to send the PictureBox image to the server file on this line of code:
q.Comet.Send("camerastream" & Yy & PictureBox1.Image)
Under PictureBox1.Image, I get this error
BC30452 Operator '&' is not defined for types 'String' and 'Image'.
I've done some re-search on this issue, but it's usually with different controls, and I've never dealt with an error like this with a PictureBox.
Can someone explain what I'm doing wrong here?
Thanks!
You can convert the image to a Base64 string by using a MemoryStream to get the bytes. The following untested code, written from the docs for MemoryStream and Image, will do exactly this:
Dim s As New MemoryStream
PictureBox1.Image.Save(s, System.Drawing.Imaging.ImageFormat.Bmp)
Dim asBase64 = System.Convert.ToBase64String(s.ToArray())
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!
I have a background worker control that is set to perform a task, and update a multiline text box on my main UI using a delegate procedure. this is all working perfectly, however once the updating scrolls off the bottom of the text box, the scroll bars appear, but the continuous refreshing causes the text box to stay locked at the top. Ideally, I would like the text box to auto-scroll itself to the bottom to show the latest entry in real-time. What would be the best way to implement this?
I have tried using the scrolltocaret() method, with and without a SelectionStart = txtlog.Text.Length command preceding it. perhaps I'm putting it in the wrong place?
some code samples below:
Delegate code:
Delegate Sub updateresults_delegate(ByVal textbox As TextBox, ByVal text As String)
Private Sub updatelog_threadsafe(ByVal textbox As TextBox, ByVal text As String)
If textbox.InvokeRequired Then
Dim mydelegate As New updateresults_delegate(AddressOf updatelog_threadsafe)
Me.Invoke(mydelegate, New Object() {textbox, text})
'Me.txtlog.SelectionStart = txtlog.Text.Length
'Me.txtlog.ScrollToCaret()
Else
textbox.Text = text
End If
End Sub
main backgroundworker activity:
For i As Integer = val1 To val2
'generate an IP address from split host parts and current value of i
host = s1(0) & "." & s1(1) & "." & s1(2) & "." & i
Try 'attempt to ping the IP
Dim reply As PingReply = pingsender.Send(host, timeoutval, buffer, options)
If reply.Status = IPStatus.Success Then
name = System.Net.Dns.GetHostEntry(host)'get DNS entry
resulttext += String.Format("{1} - {2}: reply: Bytes={3} time{4} TTL={5}{0}", vbCrLf, name.HostName, reply.Address.ToString, reply.Buffer.Length, getms(reply.RoundtripTime), reply.Options.Ttl) 'print out success text
Else
resulttext += String.Format(" {1}: Ping failed. {2}{0}", vbCrLf, host, reply.Status.ToString) 'print out fail text
End If
updatelog_threadsafe(txtlog, resulttext) 'send text to textbox
System.Threading.Thread.Sleep(1000)
Catch ex As Exception
End Try
Next
I guess my main question is: I'm pretty certain that the textbox.scrolltocaret() is the correct method to use for what I want, but where is the best place for me to put it? I've tried it in the delegate, the main backgroundworker, as well as before & after the runworkerasync() method. none of these worked, and now I'm stumped!
Try it this way:
'textbox.Text = text
textbox.AppendText(text)
The code you commented out wasn't running on the GUI thread, and as M Granja pointed out, AppendText will automatically scroll to the appended text in the box, so no need to call ScrollToCaret.
xxx.SetFocus ' xxx = the name of the textbox
SendKeys "^{END}" ' pop to last line
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(" - "))