【问题标题】:tablelayoutpanel not raising cellpaint eventtablelayoutpanel 没有引发 cellpaint 事件
【发布时间】:2014-05-01 18:27:53
【问题描述】:

我正在尝试绘制我的 tablelayoutpanel 对象的行。我为 cellpaint 事件添加了一个处理程序,但我什至无法让调试器捕获此事件。我还需要在 tablelayoutpanel 上配置什么,以便自定义绘制我的单元格吗?

我已尝试强制调用使用户控件加载无效,但这没有帮助。

这是我的代码:

 Private Sub TableLayoutPanel3_CellPaint(sender As Object, e As TableLayoutCellPaintEventArgs) Handles TableLayoutPanel3.CellPaint
    Select Case e.Row
        Case 1
            e.Graphics.FillRectangle(Brushes.Orange, e.CellBounds)
        Case 2
            e.Graphics.FillRectangle(Brushes.Green, e.CellBounds)
        Case 3
            e.Graphics.FillRectangle(Brushes.Yellow, e.CellBounds)
    End Select
End Sub

我使用this other thread 作为参考。

编辑:我的表格布局面板单元格中的对象未设置为自动调整大小。当我启用自动调整大小时,我发现 cellpaint 事件会引发,并且我的对象会按预期显示。我想这种行为是设计使然。

【问题讨论】:

  • CellPaint 事件仅在重绘单元格时发生。你确定要重绘它吗?
  • 即使我在设计时构建了面板,它也应该在加载时绘制单元格,对吧?
  • 在您展示的参考资料中,问题的答案似乎创建了一个 New TableLayoutCellPaintEventHandler(tableLayoutPanel1_CellPaint) ... 创建要触发的事件。试试看:)
  • 顺便说一下,我已经测试了你的代码,它对我有用。尝试重建/清理解决方案并再次测试。它应该可以工作。
  • 在从参考链接转换示例代码时,我仍然无法触发事件。不知道我做错了什么。

标签: vb.net tablelayoutpanel


【解决方案1】:

为了在运行时加载 UserControl,我将创建一个按钮来触发它。我可以在 Form_Load 上执行此操作,但对于此示例,我将在 Button_Click 上执行此操作。

我有以下文件:

  • Form1.vb
  • UserControl1.vb

Form1.vb

在我的Form1.vb 中,我有一个 TableLayoutPanel(用于调整控件的大小)。在我的 TableLayoutPanel 的第一行中,我有一个按钮,通知用户“加载用户控件”。第二行是我的 UserControl 的占位符。

以下代码只是将我创建的 UserControl1 添加到我的 TableLayoutPanel 中

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ucColors As New UserControl1 'Create a new instance of the UserControl
        ucColors.Dock = DockStyle.Fill 'Make it dock to it's parent container (this tablelayoutpanel)
        TableLayoutPanel1.Controls.Add(ucColors, 0, 1) 'Add the control to the tablelayoutpanel
    End Sub
End Class

UserControl1.vb

这是我的 UserControl1.vb。我在这个 UserControl 中创建了一个 TableLayoutPanel 并将其停靠(填充)。这会使 TableLayoutPanel 调整为 UserControl 的大小。我创建了三行,因为您的示例中有三行。

这是我绘制 UserControl>TableLayoutPanels>Rows 所需的唯一代码。

Public Class UserControl1
    Private Sub TableLayoutPanel1_CellPaint(sender As Object, e As TableLayoutCellPaintEventArgs) Handles TableLayoutPanel1.CellPaint
        Select Case e.Row
            Case 0 'Row 1
                e.Graphics.FillRectangle(Brushes.Orange, e.CellBounds)
            Case 1 'Row 2
                e.Graphics.FillRectangle(Brushes.Green, e.CellBounds)
            Case 2 'Row 3
                e.Graphics.FillRectangle(Brushes.Yellow, e.CellBounds)
            End Select
    End Sub
End Class

运行时

当我执行我的程序时,Form1 会弹出:

然后当我按下加载按钮时,会发生这种情况:


所以,当我点击我的按钮时,会创建一个新的用户控件实例,使其执行 UserControl 中的 Cell_Paint。

希望这可以解决问题!

【讨论】:

  • 原来我的问题与我的 tablelayoutpanel 对象的配置有关。当我在设计时在用户控件上放置一个新的并将处理程序添加到现有的 cellpaint 事件时,我发现它可以按预期工作。因此,我将删除我的另一个并在运行时用我的标签对象填充行,而不是在设计时全部完成。非常感谢您在这方面的帮助。
  • @TWood 很高兴你知道了! :)
  • 罪魁祸首属性似乎是一个对象,其属性 autosize 在任何单元格上设置为 false。我在原始帖子中添加了一张图片以反映这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2017-11-29
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多