title: errorprovider or message box?
body: <p>I have a simplr windows form input(student name,studentid) to display to a listbox.</p>
  
  <p>I need to check for duplicate student id in listbox before i add a value.
  Can I use error provider on student id text box to do this?
  any help much appreciated</p>
  
  <p>please check last bit of my code below -is for/foreach loop required?
  Thanks</p>
  
  <pre><code>private void txtSid_Validating(object sender, CancelEventArgs e)
      {
          bool can = false;
          int sid = 0;
  
  
          if (string.IsNullOrEmpty(txtSid.Text))
          {
  
              ep1.SetError(txtSid, "Please Enter Student ID");
              can = true;
          }
          else if (!int.TryParse(txtSid.Text, out sid))
          {
              ep1.SetError(txtSid, "Student ID must be a number");
              can = true;
          }
          else
           for (int i = 0; i &lt; lstDisplay.Items.Count; i++)
          {
  
              if (lstDisplay.Items[i].ToString().Contains(txtSid.Text))
              {
               ep1.SetError(txtSid, "Student ID already added");
              can = true;
              }
          {
                          }
  
          e.Cancel = can;
      }
  </code></pre>