Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
Posts: 7
Registered: ‎09-18-2013
My Device: Z10
My Carrier: T-MOBILE
Accepted Solution

Sum numbers and display

Hi, I'm a new developer and trying to learn the native sdk.

I tryed many times, but dont know how I can sum 2 numbers and display, dont have a convert option or number type just text.

I want receive 2 numbers(in diferent textfield) and show the result of the sum in one label.

My code:

--------------------

 

import bb.cascades 1.2

 

Page {

    

    Container {

        //Todo: fill me with QML

        id: containerID

        //layout: DockLayout {}

        background: Color.Red

        layout: StackLayout {

        }

        topPadding: 80.0

        rightPadding: 60

        leftPadding: rightPadding

        onCreationCompleted: {

            OrientationSupport.supportedDisplayOrientation = SupportedDisplayOrientation.All;

        }

 

        TextField {

        id: number1

 

        inputMode: TextFieldInputMode.NumbersAndPunctuation

        }

        TextField {

        id: number2

        inputMode: TextFieldInputMode.NumbersAndPunctuation

        }

 

        Label {

            id: result

            // Localized text with the dynamic translation and locale updates support

            //text: qsTr("Hello World") + Retranslate.onLocaleOrLanguageChanged

            //textStyle.base: SystemDefaults.TextStyles.BigText

        }

 

        Button {

        onClicked: {

            result.value = nuber1.value + number2.value

        }

        }

        

    }

   

}

 

--------------------------------



Thanks for the attention,
David L

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Sum numbers and display

javascript is typeless, if you add variables that contain numbers you should get the addition result.
if that fails you can multiply the variable with 1 to make sure it is a number.

the text of a textfield is in the 'text' property, not 'value'
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Sum numbers and display

[ Edited ]

Sorry Simon, I'm going to disagree with your answer as the following snippet demonstrates.

 

@davidlondubat The function you are looking for is parseInt(), however you should do some checking to make sure the user has actually entered a number (not just a string of commas) but I'll leave that exercise up to you.

 

   Container {

        layout: StackLayout {}

        TextField {
            id: textEdit1
        }

        TextField {
            id: textEdit2
        }

        Label {
            id: textResult
            text: textEdit1.text + textEdit2.text
        }

        Label {
            id: numResult
            text: ""
        }
        
        Button {
            text: "Sum me"
            onClicked: {
                numResult.text = parseInt(textEdit1.text) + parseInt(textEdit2.text);
            }
        }

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
New Contributor
Posts: 7
Registered: ‎09-18-2013
My Device: Z10
My Carrier: T-MOBILE

Re: Sum numbers and display

Thank you guys!