Need help in getting the value inputed in the text box to work for if-else.
I also program that the textbox will only alow numeric characters.

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 OrderingSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            hidemenu();
        }

        private void hidemenu()
        {
            panelpayment.Visible = false;
        }
        private void showmenu(Panel subMenu)
        {
            if (subMenu.Visible == false)
            {
                hidemenu();
                subMenu.Visible = true;
                btnAdd1.Enabled = false;
                btnAdd2.Enabled = false;
                btnAdd3.Enabled = false;
                btnAdd4.Enabled = false;
                btnAdd5.Enabled = false;
                btnAdd6.Enabled = false;
                btnAdd7.Enabled = false;
                btnAdd8.Enabled = false;
            }
            else
                subMenu.Visible = false;
        }

        public void totalPrice()
        {
            int sum = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
            }
            lblTotal.Text = "₱" + sum.ToString();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }     
        private void btnAdd1_Click(object sender, EventArgs e)
        {
            int a = 75;
            decimal b = numq1.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "1pcs ChickenJoy", price);
            totalPrice();
        }
        private void btnAdd2_Click(object sender, EventArgs e)
        {
            int a = 50;
            decimal b = numq2.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "1pcs Burger Stake", price);
            totalPrice();
        }

        private void btnAdd3_Click(object sender, EventArgs e)
        {
            int a = 33;
            decimal b = numq3.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "1pcs Burger", price);
            totalPrice();
        }

        private void btnAdd4_Click(object sender, EventArgs e)
        {
            int a = 40;
            decimal b = numq4.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "1pc Jolly Crispy Fries", price);
            totalPrice();
        }

        private void btnAdd5_Click(object sender, EventArgs e)
        {
            int a = 30;
            decimal b = numq5.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "Peach Mego Pie", price);
            totalPrice();
        }

        private void btnAdd6_Click(object sender, EventArgs e)
        {
            int a = 27;
            decimal b = numq6.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "Coke Float", price);
            totalPrice();
        }

        private void btnAdd7_Click(object sender, EventArgs e)
        {
            int a = 53;
            decimal b = numq7.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "Jolly Spagetti", price);
            totalPrice();
        }

        private void btnAdd8_Click(object sender, EventArgs e)
        {
            int a = 29;
            decimal b = numq8.Value;

            decimal price;
            price = a * b;
            dataGridView1.Rows.Add(b, "Sundae", price);
            totalPrice();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string cname = dataGridView1.Columns[e.ColumnIndex].Name;
            if (cname == "delete")
            {
                int rowIndex = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows.RemoveAt(rowIndex);
                totalPrice();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showmenu(panelpayment);

        }

        private void confirmpayment_Click(object sender, EventArgs e)
        {
            // button to check the payment
            // textbox to if loop ????
            bool money = bool.Parse(textBox1.Text);
            if (money > lblTotal.Text)

        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }
        }
    }
}

Recommended Answers

All 3 Replies

ops
I ment c#

I don't really understand your question though. But if it is what am thinking, you can try this or elaborate your question for good response.

if (TextBox1.Text == "expression")
{
//Code to execute
}

As for the numeric check, you can try

  if (!IsNumeric(TextBox1.Text))
{
        MessageBox.Show("Only numeric value is allowed");
}
    else
    {
    }

Check [url]https://www.emmason247.com.ng/tutorial/c--if-statement-if-else-statement-if-else-if-else-statement--nested-if-else-statement/RGRIVGIGb[/url] for more details on c# if statement

Having a textbox that only accpets numbers is actually fairly common. One way to handle this is to use the NumericUpdown control. This does what most people need but the up/down arrows are hard coded and as far as I know can't easily be removed.

A pattern I like to follow is to use the Validating event that will allow one to have code that checks that the text is valid for a specific purpose. To that end I designed a simple IntegerTextBox that won't let the user leave the textbox until a valid integer is entered or the textbox is empty. Simply add this code to the Form1.cs file and rebuild it. Now it is ready to drop onto your form.

public class ValidatingTextBox : TextBox
{

    public ValidatingTextBox() : base()
    {
        this.Validating += this_Validating;
    }
    void this_Validating(object sender, CancelEventArgs e)
    {
        int temp = 0;
        if (!string.IsNullOrEmpty(Text) && !int.TryParse(Text, out temp))
        {
            Text = "";
            e.Cancel = false;
            return;
        }
        e.Cancel = true;
    }
    public override string ToString()
    {
        return Text;
    }
}

I've included a ToString() override that returns the Text value. I find this a little more intuitive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.