Member Avatar for JustLacksZazz

Hello everyone,

I'm quite new to coding and can't seem to find an answer to this question online. So far I have created a multiple choice quiz and am now working on the nitpicky details.

Right now the quiz works great but I'm just trying to add a function where the button flashes green once for about 250 milliseconds and then goes back to the original color; this would happen each time the button is pressed. This would give the subject an indication of their selection - I know it's a bit redundant but this is what my boss wants (for the record this isn't my main job but just a fun side-project to try and learn some coding). I can't seem to figure out how to make the button flash briefly without staying at that color indefinitely or not flashing at all. In addition, it would be helpful if the button is disabled during this color flash so the patients can't click through the quiz incredibly fast (I believe my code should work for that). Please see my code for the button_click event below. If anyone could help me out it would be greatly appreciated!

(Some extra background):
I have created a quiz for depression. The patient is presented with an unchanging label "In the past TWO WEEKS INCLUDING TODAY..." and then 4 buttons with selections that switch with each button press "0. I do not feel sad", "1. I feel sad much of the time", "2. I am sad all of the time", "3. I am so sad I can barely stand it" (these would then switch to a new topic like sleep or energy). This is an array that changes with each button press.

Please let me know if you need any additional details (I'm always unsure). Cheers!

MY CODE:

Private Sub Button_Opt0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Opt0.Click

answers(quesNum - 1) = Button_Opt0.Text

If quesNum < 23 Then 

    quesNum += 1
    Button_Opt0.Text = questions(quesNum -1, 0)
    Button_Opt1.Text = questions(quesNum -1, 1)
    Button_Opt2.Text = questions(quesNum -1, 2)
    Button_Opt3.Text = questions(quesNum -1, 3)


    '-------What I'm interested in:

    Button_Opt0.BackColor = Color.Green
    System.Threading.Thread.Sleep(250)
    Button_Opt0.BackColor = Color.Empty   'I want the button to return to the original color...

    '----I don't want the button to be clickable during this color flash:

        If Button_Opt0.BackColor = Color.AliceBlue Then
            Button_Opt0.Enabled = False
        End If




Else

    MarkTest()

End If

End Sub

Recommended Answers

All 5 Replies

Oh boy blinky buttons! : )

Give this button a try:

Public Class FlashyButton
   Inherits Button
   Private WithEvents FlashTimer As New Timer

   Private _AlternateBackColor As Color = Color.Green
   Public Property AlternateBackColor() As Color
      Get
         Return _AlternateBackColor
      End Get
      Set(ByVal value As Color)
         _AlternateBackColor = value
      End Set
   End Property 'AlternateBackColor


   Private _FlashDuration As Int32 = 1000
   Public Property FlashDuration() As Int32
      Get
         Return _FlashDuration
      End Get
      Set(ByVal value As Int32)
         _FlashDuration = value
      End Set
   End Property 'FlashDuration

   Private _FlashEnabled As Boolean = True
   Public Property FlashEnabled() As Boolean
      Get
         Return _FlashEnabled
      End Get
      Set(ByVal value As Boolean)
         _FlashEnabled = value
      End Set
   End Property 'FlashEnabled

   Private ElapsedTime As Int32
   Private OrigBackColor As Color
   Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
      If FlashEnabled Then
         Enabled = False
         FlashTimer.Interval = 75
         ElapsedTime = 0
         OrigBackColor = Me.BackColor
         FlashTimer.Start()
      End If
      MyBase.OnClick(e)
   End Sub

   Private AltColorSet As Boolean

   Private Sub FlashTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FlashTimer.Tick
      ElapsedTime += FlashTimer.Interval
      If ElapsedTime >= FlashDuration Then
         BackColor = OrigBackColor
         FlashTimer.Stop()
         Enabled = True
         Exit Sub
      Else
         If BackColor = OrigBackColor Then
            BackColor = AlternateBackColor
         Else
            BackColor = OrigBackColor
         End If
      End If
   End Sub
End Class

Just add to your project code as a new class and rebuild the project. You should then see it at the top of your toolbox.

Member Avatar for JustLacksZazz

I will certainly give that a try tomorrow at work! Thanks very much for your reply, I really appreciate it :D

Member Avatar for JustLacksZazz

It worked great. I will go ahead and replace the necessary buttons with my "Blinky Buttons". Thank you for your help!

You are welcome.

Your mentioned patients. I hope none of these are susceptible to photosensitive epilepsy.

Member Avatar for JustLacksZazz

I hope not either! I did change the code slightly so it was more of a brief highlight vs. multiple flash. That should solve the epilepsy issue!!! ;) Either way, it was just what I was looking for so thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.