Reputation: 13216
Is it possible to overload an event handler with parameters? If not, what is the best way of passing through an additional 3 parameters?
I keep getting the error message:
Here is a picture of what I am trying to achieve:
MainWindow.xaml.cs
public bool DotControl { get; set; }
public int Count { get; set; }
TextBox Tb = Keyboard.FocusedElement as TextBox;
private void RemoveLastButton_Click(object sender, RoutedEventArgs e, bool DotControl, int Count, TextBox tb)
{
if (Tb != null && Tb != DriverTextBox)
{
try
{
var keypadObject = new Keypad();
keypadObject.RemoveLast(Tb, DotControl, Count);
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
else
{
TotalTextBox.Focus();
}
}
private void ResetButton_Click(object sender, RoutedEventArgs e, bool DotControl, int Count, TextBox tb)
{
if (Tb != null && Tb != DriverTextBox)
{
try
{
var keypadObject = new Keypad();
keypadObject.Reset(Tb, DotControl, Count);
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
else
{
TotalTextBox.Focus();
}
}
Keypad.cs
// Backspace textbox
public void RemoveLast(TextBox tb, bool dotControl, int count)
{
if (tb.Text.Length > 0)
{
if (char.IsDigit(tb.Text[tb.Text.Length - 1])) count = 0;
else
{
dotControl = false;
count = 0;
}
tb.Text = tb.Text.Remove(tb.Text.Length - 1, 1);
}
}
// Clear textbox
public void Reset(TextBox tb, bool dotControl, int count)
{
dotControl = false;
count = 0;
tb.Text = "";
}
Upvotes: 0
Views: 569
Reputation: 6415
Event handlers have specific signatures, so call your custom method from the event handler. For example:
private void RemoveLastButton_Click(object sender, RoutedEventArgs e)
{
RemoveLastButton(DotControl, Count, tb)
}
private void RemoveLastButton(bool DotControl, int Count, TextBox tb)
{
if (Tb != null && Tb != DriverTextBox)
{
try
{
var keypadObject = new Keypad();
keypadObject.RemoveLast(Tb, DotControl, Count);
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
else
{
TotalTextBox.Focus();
}
}
BUT from the example you have you don't even need to be passing those parameters (DotControl, tb, Count
) to the new RemoveLastButton
method because they are properties of your class anyway so accessible. Which means you don't even need to break out into a new method like this and can just put that code in the event handler as you had it, but without changing the signature of the event handler.
Upvotes: 3