Button colour change through system time

SOUBHIK BISWAS 21 Reputation points
2021-07-17T11:08:54.503+00:00

In Vb.net I have 34 different Button that button colour have to change in red with set time (it's should continue run ,if software close also it should not off means software reopen that button colour should be red ) and duration time ...
Is it possible??
Please help me.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,723 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,366 Reputation points
    2021-07-19T02:15:49.337+00:00

    Hi @SOUBHIK BISWAS ,
    Right click your project -> 'Properties' -> 'Resources' -> then save the set time as string.
    For Example:
    115731-screenshot-2021-07-19-101259.png
    Finally, you can refer to the following code to set Button colour.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            If Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")) > Convert.ToDateTime(My.Resources.MyDateTime) Then  
                BackgroundWorker1.RunWorkerAsync()  
            End If  
        End Sub  
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork  
            Button1.BackColor = Color.Red  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,421 Reputation points
    2021-07-19T13:27:03.383+00:00

    Hello,

    Full source

    • Create a setting under project properties for storing the date (or store it in a text file), the code below uses MySettings.
    • Use code below which checks today date against the date under MySettings. If met set one button's back color and then set the others via a list.

    Code

        Public Class Form1
            Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
                CheckDateCondition()
            End Sub
    
            Private Sub CheckDateCondition()
                If Now >= My.Settings.DateSetting Then
    
                    Dim buttonList = Controls.
                            OfType(Of Button).
                            Where(Function(but) but.Name <> "Button1").
                            ToList()
    
                    Button1.BackColor = Color.Red
    
                    For Each button As Button In buttonList
    
                        button.DataBindings.Add(
                            "BackColor",
                            Button1,
                            "BackColor")
    
                    Next
                End If
            End Sub
        End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.