我已经覆盖了onPaint方法,我打算做一个小修改,我需要从我的VB.NET代码将列表传递给这个C#脚本,下面是我的代码。
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参数是另一个自定义类。 我应该在这里使用任何解决方案或方法?
提前致谢。
答案 0 :(得分:1)
您无法通过onPaint事件传递列表,您可以做的是以另一种方式传递并在on paint事件中使用该parmater
您可以按如下方式修改您的课程:
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
}
}
答案 1 :(得分:1)
onPaint
事件的签名是
protected virtual void OnPaint(PaintEventArgs e)
因此,您无法传递任何其他参数,尤其是PaintEventArgs
没有任何其他属性,例如Data
或ExtendedProperties
onPaint
中使用它。 Thread.SetData
传递您的列表,然后通过onPaint
Thread.GetData
来检索它}。答案 2 :(得分:0)
这就是我做的......
我为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并执行适当的操作