+ Start a Discussion
The SFDC MatrixThe SFDC Matrix 

How to use single inputTextField to Create a Calculator with Controller Class in Visual Force?

For Example to create calculator i want to use only one

<apex:page controller="classname">
<apex:inputText value="somevalue" action="{!someaction}"/>
</apex:page>

With this single inputText and On clicking the button , the values in the text field should be passed to the controller class and generates output.
ClintLeeClintLee
Here's a very basic example:

Controller:
public with sharing class Calculate
{
    public String inputNumber { get; set; }
    public Double outputNumber { get; private set; }

    public Calculate() 
    { 
        inputNumber  = 0;
        outputNumber = 0;
    }

    public PageReference addOne()
    {
        if(inputNumber != null && inputNumber != '')
            outputNumber = Double.valueOf(inputNumber) + 1.0;
    
        return null;
    }
}
VF Page:
<apex:page controller="Calculate">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Add 1" action="{!addOne}" rerender="output" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Enter Value" />
                    <apex:inputText value="{!inputNumber}" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem id="output">
                    <apex:outputLabel value="Output" />
                    <apex:outputText value="{!outputNumber}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

The VF page has an inputText that is bound to the controller's inputNumber variable.  When the button is clicked, the addOne() method is executed and the inputNumber is incremented by 1.  The resulting number is assigned to the outputNumber variable and displayed back on the VF page.

Hope that helps,

Clint
aparna sangaleaparna sangale
Hi can you send me the screenshot of output means how calculator looks like, and does it only do additionno other functions?