Hey Everyone,
This is my second post so bare with me. I am trying to get some format exceptions to the following text boxes. It's starting to get on my nerves. I just hope I'm somewhat close to the solution but here is the code. I feel like I could do this with if-else statements or a do while statement. When I try and run the program it shows the messages but continues on and does not the user re-enter the text.

private void submit_btn_Click(object sender, EventArgs e)
        {
            while (anError != true)
            {

                if (vin_txt.Text.Length < 17)
                {

                    MessageBox.Show("A VIN has more Characters");
                    anError = true;
                    vin_txt.Clear();
                    break;
                   
                }


                if (vin_txt.Text == "")
                {
                    MessageBox.Show("You need to enter a VIN");

                }

                if (make_txt.Text == "")
                {
                    MessageBox.Show("You need to enter a vehicle make");

                }

                if (model_txt.Text == "")
                {
                    MessageBox.Show("You need to Enter a vehicle model");

                }

                if (year_txt.Text == "")
                {
                    MessageBox.Show("You need to enter a vehicle year");

                }

                if (odometer_txt.Text == "")
                {
                    MessageBox.Show("You need to enter an Odometer value");

                }

                if (Convert.ToInt32(odometer_txt.Text) > 300000)
                {
                    MessageBox.Show("The odometer value is too large");

                }

                if (wIP_rad.Checked == false)
                {
                    if (partCar_rad.Checked == false)
                    {
                        MessageBox.Show("You did not enter a Vehicle Status");

                    }
                }

                if (partCar_rad.Checked == false)
                {
                    if (wIP_rad.Checked == false)
                    {
                        MessageBox.Show("You did not enter a Vehicle Status");

                    }

                }

                if (price_txt.Text == null)
                {
                    MessageBox.Show("Please enter a price");

                }
            }
      
            if (this.partCar_rad.Checked)
            {
                aStatus = partCar_rad.Text;
            }

            if (this.wIP_rad.Checked)
            {
                aStatus = wIP_rad.Text;
            }
Attachments buyform.PNG 15.16 KB

Don't use loop. Terminate the method execution through return statement.

private void submit_btn_Click(object sender, EventArgs e)
        {
                if (vin_txt.Text.Length < 17)
                {

                    MessageBox.Show("A VIN has more Characters");
                    anError = true;
                    vin_txt.Clear();
                    return;
                   
                }


                if (vin_txt.Text == "")
                {
                    MessageBox.Show("You need to enter a VIN");
                    return;
                }
    ......

To expand on adataposts answer:

private void button1_Click(object sender, EventArgs e)
    {
      int iYear;
      string sModel;

      if (!int.TryParse(textBox1.Text, out iYear))
      {
        MessageBox.Show("The year must be numeric");
        return;
      }
      if ((iYear < 1950) || (iYear > DateTime.Today.Year))
      {
        MessageBox.Show("Invalid year, grandpa.");
        return;
      }

      sModel = textBox2.Text.Trim();
      if (string.IsNullOrEmpty(sModel))
      {
        MessageBox.Show("Model can't be empty....");
      }

      MessageBox.Show("OK! Record saved.");
      //Do your save stuff...

      textBox1.Clear();
      textBox2.Clear();
      checkBox1.Checked = false;
      
      //They are ready to enter another record
    }

Notice the int.TryParse . It will keep your code from blowing up if the input is non-numeric when calling Convert.ToInt32()

Comments
Exactly!
This article has been dead for over six months. Start a new discussion instead.