Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[NEED HELP] Currency coverter.
#1
Alright, so I tried to make a currency converter, that connects to http://www.xe.com/ and using xe.com's currency rates.

I can't seem to find a way to get it working.

Any advice or help is appreciated.
Thanks.
Reply
#2
What have you got so far?

The URL is very straight forward, for example:
http://www.xe.com/ucc/convert/?Amount=10...EUR&To=USD

This means all you need to do, is replace the values in the URL, download the page, and extract the results.
Reply
#3
(08-14-2011, 04:35 AM)Fragma Wrote: What have you got so far?

The URL is very straight forward, for example:
http://www.xe.com/ucc/convert/?Amount=10...EUR&To=USD

This means all you need to do, is replace the values in the URL, download the page, and extract the results.

Only the layout, I don't know how I would go about getting the converted number into a textbox.
I guess I would need to use webrequests right?
Reply
#4
Yep, and you will need to use a GetBetween function in order to extract the specific string you need.
Credits go to whoever coded this function. I've forgotten his username.

Code:
Public Function GetBetween(ByRef strSource As String, ByRef strStart As String, ByRef strEnd As String, _
                                Optional ByRef startPos As Integer = 0) As String
        Dim iPos As Integer, iEnd As Integer, lenStart As Integer = strStart.Length
        Dim strResult As String

        strResult = String.Empty
        iPos = strSource.IndexOf(strStart, startPos)
        iEnd = strSource.IndexOf(strEnd, iPos + lenStart)
        If iPos <> -1 AndAlso iEnd <> -1 Then
            strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart))
        End If
        Return strResult
    End Function

If you need anymore help just let me know.
Reply
#5
(08-14-2011, 07:25 AM)Fragma Wrote: Yep, and you will need to use a GetBetween function in order to extract the specific string you need.
Credits go to whoever coded this function. I've forgotten his username.

Code:
Public Function GetBetween(ByRef strSource As String, ByRef strStart As String, ByRef strEnd As String, _
                                Optional ByRef startPos As Integer = 0) As String
        Dim iPos As Integer, iEnd As Integer, lenStart As Integer = strStart.Length
        Dim strResult As String

        strResult = String.Empty
        iPos = strSource.IndexOf(strStart, startPos)
        iEnd = strSource.IndexOf(strEnd, iPos + lenStart)
        If iPos <> -1 AndAlso iEnd <> -1 Then
            strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart))
        End If
        Return strResult
    End Function

If you need anymore help just let me know.

I don't really understand that code, would you mind explaining what each line does?
I would really appreciate it.
Reply
#6
That function will find 2 strings, and read the text in between both of those strings.

For example...

Code:
Dim myWebClient As New WebClient()
Dim myDatabuffer As Byte() = myWebClient.DownloadData("http://www.xe.com/ucc/convert/?Amount=" & NUMBER & "&From=" &EUR&To=USD")
Dim download As String = Encoding.ASCII.GetString(myDatabuffer)

Dim CON As String = GetBetween(download, _
"<td align="right">", "</td>", 0)

That will download the webpage with the results from your search, and extract the string between "<td align="right">" and "</td>", which in this example would be 1 EUR = 1.42440 USD
Reply
#7
(08-14-2011, 07:39 AM)Fragma Wrote: That function will find 2 strings, and read the text in between both of those strings.

For example...

Code:
Dim myWebClient As New WebClient()
Dim myDatabuffer As Byte() = myWebClient.DownloadData("http://www.xe.com/ucc/convert/?Amount=" & NUMBER & "&From=" &EUR&To=USD")
Dim download As String = Encoding.ASCII.GetString(myDatabuffer)

Dim CON As String = GetBetween(download, _
"<td align="right">", "</td>", 0)

That will download the webpage with the results from your search, and extract the string between "<td align="right">" and "</td>", which in this example would be 1 EUR = 1.42440 USD
And where will it extract it to?
Is it possible to make it extact to texbox3?

Reply
#8
What the code above is, is Dims CON as the extracted string. So to show the string into a textbox, simply do:

Code:
Textbox3.Text = CON

Reply
#9
I'm getting some errors.
[Image: OKcoo.png]
Do you happen to know why I get these errors.
This is what I have in the lines:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myWebClient As New WebClient()
Dim myDatabuffer As Byte() = myWebClient.DownloadData("http://www.xe.com/ucc/convert/?Amount=" & amount.text & "&From=" & combobox1.text & "&to=" & combobox2.Text ")
        Dim download As String = Encoding.ASCII.GetString(myDatabuffer)

Dim CON As String = GetBetween(download, _
"<td align="right">", "</td>", 0)
    End Sub
Reply
#10
What errors are you getting? The code I gave you might not work 100% it was just an example.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)