How to Make a Blinking Button in VB
Learning how to make a blinking button in your Visual Basic.NET (VB.NET) application can make your application more appealing to the user. You’re going to need to repeat a section of code to change the background color of your button and the best control to accomplish this is a timer control. A timer control will automatically execute a section of code depending on the value in the “Interval” property. You can use the “BackColor” property to change the color of the button and refresh the control by using the “Application.DoEvents” method.
Instructions
-
-
1
Launch Microsoft Visual Studio, click “New Project” from the left pane of your computer screen and expand “Visual Basic” below “Installed Templates.” Click “Windows” and double-click “Windows Forms Application” to create a new application.
-
2
Double-click “Button” from the Toolbox pane located on the left side of your computer screen to add a new button to your form.
-
-
3
Double-click “Timer” to add a new timer control. Right-click the timer control and click “Properties.” Set the “Enabled” property to “True” and set the “Interval” property to “500.”
-
4
Double-click the timer control to create a tick event and add the following line of code to create a global variable above “Timer1_Tick:”
Dim blink As Boolean
-
5
Copy and paste the following code inside the “Timer1_Tick” event to make the button blink by changing the background back and forth from orange to red:
If (blink) Then
Me.Button1.BackColor = Color.Red
blink = False
Application.DoEvents()
Else
Me.Button1.BackColor = Color.Orange
blink = True
Application.DoEvents()
End If -
6
Press “F5” to run the program and see the blinking button.
-
1
References
- Photo Credit Comstock Images/Comstock/Getty Images