C
C#17mo ago
Saetherin

❔ Button click is advancing control focus

I am writing a program which runs a test after the user clicks a button. The test takes a couple seconds, so I am disabling the button after it is clicked to prevent the user from firing multiple events. However, after the button is pressed, focus is being passed to the TextBox above it (next in tab order), causing the text to be selected which could allow for the user to accidentally change the IP. The desired behavior would be to have the button maintain focus the entire time, or at the very least prevent the TextBox from gaining focus while still being next in the tab order. What would be the best way to achieve this behavior? Current code flow is:
onButtonClick(){
button.Enabled = false;

//I have tried this to keep the TextBox from gaining focus, but it will still be highlighted/selected
form.Focus();
try
{
perform tests and IO operations here
}
catch
{
deal with exceptions
}
finally
{
button.Enabled = true;
button.Focus();
}
}
onButtonClick(){
button.Enabled = false;

//I have tried this to keep the TextBox from gaining focus, but it will still be highlighted/selected
form.Focus();
try
{
perform tests and IO operations here
}
catch
{
deal with exceptions
}
finally
{
button.Enabled = true;
button.Focus();
}
}
1 Reply
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?