根据用户输入实时更新字段

时间:2017-05-29 12:55:28

标签: c# field real-time

这里总计n00b,我正在构建一个简单的应用程序来进行一些计算。截至目前,我仍然坚持让我的字段连接到我的变量并实时显示结果。我在变量初始化方面遇到错误。

以下是我想要实现的类似示例: https://media.giphy.com/media/xUPGcK74nHdNpEnmKs/giphy.gif

这是我的用户界面: https://image.ibb.co/hUvm3F/2017_05_29_13_46_19_Risk_Calc_Form1_cs_Design.png

到目前为止,这是我的代码:

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            double price;               //entered by user or fetched
            double balance;             //entered by user or fetched   
            double equity;              //entered by user or fetched
            Int32  leverage;            //entered by user
            double availableUnits;      //calculated value
            double margin80;            //calculated margin of 80%
            double margin50;            //calculated margin of 50%
            double marginCall;          //calculated margin of 30%
            double marginStopOut;       //calculated margin of 15%
            Int32  units;               //units for trading, entered by user
            double TP;                  //Take Profit entered by user
            double SL;                  //Stop Loss entered by user
            double tradeStopOut;        //calculated rate at which trade has 100% loss
            double marginUsed;          //calculated funds invested in the market
            double positionValue;       //calculated funds invested accounting for leverage
            double profitCash;          //calculated profit in currency
            double lossCash;            //calculated loss in currency
            double profitPct;           //calculated profit in percentage
            double lossPct;             //calculated loss in percentage
            double balanceLoss;         //calculated balance + loss
            double balanceWin;          //calculated balance + win
            double rrr;                 //calculated risk reward ratio


            //calculating the number of available units to trade
            availableUnits = equity / price * leverage;

            //calculating critical margin levels
            margin80 = balance * 0.8;
            margin50 = balance * 0.5;
            marginCall = balance * 0.3;
            marginStopOut = balance * 0.15;

            //calculating trade stop out level
            tradeStopOut = price - (price / leverage);

            //calculating margin used and position value
            marginUsed = (price * units) / leverage;
            positionValue = price * units;

            //calculating profit and loss ( cash, percentile, +balance)
            profitCash = units * TP - (units * price);
            lossCash = units * SL - (units * price);

            profitPct = profitCash / balance * 100;
            lossPct = lossCash / balance * 100;

            balanceLoss = price + lossCash;
            balanceWin = price + profitCash;

            //calculating risk reward ratio (RRR)
            rrr = SL / TP;


            //UI TEXTBOXES

            balance = Convert.ToDouble(balanceTextbox.Text);
            equity = Convert.ToDouble(equityTextbox.Text);
            price = Convert.ToDouble(priceTextbox.Text);
            leverage = Convert.ToInt32(leverageTextbox.Text);
            units = Convert.ToInt32(unitsTextbox.Text);
            TP = Convert.ToDouble(tpTextbox.Text);
            SL = Convert.ToDouble(slTextbox.Text);

            //UI LABELS

            availableUnits = Convert.ToDouble(availableUnitsLabel.Text);
            marginUsed = Convert.ToDouble(marginUsedLabel.Text);
            positionValue = Convert.ToDouble(positionValueLabel.Text);
            profitCash = Convert.ToDouble(profitCashLabel.Text);
            lossCash = Convert.ToDouble(lossCashLabel.Text);
            profitPct = Convert.ToDouble(profitPctLabel.Text);
            lossPct = Convert.ToDouble(lossPctLabel.Text);
            tradeStopOut = Convert.ToDouble(tradeStopOutLabel.Text);
            rrr = Convert.ToDouble(rrrLabel.Text);
            balanceLoss = Convert.ToDouble(balanceLossLabel);
            balanceWin = Convert.ToDouble(balanceWinLabel);
            margin80 = Convert.ToDouble(margin80Label.Text);
            margin50 = Convert.ToDouble(margin50Label.Text);
            marginCall = Convert.ToDouble(marginCallLabel.Text);
            marginStopOut = Convert.ToDouble(marginStopOutLabel.Text);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以做一个函数来做所有的计算,并相应地更新字段,并在更改输入字段值时调用它,为所需的所有输入添加文本更改侦听器,并在每次更改时调用该函数:) example

答案 1 :(得分:0)

您需要添加活动

        public Form1()
        {
            InitializeComponent();

            balanceTextbox.TextChanged += new EventHandler(balanceTextbox_Changed);
        }
        private void balanceTextbox_Changed(object sender, EventArgs e)
        {
        }