Reputation: 13206
I have a application with 4 textbox controls: TotalTextBox
, PaidTextBox
, RemainingTextBox
and DriverTextBox
.
How do I pass through the current textbox in focus by the user (only if it is not DriverTextBox
, i.e. only from TotalTextBox
, PaidTextBox
, RemainingTextBox
)?
On program launch it should focus on the TotalTextBox control. I have also implemented a try/catch so that it only defaults back to focusing on the TotalTextBox control.
This is my code so far:
MainWindow.Xaml.cs (where tb is the Textbox in focus I want to pass through)
public MainWindow()
{
InitializeComponent();
TotalTextBox.Focus();
// Setup keypad
bool dotControl = false;
int count = 0;
}
private void RemoveLastButton_Click(object sender, RoutedEventArgs e)
{
try
{
var keypadObject = new Keypad();
keypadObject.RemoveLast(TextBox tb, bool dotControl, int count);
}
catch (Exception)
{
TotalTextBox.Focus();
}
}
Keypad.cs
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);
}
}
Upvotes: 0
Views: 33
Reputation: 11319
To get the currently focused textbox, you can do something like this:
TextBox textbox = Keyboard.FocusedElement as TextBox;
If textbox
ends up being null
, then it's not a textbox that is currently focused.
To complete the answer, you can filter what you want by simple comparison to your known textboxes.
if (textbox != null && textbox != DriverTextBox)
{
var keypadObject = new Keypad();
keypadObject.RemoveLast(textbox, dotControl, count);
}
Upvotes: 2