1

What would be the best way of refactoring the individual button click events below? Is it possible. They are all doing the same thing, and changing each of them individually is a pain in the backside.

MainWindow.xaml.cs

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Button1_Add();
        }

        private void Button1_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button1.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            Button2_Add();
        }

        private void Button2_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button2.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            Button3_Add();
        }

        private void Button3_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button3.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            Button4_Add();
        }

        private void Button4_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button4.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button5_Click(object sender, RoutedEventArgs e)
        {
            Button5_Add();
        }

        private void Button5_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button5.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button6_Click(object sender, RoutedEventArgs e)
        {
            Button6_Add();
        }

        private void Button6_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button6.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button7_Click(object sender, RoutedEventArgs e)
        {
            Button7_Add();
        }

        private void Button7_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button7.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button8_Click(object sender, RoutedEventArgs e)
        {
            Button8_Add();
        }

        private void Button8_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button8.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button9_Click(object sender, RoutedEventArgs e)
        {
            Button9_Add();
        }

        private void Button9_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button9.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

        private void Button0_Click(object sender, RoutedEventArgs e)
        {
            Button0_Add();
        }

        private void Button0_Add()
        {
            var tb = Keyboard.FocusedElement as TextBox;
            try
            {
                var keypadObject = new Keypad();
                keypadObject.AppendValue(tb, Button0.Content.ToString());
            }
            catch (Exception)
            {
                TotalTextBox.Focus();
            }
        }

Keypad.cs

    public void AppendValue(TextBox tb, string valueToAppend)
    {
        if (tb.Text.Length < 6 && tb.Name != "RemainingTextBox")
        {
            if (tb.Text.IndexOf(".") == -1)
            {
                int beforeDotLength = tb.Text.Length;
                if (beforeDotLength < 3)
                {
                    tb.AppendText(valueToAppend);
                }
            }
            else if (tb.Text.IndexOf(".") > -1)
            {
                int dotPosition = tb.Text.IndexOf(".");
                int afterDotLength = tb.Text.Substring(dotPosition).Length;
                if (afterDotLength < 3)
                {
                    tb.AppendText(valueToAppend);
                }
            }
        }
    }
methuselah
  • 12,766
  • 47
  • 165
  • 315

1 Answers1

1

You can get currently clicked button from sender parameter, because it is the currently clicked button, just cast it to the (Button):

    private void Button_Add_Click(object sender, RoutedEventArgs e)
    {
        Button_Add((Button)sender);
    }

    private void Button_Add(Button button)
    {
        var tb = Keyboard.FocusedElement as TextBox;
        try
        {
            var keypadObject = new Keypad();
            keypadObject.AppendValue(tb, button.Content.ToString());
        }
        catch (Exception)
        {
            TotalTextBox.Focus();
        }
    }

And then use Button_Add_Click as event handler for all buttons.

P.S.: (Button)sender cast may fail if you use it in wrong(not Button) handler, so you should be prepared for such scenario (catch exception - notify user or at least log).
P.P.S: Taking into account that it is WPF application you may want to consider using MVVM design pattern. It has steep learning curve and may at first look too complex for minimal or no advantages, but it will allow you to create more robust and less coupled application.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53