Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:35:48+00:00 2026-06-14T18:35:48+00:00

Hey so I have the following code which should throw up the errors if

  • 0

Hey so I have the following code which should throw up the errors if the text boxes are empty but it doesn’t it just carries on with what it would do were they not and adds an item with 0 or whatever to the list instead, is there a problem with my code?

private void BtnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            theVisit.name = txtName.Text;
            theVisit.address = txtAddress.Text;
            theVisit.arrival = DateTime.Parse(txtArrival.Text);
            //Update theVisit object to reflect any changes made by the user

            this.Hide();
            //Hide the form
        }
        catch (Exception)
        {
            if (txtName.Text == "")
                MessageBox.Show("please enter a customer name");

            if(txtAddress.Text == "") 
                MessageBox.Show("Please enter a customer address");

            if(txtArrival.Text == "")
                MessageBox.Show("Please enter an arrival time");
        }

NEW

if (txtName.Text == "" || txtAddress.Text == "" || txtArrival.Text == "")
            MessageBox.Show(" Please enter a value into all boxes");
        else
        theVisit.name = txtName.Text;
        theVisit.address = txtAddress.Text;
        theVisit.arrival = DateTime.Parse(txtArrival.Text);
        //Update theVisit object to reflect any changes made by the user
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:35:50+00:00Added an answer on June 14, 2026 at 6:35 pm

    The try-catch-statement is used to catch and handle exceptions. An exception can be thrown, if an index is out of bounds, if members of a variable set to null are accessed, and in many other situations. A TextBox being empty is not an error by itself and does not throw an exception.

    I suggest you to use a completely different approach. Add an ErrorProvider to your form. You find it in the toolbox in the section “Components”. Now you can add the following code to your form:

    private HashSet<Control> errorControls = new HashSet<Control>();
    
    private void ValidateTextBox(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox.Text == "") {
            errorProvider1.SetError(textBox, (string)textBox.Tag);
            errorControls.Add(textBox);
        } else {
            errorProvider1.SetError(textBox, null);
            errorControls.Remove(textBox);
        }
        btnAdd.Enabled = errorControls.Count == 0;
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        txtName.Tag = "Please enter a customer name";
        txtAddress.Tag = "Please enter a customer address";
        errorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink;
    
        ValidateTextBox(txtName, EventArgs.Empty);
        ValidateTextBox(txtAddress, EventArgs.Empty);
    }
    

    Select the ValidateTextBox method as error handler for the TextChanged event of all your textboxes. Insert the desired message in the Tag property of the textboxes. Set the BlinkStyle property of the ErrorProvider to ErrorBlinkStyle.NeverBlink. You can do these settings in code or in the form designer.

    Now a red error symbol will appear next to empty textboxes. If you hoover the mouse over them, a tooltip with the error message will appear.


    UPDATE

    I updated the code above to automatically disable or enable the “Add”-button. Therefore I added a HashSet that contains all the controls currently being in an error state. If the set is empty the button is enabled, otherwise disabled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey community I have the following ServerSocket which should listen to port 53000 and
Hey all, basically i have an empty AS3 fla file with just the following
Hey I have the following code which I'm using to isolate the a b
Hey guy i have following problem: I have this line of code which includes
Hey. I have the following code: final String text = (String) lt.getItemAtPosition(position); db.removeCategory(text); What
Hey guys I have the following code working which is sending a GET Request
Hey I have the following code which checks if the file already exists in
I have written code such as the following, which by assumption shouldn't compile. I'm
Hey so I have all my code working and it's fine. But I'd like
Hey, if you have got the following code and want to check if $key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.