Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据用户输入实时更新字段_C#_Field_Real Time - Fatal编程技术网

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

C# 根据用户输入实时更新字段,c#,field,real-time,C#,Field,Real Time,总n00b在这里,我正在建立一个简单的应用程序做一些计算。到目前为止,我一直无法让字段连接到变量并实时显示结果。我在变量初始化方面遇到了错误 下面是一个与我想要实现的目标类似的示例: 这是我的用户界面: 这是我到目前为止的代码: { public partial class Form1 : Form { public Form1() { InitializeComponent(); double

总n00b在这里,我正在建立一个简单的应用程序做一些计算。到目前为止,我一直无法让字段连接到变量并实时显示结果。我在变量初始化方面遇到了错误

下面是一个与我想要实现的目标类似的示例:

这是我的用户界面:

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

{
    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);

        }
    }
}

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

        public Form1()
        {
            InitializeComponent();

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

我不知道您正在使用的UI技术,但是如果是WPF,那么您可以在
INotifyPropertyChanged
界面的帮助下使用数据绑定,该界面实时同步属性和UI组件。如果您使用的是好的旧WinForms,那么您需要挂接到各种用户控件的事件中,并手动更新必要的属性。更新:好的,我看到你正在使用WinForms,所以你可以钩住控件的事件,然后更新必要的属性。这和实时有什么关系?如果希望在每个按键事件上更新值,请处理按键event@PanagiotisKanavos除了实时,我不知道如何放置它,因为当我搜索解决方案时,我发现的大多数示例都包括一个按钮,我不想在每次更改值时按下该按钮。所以当我改变其他值时,它必须实时改变所有变量。这里已经有一些很好的答案,可以测试一些东西。@G10Link这是因为您搜索的东西不正确。关于如何使用keypress或keydown事件,有很多例子。这个按钮只会生成一个点击事件,就像大多数可点击的控件一样,我还没弄明白,但它没问题。我会继续努力。。。感谢大家花时间回复。