AutoHotkey Community

It is currently May 27th, 2012, 11:55 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: October 2nd, 2010, 7:39 pm 
Offline

Joined: November 11th, 2005, 3:13 am
Posts: 202
i'm trying to write a simple currency converter that will show several currencies at the same time. the idea is that if i key-in in one edit-box the others would change as well.

however this does not happen. the numbers are continuously processed. can someone take a look and help me find the bug?

Code:
#Persistent
#SingleInstance, Force
#NoEnv

SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
SetFormat, Float, 0.2

currencydataxml := A_ScriptDir "\Resources\Rates\eurofxref-daily.xml"
imageDir := A_ScriptDir "\Resources\Images"
euro_rate := 1  ; Euro is the base rate

IfNotExist, %currencydataxml%
  {
  Text=Downloading the latest currency rates.
  SplashImage, , b CTWhite CW000000 fs12, %Text%.
  URLDownloadToFile, http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml, %currencydataxml%
  Sleep, 4000
  SplashImage, Off
  }

 
path = %currencydataxml%
xpath_load(rss, path)
date1 := XPath(rss, "/gesmes:Envelope/Cube/Cube/@time/text()")

currency1 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""JPY""]/@currency/text()")
rate1 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""JPY""]/@rate/text()")
currency2 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""USD""]/@currency/text()")
rate2 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""USD""]/@rate/text()")
currency3 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""GBP""]/@currency/text()")
rate3 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""GBP""]/@rate/text()")
currency4 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""SGD""]/@currency/text()")
rate4 := XPath(rss, "/gesmes:Envelope/Cube/Cube/Cube[@currency=""SGD""]/@rate/text()")

GoSub, LoadGUI
Return

LoadGUI:
Gui, Font, S7 CDefault, Verdana
Gui, Add, GroupBox, x6 y6 w290 h150 , Last Update: %date1%
Gui, Font, S10 CDefault, Verdana
Gui, Font, W700
Gui, Add, Picture, x26 y35 , %imageDir%\flag_jpy.png
Gui, Add, Text, xp+40 y35 w100 h20 , JPY
Gui, Add, Edit, xp+120 y35 w90 h20 vEdit1 gEditNum, 0.00

Gui, Add, Picture, x26 y60 , %imageDir%\flag_usd.png
Gui, Add, Text, xp+40 y60 w100 h20 , USD
Gui, Add, Edit, xp+120 y60 w90 h20 vEdit2 gEditNum, 0.00

Gui, Add, Picture, x26 y85 , %imageDir%\flag_gbp.png
Gui, Add, Text, xp+40 y85 w100 h20 , GBP
Gui, Add, Edit, xp+120 y85 w90 h20 vEdit3 gEditNum, 0.00

Gui, Add, Picture, x26 y110 , %imageDir%\flag_sgd.png
Gui, Add, Text, xp+40 y110 w100 h20 , SGD
Gui, Add, Edit, xp+120 y110 w90 h20 vEdit4 gEditNum, 0.00
Gui, Show, Center, Currency Convertor
Return


EditNum:              ;--- update converted figures     
Gui, Submit, NoHide
If (A_GuiControl = "Edit1")
  {
  GuiControlGet, Numb,,%A_GuiControl%
  If Numb is Number
    {
    ControlSetText, Edit2, % (euro_rate/rate2)/(euro_rate/Numb)
    ControlSetText, Edit3, % (euro_rate/rate3)/(euro_rate/Numb)
    ControlSetText, Edit4, % (euro_rate/rate4)/(euro_rate/Numb)
    }
  }
Else If (A_GuiControl = "Edit2")
  {
  GuiControlGet, Numb,,%A_GuiControl%
  If Numb is Number
    {
    ControlSetText, Edit1, % (euro_rate/rate1)/(euro_rate/Numb)
    ControlSetText, Edit3, % (euro_rate/rate3)/(euro_rate/Numb)
    ControlSetText, Edit4, % (euro_rate/rate4)/(euro_rate/Numb)
    }
  }
