是什么导致控件无法在C#中重新绘制

时间:2014-10-29 08:50:51

标签: c# invalidation

我使用TreeView以winform和.Net 4.5显示数据。有时我添加节点时不会重新绘制。为了强制重绘,我使用了this.invalidates()this.update(),但它无效。我添加了一个PaintEventHandler,以查看是否调用了Paint()

    DefaultConstructor(){
        [...]
        this.Paint += new PaintEventHandler(this.OnThisPaint);
    }

    private void OnThisPaint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Console.WriteLine("repaint");
    }

    private void updateTree()
    {
        //Some fabulous code
        Console.WriteLine("Updating the tree");

        try
        {
            this.Invalidate();
            this.Update();
        }
        catch (Exception)
        {
            Console.WriteLine("Error in invalidate");
            throw;
        }
    }

有时不会调用油漆,导致显示错误。即使没有重新绘制控件也不会引发错误。

编辑:

这是我得到的输出:

'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'Microsoft.GeneratedCode'
Bubble Graph // <- Start of the treeview
Cycle Graph
Line Graph
   Flw_OilConc_Graph
   Spec_OilConc_Graph
   WorkingExample
Stacked Graph
   ELR_Smoke_Test
   Full Load p.1
   Spec_Oil_Graph
Table
Table APM
Table Dura // <- Start of the treeview
invalidated
repaint
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\TFS\2014\Source_LYS\Source-1\Sakura II\Projects\Sakura.UI\bin\Debug\Sakura.UI.Reporting.dll', Symbols loaded.
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'Microsoft.GeneratedCode'
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\TFS\2014\Source_LYS\Source-1\Sakura II\Projects\Sakura.UI\bin\Debug\Sakura.DataCoordinating.dll', Symbols loaded.
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\TFS\2014\Source_LYS\Source-1\Sakura II\Projects\Sakura.UI\bin\Debug\Sakura.Reporting.dll', Symbols loaded.
invalidated
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Program Files\Volvo Powertrain\Sakura\Evaluations\Sakura.Eval.dll', Symbols loaded.
'Sakura.UI.vshost.exe' (Managed (v4.0.30319)): Loaded 'Microsoft.GeneratedCode'
repaint
repaint
Bubble Graph // <- Start of the treeview
Cycle Graph
Line Graph
   Flw_OilConc_Graph
   Spec_OilConc_Graph
   UnworkingExample // <- Undisplayed node
   WorkingExample
Stacked Graph
   ELR_Smoke_Test
   Full Load p.1
   Spec_Oil_Graph
Table
Table APM
Table Dura // <- End of the treeview
invalidated
The thread 'vshost.RunParkingWindow' (0x1620) has exited with code 0 (0x0).
The thread '<No Name>' (0xed4) has exited with code 0 (0x0).

什么会导致这种行为?

1 个答案:

答案 0 :(得分:0)

由于实施不佳,显示的控件不是修改过的控件。这就是为什么没有调用油漆的原因。