Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(textbox.text == "" ){
- MessageBox.Show("hacen falta campos por llenar")
- }
- private void textbox_Validating(object sender,System.ComponentModel.CancelEventArgs e)
- {
- if(textbox.text == "" )
- {
- e.Cancel = true;
- textbox.Select(0, textBox1.Text.Length);
- errorProvider1.SetError (textBox1,"Debe introducir el nombre");
- }
- }
- private void textBox1_Validated(object sender, System.EventArgs e)
- {
- errorProvider1.SetError(textbox, "");
- }
- this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
- private void buttonGuardar_Click(object sender, EventArgs e)
- {
- if (this.ValidateChildren(ValidationConstraints.Enabled))
- {
- //Todo es correcto, guardamos los datos
- }
- else
- {
- MessageBox.Show("Faltan algunos campos por rellenar");
- }
- }
- public class MiTextBox : TextBox
- {
- public bool EsValido()
- {
- // El método IsNullOrWhiteSpace devuelve TRUE cuando el parametro string pasado es NULL o tiene una cadena de caracteres de espacios o vacía.
- // Usamos el operador ! para invertir el bool devuelvo.
- return !string.IsNullOrWhiteSpace(this.Text);
- }
- }
- //Si EsValido devuelve false, mostrar el MessageBox
- if(!textbox.EsValido())
- {
- MessageBox.Show("hacen falta campos por llenar")
- }
- bool validarTextBoxs()
- {
- foreach (Control item in this.Controls)
- {
- try
- {
- if (item is TextBox)
- {
- //Codigo comprobacion de textbox
- if (item.Text == "")
- {
- MessageBox.Show("Hay campos vacios");
- item.Focus();
- return false;
- }
- }
- else if (item is RichTextBox)
- {
- //codigo comprobacion de richtextbox
- if (item.Text == "")
- {
- MessageBox.Show("Hay campos vacios");
- item.Focus();
- return false;
- }
- }
- else if (item is ComboBox)
- {
- if (item.Text == "")
- {
- MessageBox.Show("Debes seleccionar un item");
- item.Focus();
- return false;
- }
- }
- }
- catch { }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment