Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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
Vb.net tablelayoutpanel未引发cellpaint事件_Vb.net_Tablelayoutpanel - Fatal编程技术网

Vb.net tablelayoutpanel未引发cellpaint事件

Vb.net tablelayoutpanel未引发cellpaint事件,vb.net,tablelayoutpanel,Vb.net,Tablelayoutpanel,我正在尝试绘制tablelayoutpanel对象的行。我为cellpaint事件添加了一个处理程序,但我甚至无法让调试器捕获此事件。是否需要在tablelayoutpanel上配置其他内容,以便自定义绘制单元格 我尝试了一个强制调用,以在usercontrol加载时使其无效,但没有帮助 这是我的密码: Private Sub TableLayoutPanel3_CellPaint(sender As Object, e As TableLayoutCellPaintEventArgs) Ha

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

我尝试了一个强制调用,以在usercontrol加载时使其无效,但没有帮助

这是我的密码:

 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
我用它作为参考

编辑:“我的tablelayoutpanel”单元格中的对象未设置为自动调整大小。当我启用autosize时,我发现cellpaint事件将引发,并且我的对象会像我预期的那样直观地显示出来。我想这种行为是故意的


为了在运行时加载UserControl,我将创建一个按钮来触发它。我可以在Form_Load上完成,但对于本例,我将在单击按钮时完成

我有以下文件:

  • 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。我在这个用户控件中创建了一个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


希望这能把事情弄清楚

CellPaint事件仅在重新绘制单元格时发生。你确定要重画它吗?即使我在设计时构建了我的面板,它也应该在加载时绘制单元格,对吗?在你展示的参考资料中,问题的答案似乎创建了一个新的TableLayoutCellPaintEventHandler(tableLayoutPanel1\u CellPaint)
。。。正在创建要触发的事件。试试:)顺便说一句,我已经测试了你的代码,它对我很有效。尝试重新生成/清洁溶液,然后再次测试。在从引用链接转换示例代码时,我仍然无法触发事件。不确定我做错了什么。结果发现我的问题与tablelayoutpanel对象的配置有关。当我在designtime在usercontrol上删除了一个新的处理程序,并将该处理程序添加到现有的cellpaint事件时,我发现它可以按预期工作。因此,我将在运行时删除我的另一个,并用我的标签对象填充行,而不是在设计时执行所有操作。我真的很感谢你在这方面的帮助。@TWood很高兴你找到了答案!:)罪魁祸首属性似乎是任何单元格上属性autosize设置为false的对象。我在原来的帖子中添加了一张图片来反映这一点。