Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want a button to disable for 30 second after click and enable it automatically

I am on a project and this project needs to take randomly 16 bytes from random.org. I can take it from website and write this 16 bytes to a textedit. I also have a refresh button. Everytime this button is clicked, new 16 byte comes and it is written in textedit.

I want to prevent the user to click refresh button sequentially. What I want is to disable refresh button after it is click. However, another thing I want is to enable this button after 30 seconds automatically.

I've tried thread.sleep(30000) in the button click event but it stops whole program for 30 seconds. I want to disable just refresh button for 30 seconds, not the rest of the program.

I am thinking to show to the users kind of stopwatch so that they can see how much time they should wait for next click. For example, after they click refresh button, there appears this 30 seconds stopwatch.

like image 414
Meriç Akgül Avatar asked Dec 21 '13 10:12

Meriç Akgül


1 Answers

If we are talking about WPF here, there is a way to achieve the desired behavior with EventTriggers without any code-behind:

 <Style TargetType="Button">

        <Style.Triggers>

            <EventTrigger RoutedEvent="Button.Click">

                <EventTrigger.Actions>

                    <BeginStoryboard>

                        <Storyboard>

                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled">

                                <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />

                                <DiscreteBooleanKeyFrame KeyTime="00:00:30" Value="True" />

                            </BooleanAnimationUsingKeyFrames>

                        </Storyboard>

                    </BeginStoryboard>

                </EventTrigger.Actions>

            </EventTrigger>

        </Style.Triggers>

    </Style>
like image 71
Ilya Avatar answered Sep 28 '22 07:09

Ilya