简体   繁体   English

使用Dynamic AX 2012创建简单的计算器

[英]Create simple calculator with Dynamic AX 2012

I'm new with Dynamic AX and I want to create a simple calculator with input values and display the result in the form: 我是Dynamic AX的新手,我想用输入值创建一个简单的计算器,并以以下形式显示结果:

在此处输入图片说明

Your question is very broad to be answered precisely because there are a lot of strategies to tackle the task, but judging from the screenshot you have provided you have a class which should contain all calculation logic and a form to provide UI to the user with two input fields and one output field which should display operation result. 您的问题非常广泛,可以精确地回答,因为有很多策略可以解决该任务,但是从您提供的屏幕快照来看,您有一个应该包含所有计算逻辑的类和一个向用户提供两个UI的表单输入字段和一个输出字段应显示操作结果。

So the easiest solution would be: 因此,最简单的解决方案是:

  1. Implement the Kalkulator class which exposes two parm methods to set up the operands and four methods which execute the operation and return the result: add , subtract , multiply and divide . 实现Kalkulator类,该类公开两个parm方法来设置操作数,以及四个方法来执行该操作并返回结果: addsubtractmultiplydivide
  2. Create a private instance of the Kalkulator class in your form, initialize it, set up operands when user clicks one of the buttons, call appropriate method to run the operation and output the result on the form field. 在您的表单中创建Kalkulator类的私有实例,对其进行初始化,在用户单击按钮之一时设置操作数,调用适当的方法以运行该操作并在表单字段上输出结果。

So supposing that operands are integer values (for demonstrative purpose) your TRN_Kalkulator may look something like this: 因此,假设操作数是整数值(出于说明目的),您的TRN_Kalkulator可能看起来像这样:

class TRN_Kalkulator
{
    private int value1;
    private int value2;

    public int parmValue1(int _value = value1)
    {
        value1 = _value;
        return value1;
    }

    public int parmValue2(int _value = value2)
    {
        value2 = _value;
        return value2;
    }

    public int Sum()
    {
        return value1 + value2;
    }

    public int Diff()
    {
        return value1 - value2;
    }

    public int Mult()
    {
        return value1 * value2;
    }

    public int Div()
    {
        return value2 == 0 ? 0 : value1 / value2;
    }

}

In the class declaration on the form you have to declare a private instance of TRN_Kalkulator which will be initialized by overriding the init() method: 在表单的类声明中,您必须声明TRN_Kalkulator的私有实例,该实例将通过重写init()方法进行初始化:

TRN_Kalkulator calculator;
//...
public void init()
{
    super();
    calculator = new TRN_Kalkulator();
}

Finally when one of the buttons is clicked you parse user input by reading the values of the form fields, set up the operands, run the operation and output the result. 最后,当单击其中一个按钮时,您可以通过读取表单字段的值来分析用户输入,设置操作数,运行操作并输出结果。 All of this is done by overriding click() method on each of the buttons: 所有这些都是通过覆盖每个按钮上的click()方法来完成的:

// read text values of the textboxes and parse them to integer
int a = str2Int(TxtOperand1.text());
int b = str2Int(TxtOperand2.text());

// set up calculator operands
calculator.parmValue1(a);
calculator.parmValue2(b);

// call the operation depending on which button was clicked
int result = calculator.Sum();

// set result textbox text
TxtResult.text(int2Str(result));

Notice that there are a lot of ways to improve this code (like for example using some display and edit methods on the form) and you definitely should do it, but this implementation suits your current setup and should point you in the right direction. 请注意,有很多方法可以改进此代码(例如,在表单上使用某些显示编辑方法),您当然应该这样做,但是此实现适合您当前的设置,并且应为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM