Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么';是否正在重新绘制用户控件?_C#_Winforms_User Controls - Fatal编程技术网

C# 为什么';是否正在重新绘制用户控件?

C# 为什么';是否正在重新绘制用户控件?,c#,winforms,user-controls,C#,Winforms,User Controls,我有一个自定义控件,我正在绘制它的一些内容,如下所示: public class TextItem { public Font Font { get; set; } } public TaskBox() { SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(Contro

我有一个自定义控件,我正在绘制它的一些内容,如下所示:

public class TextItem
{
    public Font Font { get; set; }
}

    public TaskBox()
    {
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.ResizeRedraw, true);

        this.items = new List<TextItem>();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        foreach (TextItem item in items)
        {
            if (item.Bounds.Contains(e.Location))
            {
                item.ForeColor = Color.Red;
                Cursor.Current = Cursors.Hand;
            }
            else
            {
                item.ForeColor = Color.Black;
                Cursor.Current = Cursors.Default;
            }
        }
    }
公共类TextItem
{
公共字体{get;set;}
}
公共任务箱()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
SetStyle(ControlStyles.UserPaint,true);
设置样式(ControlStyles.AllPaintingInWmPaint,true);
SetStyle(ControlStyles.ResizerDraw,true);
this.items=新列表();
}
MouseMove上的受保护覆盖无效(MouseEventArgs e)
{
基地移动(e);
foreach(项目中的TextItem项目)
{
if(item.Bounds.Contains(例如位置))
{
item.ForeColor=颜色为红色;
Cursor.Current=Cursors.Hand;
}
其他的
{
item.ForeColor=颜色.黑色;
Cursor.Current=Cursors.Default;
}
}
}

光标会相应地改变,但是文本不会改变颜色。我缺少一些初始化吗?

您没有告诉它重新绘制。调用
this.Invalidate()当您更改OnPaint()方法中也使用的任何字段或属性,以便用户可以看到副作用时


还请注意,分配Cursor.Current是不确定的,这通常不会持续很长时间,因为光标形状是由
this.Cursor
属性确定的。我怀疑,但没有检查,它应该闪烁。分配属性更好。

您没有告诉它使用新颜色重新绘制,这实际上就是答案。控件必须无效。