Given a WPF form with a Button (Button1) set to default and a TextBox (TextBox1)
I would like to disable the button as soon as it is clicked, and then execute some long running code.
Normal WPF behaviour would not refresh the disabled button straight away, rather the interface will wait for the code in the button click handler to finish executing and then disable the button for a split second and re-enable it.
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
‘disable the button
Button1.IsEnabled = False
‘Now perform a long running code
‘re-enable the button
Button1.IsEnabled = True
Of all the solutions I have seen posted on the internet, majority of them involve forcing the Dispatcher to update the UI before running the long running code. This works for most scenarios, however if the focus is on the TextBox, and the button click event is executed by clicking the ‘Enter’ button on your keyboard (Button is default button on form), the solution mentioned earlier would fail almost all of the time.
Does anyone out there have a solution to this problem where if I was to execute the button click event by pressing Enter on keyboard whilst the focus was on the TextBox, the button would disable straight away before executing the sleep?