突出显示“创建日历的当前日期"问题. [英] Highlighting Current Day of Created Calendar problem..
问题描述
好的,所以我创建了自己的日历版本,但是我不确定如何使它突出显示今天的当前日期..我仅使用(应该起作用)的代码突出显示一个月的第一天或最后一天.出于某种原因,它不突出显示CURRENT DAY.我搜索它来查找今天的标签,然后将其突出显示.好吧,下面是一些代码,可能有助于缩小范围.谢谢大家!
私有 dtNow As 日期 = Now()
私有 Istoday As 布尔值 = 错误
私有 子 SetLabelBackColor( ByRef LabelName As 字符串)
尝试
对于 每个 itm As 控件在中EnumerateAllControls(我)
如果 itm.Name.StartsWith(" span>)然后
对于 每个 c As 控件在 itm.Controls中
如果 TypeOf c 是标签然后
如果 c.Name.StartsWith(" span>)然后
如果 c.Name 是 LabelName 然后
如果 dtNow.Month = StartMonth 然后
c.BackColor =颜色.黄色
结束 如果
其他:c.BackColor = Color.OldLace
结束 如果
结束 如果
结束 如果
下一步
结束 如果
下一步
捕获,例如 As 异常
MsgBox(例如消息)
结束 尝试
结束 子
乍一看,可以看到代码非常脏,无法维护.首先引起关注的是在此片段中是否使用立即数字符串常量进行硬编码.您依赖诸如控件名称之类的不可靠条件,该名称仅用于支持Designer. (注意:控件名称与变量名称无关.)为什么您认为必须按名称查找某些控件?难怪您的代码处于无法正常工作"状态.您应该在期望颜色变化并看到代码到达的位置上放置一个断点. (在问问题之前做过吗?您是否完全使用调试器?)
使用控件,而不使用名称!不要依赖任何字符串的内容.
—SA
好吧,我不想这么说,但是我认为该是时候结束尝试突出显示当日标签的时候了..是的,我尝试了您的建议,但对这个项目感到厌烦.我花很多时间试图突显一个愚蠢的标签,这让我越来越难.我只是要创建一个带有计时器的简单标签,并将其放入其中:LabelDay.Text = Format(Now," %d")我别无其他..再次感谢大家的帮助,对于您在这个灾难性问题上所花的时间深表歉意,我相信我很快会收到更多问题. /blockquote>
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!
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解决方案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
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: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.
这篇关于突出显示“创建日历的当前日期"问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!