当用户输入文本框中的文本更改时,如何更新表单上的文本框 [英] How can I update text box's on my form when text changes in the user input text box's

查看:41
本文介绍了当用户输入文本框中的文本更改时,如何更新表单上的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新学习C#而且我正在重新创建我很久以前制作的电子表格程序,它模拟了我曾经玩过的游戏中技能系统的运作方式(Anarchy Online)。

Hi, I am new learning C# and i am recreating my spreadsheet program i made a long time ago which emulates how the skills system works in a game i used to play (Anarchy Online).

一个按钮用于执行计算并将值发送到文本框

A button is used to perform the calculations and send the values to the text boxs

四个文本框是用户输入和另外七个文本框文本框用于显示正在计算的数字。

Four text boxs are user input and seven other text boxs are used to display a number that are being calculated among eachother.

我只想执行计算并在用户键入任何用户输入文本时发送到文本框盒子所以我可以摆脱计算按钮

I just want to perform the calculations and send to the text boxs when the user types in ANY of the user input text boxs so i can get rid the the calculate button

到目前为止这个代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AOSE_Csharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }

        // %-Key used to determin how much points trickledown to the skill that is based on points
        // spent on one or more ability that it is dependant on - in this case i use one skill
        // that is dependant on one ability therefore i only need one percentKey for now
        private double percentKey100 = 0.25;
        // these two are user input
        public double nakStm; // B-ability
        public double addedStm; // O-ability

        private double startingPointsAbilities = 6; // point character starts with
        private double startingPointsSkills = 4; // points character starts with

        private double spentIPStm; // F-ability = nakAbillity - startingAbilityPoints
        // these two are user input also
        public double nakBdyDev; // B-skills
        public double addedBdyDev; // O-skills

        private double totalStm; // G-ability = IP Spent + startingAbilityPoints + addedAbility
        // C-nakTrickledown and Collumn D-totalTrickledown
        private double nakTrickleBdyDev; // C-nakTrickle = percentKey100 * nakAbility

        private double totalTrickleBdyDev; // D-totalTrickle = percentKey100 * totalAbility        
        private double trickleAddedAbiOnlyBdyDev; // E-addedTrickle = D-totalTrickle - C-nakTrickle
        private double totalSpentIPBdyDev; // F-skill = B-skill - C-nakTrickle - startingSkill

        private double totalBdyDev; // G-Skill = B-skill + E-addedTrickle + O-skill


        private void button1_Click(object sender, EventArgs e)
        {
            // These textbox's have to be set to txtbox's and converted to double first so that
            // the following formulas can use them and not get a conversion error.
            nakStm = Convert.ToDouble(nakStmTxtBox.Text); // B-ability
            addedStm = Convert.ToDouble(addedStmTxtBox.Text); // O-ability
            nakBdyDev = Convert.ToDouble(nakBdyDevTxtBox.Text); // B-skills
            addedBdyDev = Convert.ToDouble(addedBdyDevTxtBox.Text); // O-skills
            // perform nessary calculation...          
            spentIPStm = nakStm - startingPointsAbilities; // F-ability
            totalStm = spentIPStm + startingPointsAbilities + addedStm; // G-ability
            // trickledown calculation
            nakTrickleBdyDev = Math.Ceiling(percentKey100 * nakStm); // C-nakTrickle
            totalTrickleBdyDev = Math.Ceiling(percentKey100 * totalStm); // D-addedTrickle
            trickleAddedAbiOnlyBdyDev = totalTrickleBdyDev - nakTrickleBdyDev; // E-Skill
            // Spent IP Points - for viewing only not used for other calculations
            totalSpentIPBdyDev = nakBdyDev - nakTrickleBdyDev - startingPointsSkills; // F-Skill
            // total skill after trickledown added in
            totalBdyDev = nakBdyDev + trickleAddedAbiOnlyBdyDev + addedBdyDev; // G-Skill


            // show values in text box's
            spentIPStmTxtBox.Text = spentIPStm.ToString(); // F-ability
            totalStmTxtBox.Text = totalStm.ToString(); // G-ability

            nakTrickleBdyDevTxtBox.Text = nakTrickleBdyDev.ToString(); // C-nakTrickle
            totalTrickleBdyDevTxtBox.Text = totalTrickleBdyDev.ToString(); // D-totalTrickle
            trickleAddedAbiOnlyBdyDevTxtBox.Text = trickleAddedAbiOnlyBdyDev.ToString(); // E-addedTrickle

            totalSpentIPBdyDevTxtBox.Text = totalSpentIPBdyDev.ToString(); // F-skill

            totalBdyDevTxtBox.Text = totalBdyDev.ToString(); // G-skill

            // Properties.Settings.Default.Save();        
        }
    }
}




推荐答案

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged%28v=vs.110% 29.aspx?f = 255& MSPPError = -2147217396

您可以双击表单设计器上的文本框控件,然后VS将放置 代码中的Changed事件的占位符代码,您可以在其中开始编码事件。

You can double-click the textbox control on the form designer,  and VS will put the  place holder code for the Changed event in the code for you where you can start coding the event.


这篇关于当用户输入文本框中的文本更改时,如何更新表单上的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