Click here to Skip to main content
16,006,594 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Alright, so I have created my own version of a calendar, but I am not sure how to get it to highlight the today's current day of the month.. The code I have used (that should work) only highlights the first day of the month or the last.. It is not highlighting the CURRENT DAY for some reason.. I have it search to find the today label and then it's SUPPOSED to highlight it. Well, below is some code to maybe help narrow this down. Thank you all in advance!
VB
Private dtNow As Date = Now()
Private Istoday As Boolean = False


    Private Sub SetLabelBackColor(ByRef LabelName As String)
        Try
            For Each itm As Control In EnumerateAllControls(Me)
                If itm.Name.StartsWith("Pnl") Then
                    For Each c As Control In itm.Controls
                        If TypeOf c Is Label Then
                            If c.Name.StartsWith("lbl") Then
                                If c.Name Is LabelName Then
                                    If dtNow.Month = StartMonth Then
                                        c.BackColor = Color.Yellow       
                                    End If
                                Else : c.BackColor = Color.OldLace       
                                End If
                            End If
                        End If
                    Next
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
Posted
Updated 9-Aug-11 8:18am
v2
Comments
Herman<T>.Instance 9-Aug-11 16:39pm    
where is StartMonth set in your code?
NY Andrew 9-Aug-11 23:59pm    
Sorry, I forgot to post that..
Sergey Alexandrovich Kryukov 9-Aug-11 21:04pm    
As it is not shown where you get the current date, there is nothing which helps to find a problem. However, this code already shows bad sign -- please see my answer.
--SA

From the first glance one can see the code is extremely dirty and not maintainable. First that catches the eye is the use if immediate string constants hard-codes in this fragment. You rely on such an unreliable criteria as the control name, which is only intended to be used to support Designer. (Note: control name has nothing to do with the variable name.) And why do you think you have to find some control, by name or not? No wonder that your code in "not working". You should put a breakpoint on the point where you expect the color change and see of the code gets there. (Did you do it before asking your question? Do you use debugger at all?)

Work with controls, not with there names! Do not rely on the contents of any strings.

—SA
 
Share this answer
 
Comments
NY Andrew 10-Aug-11 0:03am    
Yes I use Breakpoints quite frequently, and yes I ALWAYS use the Debugger, and yes it does get there, but for some reason it only sets the first day of the months color..
Well, I hate to say this, but I think it's time for me to put an end on trying to highlight the current day label.. Yes, I tried your suggestions, but I am getting fed up on this project. The amount of hours I put into trying to highlight a stupid label is starting to grow on me. I am just going to create a simple label with a timer and put this in it:
VB
LabelDay.Text = Format(Now, "%d")
I can't come up with anything else.. Once, again thank you all for your help, sorry for taking your time on this disastrous problem, and I am sure I will be back with more questions soon.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900