C# - TabPage 颜色事件
C# 当然不是我的强项,所以我感谢所有慷慨的人们分享他们的知识。我正在使用 Windows 窗体,并且阅读了有关事件的信息,并发现了一些有关如何修改 TabControl 的极好的帮助,因此我可以使用 OnDraw 事件来为选项卡添加一些颜色。
每个选项卡的颜色基于连接变量的状态: 当前(绿色) 丢失(红色) 陈旧(黄色)
OnDraw 事件非常适合更新每个选项卡的颜色,但只有当用户选择不同的选项卡进行查看时才会发生这种情况。
我希望发生的是每当连接状态发生变化时每个选项卡的颜色都会更新。例如,假设 Tab#1 为绿色,但随后连接状态更改为过时,因此现在选项卡需要为黄色,但直到用户单击不同的选项卡和 OnDraw 事件之前它不会变成这样的颜色被触发。
所以我想弄清楚如何做到这一点。当 OnDraw 事件正常触发时(通过用户单击不同的选项卡),“DrawItemEventArgs”参数将传递到事件处理程序中。该变量已经填充了确定单击了哪个选项卡、该选项卡的边界等所需的相关数据。所以我不确定它来自哪里,或者如何以编程方式重新创建这样的调用以重新着色每当连接变量发生更改时都会显示选项卡。
如果我需要澄清任何事情,请告诉我! 谢谢。
C# certainly isn't my strong suit so I appreciate all the generous folk sharing their knowledge. I'm working with a Windows Form and I've read up on events and have found some excellent help on how to modify a TabControl so I can have an OnDraw event that will add some coloring to the tabs.
The color of each tab is based upon the state of a connection variable:
Current (green)
Lost (red)
Stale (yellow)
The OnDraw event works excellent for updating the color of each tab, but that only occurs when the user selects a different tab to view.
What I would like to happen is for the color of each tab to be updated whenever the connection state changes. For example, let's say Tab#1 is colored green, but then the connection state changes to stale so now the tab needs to be colored yellow but it won't get colored like that until the user clicks on a different tab and the OnDraw event is triggered.
So I'm trying to figure out how to do that. When the OnDraw event is triggered normally (by the user clicking on a different tab) a "DrawItemEventArgs" parameter is passed into the even handler. That variable is already populated with the pertinent data needed to figure out which tab was clicked on, the boundaries of that tab and etc. So I am unsure where it came from or how I can programmatically re-create such a call to re-color the tabs whenever the connection variable changes.
Please let me know if I need to clarify anything!
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在控件上调用
Invalidate()
来强制重新绘制。You can call
Invalidate()
on the control to force a repaint.如果您在连接状态更改时触发了一个事件,您可以
在该事件中对所有选项卡执行 操作。
If you have an event fired when your connection state changes you could do an
on all of your tabs from within that event.
如果您想要持续刷新,那么您可能需要创建 System.Timers.Timer 对象。
创建计时器并将计时器刻度值设置为您需要的任何时间间隔(以毫秒为单位)后,它将定期触发
OnTimerTick
事件。在此事件中,您可以通过Invalidate()
方法触发对OnDraw
方法的调用。 Invalidate 告诉系统您的屏幕需要刷新,并且它将在下一个可用机会时调用 OnDraw 和 OnPaint。If you want to have a constant refresh going, then you probably need to create
System.Timers.Timer
object.Once you create a Timer and set the timer tick value to whatever interval you need (in milliseconds) it will fire the
OnTimerTick
event at regular intervals. From this event you can trigger a call to yourOnDraw
method through theInvalidate()
method. Invalidate tells the system that your screen needs to be refreshed and it will call OnDraw and OnPaint at the next available opportunity.