Ejemplo n.º 1
0
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
            bool isNumeric       = (e.Key >= Key.D0 && e.Key <= Key.D9);

            Key[] keysToFilter = { Key.Delete, Key.Back, Key.Left, Key.Right, Key.Tab };
            if (!isNumeric && !isNumPadNumeric && !keysToFilter.Contains(e.Key))
            {
                e.Handled = true;
                return;
            }

            if ((e.Key == Key.Right && (sender as TextBox).CaretIndex == (sender as TextBox).Text.Length))
            {
                if (sender == Field1)
                {
                    Field2.Focus();
                }
                else if (sender == Field2)
                {
                    Field3.Focus();
                }
                else if (sender == Field3)
                {
                    Field4.Focus();
                }

                e.Handled = true;
            }
            else if (e.Key == Key.Left && (sender as TextBox).CaretIndex == 0)
            {
                if (sender == Field4)
                {
                    Field3.Focus();
                }
                else if (sender == Field3)
                {
                    Field2.Focus();
                }
                else if (sender == Field2)
                {
                    Field1.Focus();
                }

                e.Handled = true;
            }
            else if (e.Key == Key.OemMinus)
            {
                e.Handled = true;
            }
        }
Ejemplo n.º 2
0
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tbx = sender as TextBox;

            byte value = 0;

            if (!string.IsNullOrEmpty(tbx.Text) && !Byte.TryParse(tbx.Text, out value))
            {
                tbx.Text = "255";

                if (sender == Field1)
                {
                    Field2.Focus();
                }
                else if (sender == Field2)
                {
                    Field3.Focus();
                }
                else if (sender == Field3)
                {
                    Field4.Focus();
                }
            }
            else if (tbx.Text.Length == 3)
            {
                if (sender == Field1)
                {
                    Field2.Focus();
                }
                else if (sender == Field2)
                {
                    Field3.Focus();
                }
                else if (sender == Field3)
                {
                    Field4.Focus();
                }
            }

            SetValue(ValueProperty, string.Format("{0}.{1}.{2}.{3}", Field1.Text, Field2.Text, Field3.Text, Field4.Text));
        }