April 19, 2024, 08:19:54 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Problem : Convert String to Double and reverse

Started by Techno, June 22, 2015, 01:01:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Hi all,

I have developed my first application with the IWB+ Designer. When I calculate the Resistor the result is "0".
What wrong here: I try it with STR$, USING and VAL but it give me the same result
I send only the important part of the code



/* The Global Section */
DEF strVolt AS STRING
DEF strCurrent AS STRING
DEF strResistor AS STRING

DEF U AS DOUBLE
DEF I AS DOUBLE
DEF R AS DOUBLE
SETPRECISION(3)



/*  The eventhandler */


SUB OnClick_main_btnCalcResistor(),INT

'Input
strVolt     = GETCONTROLTEXT(main,main_txtVolt)
strCurrent  = GETCONTROLTEXT(main,main_txtCurrent)
U = VAL(strVolt)
I = VAL(strCurrent)

'Calculate
R = U / I
strResistor = STR$(R)
SETCONTROLTEXT(main,main_txtResistor, strResistor))
RETURN FALSE
ENDSUB



I ask only witch commands do I used

Kind regards
Stephane

LarryMc

Using just the info you posted above the code works (see attached screenshot)

For me to help you further zip the ENTIRE project folder and post here as an attachment so I can look at your complete project.
If you do not post your complete project folder in a zip file I can not help you.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Here's a simple test program to calculate Resistance which might help ..  :)

There's no need to keep converting values to and from string and doubles.
Also OOP'y thinking doesn't help for this type of program in my opinion. Just go for it.

There's no point in high precision (double) calculations, since high precision resistors are not readily available.

Here's the code in EBasic - so there may be error messages in IWB ..  ::)


' Resistance Calculator - EBasic code

def wstyle,Width,Height,run:int
def a$:string

def Volt,Current,Resistor:float
' (no point in higher precision - resistors aren't manufactured to high precision)

autodefine "OFF"

window w

Width=800
Height=600

run = 1

wstyle = @minbox|@maxbox
openwindow w,0,0,Width,Height,wstyle,0,"Resistance",&main
setwindowcolor w,rgb(0,70,120)
centerwindow w

control w,@Button,"Exit",(width-80)/2,500,80,30,0,1
setcontrolcolor w,1,0,rgb(80,100,170)

control w,@edit,"",(width-100)/2,80,100,25,@CTEDITCENTER,10

frontpen w,rgb(50,180,10)
backpen w,rgb(0,70,120)

a$ = "Voltage"
move w,280,85
print w,a$

control w,@edit,"",(width-100)/2,150,100,25,@CTEDITCENTER,20

a$ = "Current"
move w,280,155
print w,a$

control w,@static,"",(width-100)/2,250,100,25,@CTEDITCENTER|0x200,30
' (the 0x200 value centers text vertically in a text box)

a$ = "Resistance"
move w,255,255
print w,a$

control w,@Button,"Calculate Resistance",(width-150)/2,350,150,30,0,5
setcontrolcolor w,5,0,rgb(80,100,170)

waituntil run = 0
closewindow w
end

sub main
select @MESSAGE
case @IDCLOSEWINDOW
   run = 0
case @idcontrol
select @controlid
case 1 :' Exit
run = 0
case 5 :' Calculate R
'Input
Volt = val(GETCONTROLTEXT(w,10))
Current  = val(GETCONTROLTEXT(w,20))

'Calculate R = V / I provided values only if V and I exist
if (Volt > 0) & (Current > 0)
Resistor = Volt / Current
SETCONTROLTEXT(w,30,ltrim$(str$(Resistor)))
endif

endselect
endselect
endsub



best wishes, :)

Graham
Tomorrow may be too late ..

LarryMc

You're absolutely correct Graham if then end game here was just to  to calculate resistance.

This exercise of his is a little deeper than that.  And it has absolutely nothing to do with OOP.

He is trying to learn how to learn to use IWB+ (my visual designer for IWBasic) which automatically writes basic code shells and then lets the user sort of fill in the blanks with their specific app code.

He apparently took something simple that he understood (ohms law) and made a stab at building an app around that for the 1st time.
So, I cut him some slack on the SETPRICISION (the command is even located in the wrong place) but I left it where he had it to see if it was causing his problem.

Bottom line is this is a learning exercise..... ;)

Just remembered something (don't know why I remembered it now) a while back you where making some comparisons between CB and IWB and you said something to the effect that in CB you could do simple a=a+1 where in IWB it had to be a++ or a+=1 but the truth is that in IWB you can do all 3 a=a+1, a++, or a+=1.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Techno

June 23, 2015, 01:57:40 AM #4 Last Edit: June 23, 2015, 02:00:37 AM by Techno
Hi all,

I have my first project build with the IWB+ Visual Designer.
You can see it in attachment.


I have a few questions about my application:

1) How can I make an edit box read-only at runtime ?

2) How can I determine if text is entered in the edit box where it needs to be emptied.  I have two edit boxes and state entered anything but that should not . How can I test and empty them?

3) How do I navigate through my edit box with the tab key because that obviously does not work. Is there an message that I call it for activate the tab key?

Thanks
Stephane

Brian

Techno,

Have a look at the ENABLETABS command for your last query

