private void OnTextChange(object sender, TextChangedEventArgs e) // hago mas comprobaciones al ingresar texto e impido ingresar texto incorrecto { design = new DesignControl(this); // vuelvo a iniciar la variable design porque cuando se produce el evento por primera vez que // es al cambiar el texto en xaml aun no se ha iniciado en esta clase TextBox textBox = sender as TextBox; if (textBox.Name == "txt_hex") { if (DesignControl.IsHex(textBox.Text)) { if (textBox.Text.Length == 9 && design.IsAlpha) { design.setColor(ColorTools.FromHex(textBox.Text)); } else if (textBox.Text.Length == 7 && !design.IsAlpha) { design.setColor(ColorTools.FromHex(textBox.Text)); } } else { txt_hex.Text = ColorTools.LastColor.ToHex(true); } } else if (textBox.Name != "txt_alpha") { if (!DesignControl.TextIsNumeric(textBox.Text) || string.IsNullOrWhiteSpace(textBox.Text)) { textBox.Text = "0"; } } else { if (DesignControl.TextIsNumeric(textBox.Text) && !string.IsNullOrWhiteSpace(textBox.Text)) { int number = Convert.ToInt32(textBox.Text); if (!DesignControl.IsPercent(number)) { textBox.Text = "0"; } } else { textBox.Text = "0"; } } }
private void OnTextChange(object sender, TextCompositionEventArgs e) // comprobaciones antes de que se escriba e impedir entradas incorrectas { TextBox textBox = sender as TextBox; // obtengo el textbox que produjo el evento string ActualText = textBox.Text + e.Text; if (textBox.Name == txt_hex.Name) // compruebo el texto que se escribe en el cuadro de texto de hex { if (DesignControl.IsHex(e.Text)) { if (e.Text == "#" && ActualText.Length > 1) { e.Handled = true; } } else { e.Handled = true; } } else // compruebo el texto en los otros cuadros de texto { if (DesignControl.TextIsNumeric(e.Text)) { int number = Convert.ToInt32(ActualText); if (textBox.Name != txt_alpha.Name) { e.Handled = !DesignControl.IsByte(number); } else { e.Handled = !DesignControl.IsPercent(number); } } else { e.Handled = true; } } }