Pregunta

Estoy buscando una mejor manera de verificar que ningún campo se deja en blanco en una forma, en la actualidad este es mi aplicación, si tiene una, tha nota uno mejor será recibido estoy usando KryptonControls.

private bool verify(Control c)
{
    switch (c.GetType().Name)
    {
        case "KryptonTextBox":
            {
                if (((KryptonTextBox)c).Text == "")
                {
                    ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Red;
                    ((KryptonTextBox)c).GotFocus += new EventHandler(ControlGotFocus);
                    return false;
                }
            }
            break;
        case "KryptonComboBox":
            {
                if (((KryptonComboBox)c).SelectedIndex < 0)
                {
                    ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.Red;
                    ((KryptonComboBox)c).GotFocus += new EventHandler(ControlGotFocus);
                    return false;
                }
            }
            break;
        case "KryptonDataGridView":
            {
                if (((KryptonDataGridView)c).Rows.Count <= 0)
                {
                    ((KryptonDataGridView)c).StateCommon.HeaderColumn.Border.Color1 = Color.Red;
                    ((KryptonDataGridView)c).GotFocus += new EventHandler(ControlGotFocus);
                    return false;
                }
            }
            break;
        default:    
            break;
    }

    if (c.Controls.Count > 0)
    {
        foreach (Control cc in c.Controls)
        {
            if (!verify(cc))
            {
                return false;
            }
        }
    }
    return true;
}

Así que cuando el usuario establece el foco a un control que se tiene que verificar el código se ejecuta:

void ControlGotFocus(object sender, EventArgs e)
{
    switch (sender.GetType().Name)
    {
        case "KryptonTextBox":
            {
                ((KryptonTextBox)sender).StateCommon.Border.Color1 = Color.Gray;
            }
            break;
        case "KryptonComboBox":
            {
                ((KryptonComboBox)sender).StateCommon.ComboBox.Border.Color1 = Color.Gray;
            }
            break;
        case "KryptonDataGridView":
            {
                ((KryptonDataGridView)sender).StateCommon.HeaderColumn.Border.Color1 = Color.Black;
            }
            break;
        default:
            break;
    }
}
¿Fue útil?

Solución

Es posible optimizar el código algo como esto:

switch (c.GetType().Name)  {  case "KryptonTextBox": }

a:

TextBox tb = c as TextBox;
if (tb != null)
    return string.IsNullOrEmpty(tb.Text);

ComboBox cb = c as ComboBox;
if (cb != null)
   return cb.SelectedIndex < 0;

etc.

Pero sugiero utilizar validadores para estos puproses.

Otros consejos

Me recomiendan la clase de validación escrito por Deborah Kurata, http://msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx Esto realmente me ayudó a validar un formulario con una gran cantidad de cuadros de texto.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top