Guest User

Untitled

a guest
Feb 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. if(textbox.text == "" ){
  2. MessageBox.Show("hacen falta campos por llenar")
  3. }
  4.  
  5. private void textbox_Validating(object sender,System.ComponentModel.CancelEventArgs e)
  6. {
  7. if(textbox.text == "" )
  8. {
  9. e.Cancel = true;
  10. textbox.Select(0, textBox1.Text.Length);
  11. errorProvider1.SetError (textBox1,"Debe introducir el nombre");
  12. }
  13. }
  14.  
  15. private void textBox1_Validated(object sender, System.EventArgs e)
  16. {
  17. errorProvider1.SetError(textbox, "");
  18. }
  19.  
  20. this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
  21.  
  22. private void buttonGuardar_Click(object sender, EventArgs e)
  23. {
  24. if (this.ValidateChildren(ValidationConstraints.Enabled))
  25. {
  26. //Todo es correcto, guardamos los datos
  27. }
  28. else
  29. {
  30. MessageBox.Show("Faltan algunos campos por rellenar");
  31. }
  32. }
  33.  
  34. public class MiTextBox : TextBox
  35. {
  36. public bool EsValido()
  37. {
  38. // El método IsNullOrWhiteSpace devuelve TRUE cuando el parametro string pasado es NULL o tiene una cadena de caracteres de espacios o vacía.
  39. // Usamos el operador ! para invertir el bool devuelvo.
  40. return !string.IsNullOrWhiteSpace(this.Text);
  41. }
  42. }
  43.  
  44. //Si EsValido devuelve false, mostrar el MessageBox
  45. if(!textbox.EsValido())
  46. {
  47. MessageBox.Show("hacen falta campos por llenar")
  48. }
  49.  
  50. bool validarTextBoxs()
  51. {
  52. foreach (Control item in this.Controls)
  53. {
  54. try
  55. {
  56. if (item is TextBox)
  57. {
  58. //Codigo comprobacion de textbox
  59. if (item.Text == "")
  60. {
  61. MessageBox.Show("Hay campos vacios");
  62. item.Focus();
  63. return false;
  64. }
  65. }
  66. else if (item is RichTextBox)
  67. {
  68. //codigo comprobacion de richtextbox
  69. if (item.Text == "")
  70. {
  71. MessageBox.Show("Hay campos vacios");
  72. item.Focus();
  73. return false;
  74. }
  75. }
  76. else if (item is ComboBox)
  77. {
  78. if (item.Text == "")
  79. {
  80. MessageBox.Show("Debes seleccionar un item");
  81. item.Focus();
  82. return false;
  83. }
  84. }
  85. }
  86. catch { }
  87. }
  88. return true;
  89. }
Add Comment
Please, Sign In to add comment