none
How can I update text box's on my form when text changes in the user input text box's

    Question

  • 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

    This code I have so far:

    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();        
            }
        }
    }


    • Moved by CoolDadTx Thursday, February 01, 2018 3:40 PM Winforms related
    Tuesday, January 30, 2018 9:27 PM

All replies

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

    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.

    Tuesday, January 30, 2018 9:56 PM
  • Oh one other thing, if you right-click a control on the form in the designer, go to Properties and look at a the top of Properties, you should see the lighting bolt and if you click it, it will show all the events for a given control where you can select an event and VS will put the place holder in the code, wire it up for you, so that you can place your code in the event. 
    Tuesday, January 30, 2018 10:01 PM
  • Hi Stonehaven,

    For your question, when you use TextChanged event, the count of input value from textBox will effect the number of trigger events.

    Try to use KeyDown event. When input all the four values and print an Enter in TextBox4, it will do what you want.

    Here is a simple example for your reference.

     private void Form1_Load(object sender, EventArgs e)
            {
                textBox4.KeyDown += TextBox4_KeyDown;
            }
    
            private void TextBox4_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
                    {
                       //do something
                    }
                }
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 01, 2018 6:07 AM
  • Thanks for suggestions.

    Took a moment to get it working but it seems this only updates (//do something) after typing in only one of the text boxes and it only updates when enter is pressed. Id prefer not to use an event for a specific text box but maybe an event that detects text changed in any of the text boxes on the form

    What im looking for is; if the user types in any of the user input text boxes it will apply the update (//do something) as the user types.

    Its important to note that the code i posted here for this project is used to calculate ONLY ONE SKILL and there are 4 user input text boxes for this one skill, 2 for each "Ability" and 2 for each "Skills". I still need to add 5 more Abilities and 67 more skills

    This will be a total of 148 user input text boxes.

    Is there not an event that i can use to (//do something) when it detects a change in ANY of the Forms textbox controls?

    Tuesday, February 06, 2018 5:20 PM