954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Invalid or Valid ... that is the question

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.

behemothdave
Light Poster
29 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Char.Number and various other Char static members might be worth looking into. Also, if you want to get deeper into string parsing, look into regular expressions .

A few double.tryparse ()'s would do the trick here too.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

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

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

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.

behemothdave
Light Poster
29 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

behemothdave
Light Poster
29 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: