我如何重写这个onPaint方法来传递一个列表?
本文关键字:一个 列表 方法 重写 onPaint 何重写 | 更新日期: 2023-09-27 18:07:57
我已经覆盖了onPaint方法,我计划做一个小的修改,我需要从我的VB传递一个列表到这个c#脚本。. NET代码,下面是我的代码。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(SystemColors.Window);
for (int i = 0; i < Months.Length; i++)
{
foreach (MonthViewDay day in Months[i].Days)
{
if (!day.Visible) continue;
MonthViewBoxEventArgs evtDay = new MonthViewBoxEventArgs(e.Graphics, day.Bounds, day.Date.Day.ToString(),
StringAlignment.Far,
day.Grayed ? DayGrayedText : (day.Selected ? DaySelectedTextColor : ForeColor),
day.Selected ? DaySelectedBackgroundColor : DayBackgroundColor);
if (day.Date.Equals(DateTime.Now.Date))
{
evtDay.BorderColor = TodayBorderColor;
}
//this is where I plant to add my code IF I get to know to pass a list
else
{
//search if day.Date is present in the list
//if present then update a different border color
}
DrawBox(evtDay);
}
注意,我的list参数是另一个自定义类。这里有什么我应该使用的解决方案或方法吗?
你不能通过onPaint事件传递列表你可以通过另一种方式传递它并在on paint事件中使用该参数
你可以这样修改你的类:
Object obj = new Object();
List<int> _list = new List<int>();
Public void PassList(List<int> myList)
{
lock(obj)
{
_list = myList;
}
}
protected override void OnPaint(PaintEventArgs e)
{
lock(obj)
{
// Do something with the _list
}
}
onPaint
事件的签名为
protected virtual void OnPaint(PaintEventArgs e)
因此你不能传递任何其他参数,特别是PaintEventArgs
没有任何额外的属性,如Data
或ExtendedProperties
- 你可以在表单级别(或者,我应该说,类级别)声明你的列表,并在你的
onPaint
中使用它。 - 如果,让我们说,你的油漆发生在不同的线程,当你设置线程时,你可以通过
Thread.SetData
传递你的列表,然后通过Thread.GetData
在onPaint
中检索它。 - 你可以在某种静态类中设置你的列表并从那里获取它
我就是这么做的
我为paint事件添加了一个处理程序
AddHandler this.monthView1.Paint, AddressOf this.iPass //this is the method that would pass the list
我通过添加@User
提到的方法来更新我的自定义控件public void PassList(List<DateTime> myList)
{
lock (obj)
{
_list = myList;
}
}
现在,在我的protected override void OnPaint(PaintEventArgs e)
事件中,我能够访问_list并执行适当的操作