Another Class Issue

Hi,

Sending this data to a list box, every time I click submit it makes an index entry even if the data is invalid. When it finally is valid there are several entries before the valid one.

  {
            int IDNumber = 0;
            
            if(nameTextBox.Text == "")
            {
                MessageBox.Show("Invalid Name");
                return;
            }


            if (departmentTextBox.Text == "")
            {
                MessageBox.Show("Invalid Department");
                return;
            }


            if (positionTextBox.Text == "")
            {
                MessageBox.Show("Invalid Position");
                return;
            }

            if (int.TryParse(idNumberTextBox.Text, out IDNumber))
            {
                employee.IdNumber = IDNumber;
            }
            else
            {
                MessageBox.Show("Invalid ID Number");
                return;
            }



        }

 private void addEmployeeButton_Click_1(object sender, EventArgs e)
        {
            Employee myEmployee = new Employee();

            GetEmployeeData(myEmployee);

            employeeList.Add(myEmployee);

            employeeListBox.Items.Add(myEmployee.Name);

            nameTextBox.Clear();
            idNumberTextBox.Clear();
            departmentTextBox.Clear();
            positionTextBox.Clear();
        }

Open in new window

LVL 3
Computer GuyAsked:
Who is Participating?
 
plusone3055Commented:
well what you have above your button click method would be a good start but you have it outside of your button method so it's not stopping any of your invalid entries
0
 
plusone3055Commented:
you have nothing in your code that is stopping invalid entires
0
 
Computer GuyAuthor Commented:
How would I do that?
0
 
plusone3055Commented:
I'm about to call it for the evening in the morning I can post you a good method that would help you with this code
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.