如何覆盖此onPaint方法以传递列表?

时间:2014-06-10 06:20:14

标签: c# vb.net override onpaint

我已经覆盖了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参数是另一个自定义类。 我应该在这里使用任何解决方案或方法?

提前致谢。

3 个答案:

答案 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没有任何其他属性,例如DataExtendedProperties

  • 您可以在表单级[或者,我应该说,班级]声明您的列表,并在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并执行适当的操作