textbox problem

BlitzPlus Forums/BlitzPlus Programming/textbox problem

Armor Nick(Posted 2008) [#1]
Greetings everyone,

I made my first program with BlitzPlus, but there seems to be an error which I can't solve. I'm trying to create a little converter between celsius and fahrenheit, but adding the result to a textbox doesn't seem to work. Can someone tell me what I am doing wrong? This is the code:

;*******************************
; Nick's Convert0r
;*******************************
; By: Armor Nick
; Date: 2007-06-09
;*******************************

; Main Window Initialization
frmMain = CreateWindow("Nick's Convert0r",0,0,602,444,0,1) 
txtInput = CreateTextField(105, 23, 356, 21, frmMain)
btnF2C = CreateButton("Fahrenheit to Celcius",105, 72,357, 23,frmMain)
btnC2F = CreateButton("Celcius to Fahrenheit",105, 118,357, 23,frmMain)
btnAbout = CreateButton("About",12, 375,75, 23,frmMain)
txtOutput = CreateTextArea(105, 160,354, 210,frmMain)

; Main Window Loop
Repeat 
	If WaitEvent()=$401 Then 
		If EventSource()=btnF2C Then ConvertF2C(TextFieldText$(txtInput))
		If EventSource()=btnC2F Then ConvertC2F(TextFieldText$(txtInput))
		If EventSource()=btnAbout Then ShowAbout()
	End If 
	If WaitEvent()=$803 Then 
		Exit 
	End If 
Forever 
End 

; Fahrenheit to Celcius conversion
Function ConvertF2C( msg$ )
	degrF# = msg$
	degrC# = (5.0/9.0)*(degrF#-32.0)
	output = Str degrC#
	AddTextAreaText txtOutput,  output
End Function

;Celcius to Fahrenheit conversion
Function ConvertC2F( msg$ )
	degrC# = msg$
	degrF# = 32.0 + (9.0/5.0) * degrC#
	output = Str degrF#
	AddTextAreaText txtOutput, Str degrF#
End Function

Function ShowAbout()
	Notify "Test Program by Armor Nick"
End Function



markcw(Posted 2008) [#2]
Try making txtOutput a global or passed as a parameter.


Armor Nick(Posted 2008) [#3]
Yes, that made it work.
Thanks a lot!


markcw(Posted 2008) [#4]
Hey Nick, glad to help.

Technically variables in Blitz default to Local, so functions won't recognise them unless they are Global. I suppose that means we don't have to make variables Local in functions but I don't really know so I use Local in functions all the time just to be sure.


markcw(Posted 2008) [#5]
Oh yeah, I remember why now. Variables in functions will be Local UNLESS you have a Global of the same name, so to avoid conflicts you use Local in functions. It's good practise to always specify a variable as either Local or Global to avoid confusion.