Else If (A_GuiControl = "Edit3")
  {
  GuiControlGet, Numb,,%A_GuiControl%
  If Numb is Number
    {
    ControlSetText, Edit1, % (euro_rate/rate1)/(euro_rate/Numb)
    ControlSetText, Edit2, % (euro_rate/rate2)/(euro_rate/Numb)
    ControlSetText, Edit4, % (euro_rate/rate4)/(euro_rate/Numb)
    }
  }
Else If (A_GuiControl = "Edit4")
  {
  GuiControlGet, Numb,,%A_GuiControl%
  If Numb is Number
    {
    ControlSetText, Edit1, % (euro_rate/rate1)/(euro_rate/Numb)
    ControlSetText, Edit2, % (euro_rate/rate2)/(euro_rate/Numb)
    ControlSetText, Edit3, % (euro_rate/rate3)/(euro_rate/Numb)
    }
  }

Return


GuiEscape:
GuiClose:
ExitApp
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2010, 8:02 pm 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
The rate variables are blank in my tests. I added this debug line after the the xpath lines and it showed " | | | ". It should show something like "1.00 | 3.98 | 1.36 | 2.21".

Code:
TrayTip, Rates, %Rate1% | %Rate2% | %Rate3% | %Rate4%

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 2nd, 2010, 11:50 pm 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
Code:
#NoEnv
#SingleInstance, Force
SetFormat, Float, 0.15
SetWorkingDir, %A_ScriptDir%

Converter_Currencies = JPY,USD,GBP,SGD,EUR ; display these currencies in the gui

Gui, Font, S7 CDefault, Verdana
Gui, Add, GroupBox, x6 y6 w296 h15 vGroupBox, Loading Rates...
Gui, Font, S10 CDefault, Verdana
Gui, Font, W700
Gui, Margin, 6, 18
Gui, Add, Text, xp+5 yp+8 w0 h0 section hwnddummx
ControlGetPos,, dummx,,,, Ahk_ID %dummx%

Loop, Parse, Converter_Currencies, `,
{
    Gui, Add, Text, xs w0 h0 section
    Gui, Add, Picture, xs ys, %imageDir%\flag_%A_LoopField%.png
    Gui, Add, Text, xp+40 ys w100 h20 , %A_LoopField%
    Gui, Add, Edit, xp+120 ys w90 h20 vEdit_%A_LoopField% gEditNum, 0.00
}
Gui, Add, Text, xs w0 h0 hwnddummy
Gui, Margin,, 6
ControlGetPos,, dummy,,,, Ahk_ID %dummy%
GuiControl, Move, GroupBox, % "H" dummy - dummx + 8

Gui, Show, Center, Currency Convertor

SetTimer, UpdateCurrency, % 90 - 30 * 60 * 1000 ; reload rates every 30 minutes

UpdateCurrency:
    URLDownloadToFile, http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml, xrate.txt
    FileRead, rates, xrate.txt
    FileDelete, rates, xrate.txt
    pos := 0, EUR := 1.0000 ; base rate
    needleregex = \G<Cube currency=['"](?<_currency>\w+)['"] rate=['"]\K[^'"]+
    While pos := InStr( rates, "<Cube currency", 0, pos + 1 )
        If RegexMatch( rates, needleregex, rate, pos )
            %rate_currency% := rate
    FormatTime, date
    GuiControl,, GroupBox, Last Update: %date%
Return ; end AES

EditNum:
    GuiControlGet, %A_GuiControl%
    StringReplace, rate_currency, A_GuiControl, Edit_
    pos := %A_GuiControl% / %rate_currency%
    Loop, Parse, Converter_Currencies, `,
    {
        IfEqual, rate_currency, %A_LoopField%, Continue
        GuiControl, -g, Edit_%A_LoopField%
        GuiControl,, Edit_%A_LoopField%, % Round( pos * %A_LoopField%, 2 )
        GuiControl, +gEditNum, Edit_%A_LoopField%
    }
Return

GuiClose:
Exitapp

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2010, 11:47 am 
Offline

Joined: November 11th, 2005, 3:13 am
Posts: 202
thanks [VxE], works fine! anyway just wondering what was wrong with my code?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 3rd, 2010, 7:40 pm 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
Take a very close look at the math I used to translate the rates. Also, notice that my code disables the g-label while updating the other edit boxes so that they don't start to snowball.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group