Home > Software design >  Taking Value From Text Box Into Total (VS - C#)
Taking Value From Text Box Into Total (VS - C#)

Time:06-10

I am new to the C# scene, so don't really have much knowledge - This is my first project.

I am looking to create a very basic calorie counter, which eventually will include other functions.

Here's what I have so far;

enter image description here

I want to know how to take the value from the text box on the click of the 'Add' button - Which adds to the total value (bottom right)

I'm looking for any tips/videos to help so anything is appreciated.

TIA

CodePudding user response:

I made a simple implementation for you, you could refer to it:

enter image description here

using System;
using System.Windows.Forms;

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

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            bool Flag = Int32.TryParse(textBox1.Text, out int result);//Determine if it is a number
            if (textBox1.Text == "")//If it is null, falg takes true and skips the next judgment
            { Flag = true; }
            else if (!Flag)
            {
                MessageBox.Show("Input Error");
                textBox1.Text = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Int32.TryParse(textBox1.Text, out int result);
            int total =result   Convert.ToInt32(label3.Text);
            label3.Text = total.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label3.Text = "0";
        }
    }
}

enter image description here

  • Related