Brian

LarryMc

1st of all I can't tell what caused your original problem because you modified your code before posting the entire project and that part works now.  In the future, don't do that.

You need to change this sub
SUB OnClick_main_btnClearAll(),INT
IF(U != 0) & (I != 0) & (R != 0) THEN
SETCONTROLTEXT(main,main_txtVolt,"")
SETCONTROLTEXT(main,main_txtCurrent,"")
SETCONTROLTEXT(main,main_txtResistor,"")
ENDIF

RETURN FALSE
ENDSUB

to
SUB OnClick_main_btnClearAll(),INT
U = 0
I = 0
R = 0
SETCONTROLTEXT(main,main_txtVolt,"")
SETCONTROLTEXT(main,main_txtCurrent,"")
SETCONTROLTEXT(main,main_txtResistor,"")
RETURN FALSE
ENDSUB


In order to tab between the edit controls you have to do two things. 1st you have to use the ENABLETABS command as Brian stated.
Place it EXACTLY as shown here:
SUB OpenMainWindow()
'@@CREATE_main
OPENWINDOW main,0,0,402,179,@CAPTION|@FIXEDSIZE|@MINBOX|@SYSMENU,0,"AVO Calculator 1.00",&main_handler
'@@
ENABLETABS MAIN, 1
'@@ICON

Then you have to set the @TABSTOP style flag for each control you want to tab between (in this case the 3 edit controls). I previously explained how do this to you but I'll do it again.
In the Form Editor select the desired edit control by clicking on it.  Then, over in the Property Window in the Style box right click and select Styles from the popup menu.  From the Styles Manager move the @TABSTOP flag from the left side to the right side and then save.  Do this for all 3 edit controls.  If you do this properly  the resulting code will look like this:  
CONTROL main,@EDIT,"",125,10,128,28,@CTEDITAUTOH|@TABSTOP,main_txtVolt
CONTROL main,@EDIT,"",125,45,128,29,@CTEDITAUTOH|@TABSTOP,main_txtCurrent
CONTROL main,@EDIT,"",125,80,128,29,@CTEDITAUTOH|@TABSTOP,main_txtResistor


To make the edit control read-only you have to use the @CTEDITRO flag
To do it at run-time requires the use of the MODIFYSTYLE command
The following would making each of the edit controls read-only
MODIFYSTYLE main, @CTEDITRO, 0, main_txtVolt
MODIFYSTYLE main, @CTEDITRO, 0, main_txtCurrent
MODIFYSTYLE main, @CTEDITRO, 0, main_txtResistor

and the following would restore them to being editable
MODIFYSTYLE main, 0, @CTEDITRO, main_txtVolt
MODIFYSTYLE main, 0, @CTEDITRO, main_txtCurrent
MODIFYSTYLE main, 0, @CTEDITRO, main_txtResistor


The following line of code will check to see if an edit control is empty and if it is not then it will empty it
If GETCONTROLTEXT(main,main_txtCurrent)<>"" then SETCONTROLTEXT(main,main_txtCurrent,"")

I believe that answers all your questions
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bruce Peaslee

Techno,

Everyone learns in their own way, but I always findit useful to look stuff up first. All of your recent questions are in the help section. For me, becoming familiar with it makes getting answers faster.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

ckoehn

I agree with Bruce.  If I search the help file and the forum, I can find answers to almost all of my questions.

Later,
Clint

Egil

June 24, 2015, 12:10:39 PM #9 Last Edit: June 24, 2015, 12:16:19 PM by Egil
I quite agree with Bruce and Clint, but the claim is really only valid for people in English-speaking countries.
For people using other languages, most of the special words often used in computer and programming textbooks are never found in dictionaries. I think Techno's mother tongue is Dutch, so that can be the reason for misunderstanding some details... in addition to a large amount of impatience.

And finally, it might be somewhat easier for other forum users to help if he learned to give feedback showing that he understands the explanations of the first problem, instead of jumping into something completely different. And maybe some day, if he gets the right advice, he will end up to be the next sapero. ;D ;D


Regards,
Egil.
Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

You all make valid points.  I have to grit my teeth dealing with him sometimes (as you can tell).
But if you look at his activity here and elsewhere he really means no harm; he just has a personality that comes across as rather crude and rough around the edges.

The biggest problem I have is with his broken English. Yet, here I am, I can barely speak one language. And Egil, Sapero, and others are better than me.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

I don't know if it helps, but Techno's questions did teach me some thing new.  I did not know about the ENABLETABS command.  Will need to read up on it to see how it works.

What I used was:

' Allow Edit Box to pass along Tab and Enter Key's
SETCONTROLNOTIFY(d1,D1_EP,1,1)


Bill
When all else fails, get a bigger hammer.

LarryMc

ENABLETABS only works on windows; is ignore in dialogs
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

QuoteI did not know about the ENABLETABS command.  Will need to read up on it to see how it works
what
this is funny ...ENABLETABS is old command..
anyway if you wish to avoid problems with TAB-ing in window use dialog as child to
parent window and then TAB -ing work by default..

billhsln

Since it is a windows thing, that probably explains it.  I mostly use Dialogs for data entry.

Bill
When all else fails, get a bigger hammer.