Okay so I am creating a windows form to basically act as a bank ledger for an individual. Pretty simple they must enter the account information, and a beginning balance. The key is the validation, it is really tripping me up. I need to ensure that upon clicking the continue button the textboxes are validated, the first should only be alphabetical components while the second two should only be numeric components. Additionally the last box must also be numeric only. Doubles are okay, it is currency after all. My code is below.

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

namespace WindowsFormsApplication1
{
    public partial class BankInfo : Form
    {
        double withdrawal;
        double deposit;
        double balance;
        double newTotal;

        public BankInfo()
        {
            InitializeComponent();
        }

        private void txtAcctBegBal_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int numberEntered = int.Parse(txtAcctBegBal.Text);
                if (numberEntered == 0)
                {
                    e.Cancel = true;
                    MessageBox.Show("Please enter an amount greater than 0.");
                }
            }
            catch (FormatException)
            {
                e.Cancel = true;
                MessageBox.Show("You need to enter an integer");
            }
        }

        private void btnAcctInfo_Click(object sender, EventArgs e)
        {
            grpAcctInfo.Visible = true;
            txtAcctName.Visible = true;
            txtAcctNum.Visible = true;
            txtAcctBegBal.Visible = true;
            lblAcctName.Visible = true;
            lblAcctNum.Visible = true;
            lblBeginning.Visible = true;
            btnCnt.Visible = true;
        }

        private void btnCnt_Click(object sender, EventArgs e)
        {
                txtAmt.Visible = true;
                btnWithDraw.Visible = true;
                btnDeposit.Visible = true;
                lblAvailable.Visible = true;
                lblBeginning.Visible = false;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            grpAcctInfo.Visible = false;
            txtAcctName.Visible = false;
            txtAcctNum.Visible = false;
            txtAcctBegBal.Visible = false;
            lblAcctName.Visible = false;
            lblAcctNum.Visible = false;
            lblBeginning.Visible = false;
            btnCnt.Visible = false;
            txtAmt.Visible = false;
            btnWithDraw.Visible = false;
            btnDeposit.Visible = false;
            lblAvailable.Visible = false;
            lblBeginning.Visible = false;
            txtAcctBegBal.Clear();
            txtAcctNum.Clear();
            txtAcctName.Clear();
        }

        private void btnWithDraw_Click(object sender, EventArgs e)
        {
            withdrawal = Convert.ToDouble(txtAmt.Text);
            balance = Convert.ToDouble(txtAcctBegBal.Text);
            newTotal = (balance - withdrawal);
            txtAcctBegBal.Text = System.Convert.ToString(newTotal);
        }

        private void btnDeposit_Click(object sender, EventArgs e)
        {
            withdrawal = Convert.ToDouble(txtAmt.Text);
            balance = Convert.ToDouble(txtAcctBegBal.Text);
            newTotal = (balance + withdrawal);
            txtAcctBegBal.Text = System.Convert.ToString(newTotal);
        }
    }
}

any help is appreciated.

Recommended Answers

All 7 Replies

You could consider using a Masked Textbox. And then setting each masked textbox accordingly

your currency mask would something like this

$0000.00

you can put however many zeros infront/behind. 0 means only numbers are allowed to be inputted.
and of course the period separates the dollars from the cents


Using masked textboxes makes things a lot easier/simpler

You could consider using a Masked Textbox. And then setting each masked textbox accordingly

your currency mask would something like this

$0000.00

you can put however many zeros infront/behind. 0 means only numbers are allowed to be inputted.
and of course the period separates the dollars from the cents


Using masked textboxes makes things a lot easier/simpler

This is super helpful. Only one problem, what is a masked text box? Sorry very new to C# still not sure of all the jargon.

Only one problem, what is a masked text box?

Here's a hint for the future: always try to research things like this on your own first. The only if you come up with nothing or fail to understand what you've found should you ask for clarification. In this case, "masked text box" is standard terminology, and when passed to Google will certainly tell you what you want to know in the first hits.

This is super helpful. Only one problem, what is a masked text box? Sorry very new to C# still not sure of all the jargon.

Well it is pretty much the same as a textbox, except that it allows the end user only to input data types which you specify. Which can be a combination of anything (numbers only, letters only, a mixture of the two).

I Haven't used it in ages, so I can't remember all the characters to use (in the mask), but google found this.

Masked TextBox Mask Characters

Awesome guys thanks! I got it working using the double.TryParse method, appreciate the help!

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.