CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2007
    Posts
    22

    Have a few quick questions about a program I'm writing

    Ok I have the general idea of what i want in this program, but I just need some help in some areas.

    For one thing I want to make sure in the email textbox that the user must enter text, a "@" and a "." then more text in that perticular order. As it is right now I used an if statement that checks that the characters are both there but does not make sure they are placed in that specific order


    here is a sample of the code i used

    Code:
    private void txtEmail_TextChanged(object sender, EventArgs e)
            {
                //We will use "contains to make sure the user enters
                // "@" and "." in their email address
    
                Email = txtEmail.Text.Trim();
                Email = txtEmail.Text;
                string CheckEmailChar1 = "@";
                string CheckEmailChar2 = ".";
    
                if (Email.Contains(CheckEmailChar1) && Email.Contains(CheckEmailChar2)) 
                {
                  
                }
                
                else
                {
                    MessageBox.Show("Please include @ and . in the email feild");
                }
    
            }

    I am aslo having a aproblem in the "Loan amount requested textbox and I'm not sure exactly why. The program runs but as soon as I input anything into that textbox, I end up with an error and the program stops running.

    I am a bit tired so I hope my post was still somewhat coherent, I will get back to this post later and try and get this fixed fairly soon. You will find the full program in the zip file attached.
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Have a few quick questions about a program I'm writing

    Validating email address is best with regex.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Have a few quick questions about a program I'm writing

    What is the error message you receive when typing in the textbox?
    It's not a bug, it's a feature!

  4. #4
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    It says FormatException was unhandled

    call stack

    > BankLoan.exe!BankLoan.Form1.txtIncome_TextChanged(object sender = {Text = "$5.00"}, System.EventArgs e = {System.EventArgs}) Line 273 C#
    [External Code]
    BankLoan.exe!BankLoan.Form1.txtLoanRequest_TextChanged(object sender = {Text = "5"}, System.EventArgs e = {System.EventArgs}) Line 282 + 0x2b bytes C#
    [External Code]
    BankLoan.exe!BankLoan.Program.Main() Line 18 + 0x1d bytes C#
    [External Code]

  5. #5
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Have a few quick questions about a program I'm writing

    Can you post the code in txtIncome_TextChanged and txtLoanRequest_TextChanged ?
    It's not a bug, it's a feature!

  6. #6
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    Code:
    private void txtLoanRequest_TextChanged(object sender, EventArgs e)
            {
                //convert the input into currency
                LoanAmtRequest = txtLoanRequest.Text;
                decimal finput = Convert.ToDecimal(LoanAmtRequest);
                txtIncome.Text = finput.ToString("c");
            }

  7. #7
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    Also can someone give me an example of how to use regular expressions here:

    Code:
     private void txtEmail_TextChanged(object sender, EventArgs e)
            {
                //We will use "contains to make sure the user enters
                // "@" and "." in their email address
    
                Email = txtEmail.Text.Trim();
                Email = txtEmail.Text;
                string CheckEmailChar1 = "@";
                string CheckEmailChar2 = ".";
    
                if (Email.Contains(CheckEmailChar1) && Email.Contains(CheckEmailChar2)) 
                {
                  
                }
                
                else
                {
                    MessageBox.Show("Please include @ and . in the email feild");
                }

  8. #8
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Have a few quick questions about a program I'm writing

    Quote Originally Posted by chipninja View Post
    Code:
    private void txtLoanRequest_TextChanged(object sender, EventArgs e)
            {
                //convert the input into currency
                LoanAmtRequest = txtLoanRequest.Text;
                decimal finput = Convert.ToDecimal(LoanAmtRequest);
                txtIncome.Text = finput.ToString("c");
            }
    Code:
    private void txtLoanRequest_TextChanged(object sender, EventArgs e)
    {
        //convert the input into currency
        LoanAmtRequest = txtLoanRequest.Text;
        decimal finput = 0;
        decimal.TryParse(LoanAmtRequest, out finput);
        txtIncome.Text = finput.ToString("c");
    }
    It's not a bug, it's a feature!

  9. #9
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    Also wouldn't regex only work on the web and not withing the program? Meaning Id have to use something else for my email validation?

  10. #10
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: Have a few quick questions about a program I'm writing

    I'm terrible at Regex as the pattern syntax is like Klingon to me, but I do know that it is in no way bound to only web usage.
    A Regex (in C#) is simply a class you can create like any other class in C#.

  11. #11
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    ohh ok thank you now i know. I'm still not sure how to use regex either. I looked at the webpage and it didnt make sense to me. I would need an example to understand it.

    Also. After I run the code i get an error here:

    Code:
     private string ProperCase(string name)
            {
                string firstcharacter = "";
                string restofstring = "";
                int namelength = 0;
                firstcharacter = name.Substring(0, 1);
                namelength = name.Length - 1;
    
                restofstring = name.Substring(1, namelength);
                firstcharacter = firstcharacter.ToUpper();
                restofstring = restofstring.ToLower();
                name = firstcharacter + restofstring;
                return name;
    
    
            }
    it says ArgumentOutof Range Exeption was unhandled

  12. #12
    Join Date
    Dec 2009
    Posts
    1

    Re: Have a few quick questions about a program I'm writing

    In regards to the "@" and "." issue, this is what I would do.

    Firstly put it in the txtEmail_Leave instead of the _Textchanged. This way it will check once the user has finished putting in the email address, opposed to checking after every character they type (which will being up the Message Box for exery single character). Next I would create two integers, index of '@' and index of '.' and check to see if the index of '@' is lower than that of '.'. Therefore checking if the @ is before the .

    private void txtEmail_Leave(object sender, EventArgs e)
    {
    //We will use "contains to make sure the user enters
    // "@" and "." in their email address

    Email = txtEmail.Text.Trim();
    Email = txtEmail.Text;
    string CheckEmailChar1 = "@";
    string CheckEmailChar2 = ".";

    if (Email.Contains(CheckEmailChar1) && Email.Contains(CheckEmailChar2))
    {
    int first = Email.IndexOf('@');
    int second = Email.IndexOf('.');
    if (first < second)
    {
    }
    else
    {
    MessageBox.Show("Please include @ and . in the email feild, in the corrct order");
    }
    }

    else
    {
    MessageBox.Show("Please include @ and . in the email feild");
    }
    }

  13. #13
    Join Date
    Dec 2007
    Posts
    22

    Re: Have a few quick questions about a program I'm writing

    I'm still getting runtime errors when i run the program. Its telling me FormatExeptionwa Unhandled here:


    Code:
    private void txtIncome_Leave(object sender, EventArgs e)
            {
                //convert the input into currency
                Income = txtIncome.Text;
                decimal sinput = Convert.ToDecimal(Income);
                txtIncome.Text = sinput.ToString("c");
            }

  14. #14
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Have a few quick questions about a program I'm writing

    Quote Originally Posted by chipninja View Post
    I'm still getting runtime errors when i run the program. Its telling me FormatExeptionwa Unhandled here:


    Code:
    private void txtIncome_Leave(object sender, EventArgs e)
            {
                //convert the input into currency
                Income = txtIncome.Text;
                decimal sinput = Convert.ToDecimal(Income);
                txtIncome.Text = sinput.ToString("c");
            }
    Look at my last post in this thread..
    It's not a bug, it's a feature!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured