You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.Net WinForms中FlowLayoutPanel控件不显示内容问题求助

WinForms FlowLayoutPanel控件不显示问题求助

我做了一个带TabControl的WinForms窗体,TabControl里有6个TabPage,每个TabPage都嵌套了一个FlowLayoutPanel,运行时会动态往这些面板里添加自定义控件和标签。但目前有一个页面(现在是第三个)完全看不到任何控件,所有面板的配置都是一致的:Dock:Fill、FlowDirection:TopDown、WrapContents:False。

我给每个标签页的自定义控件数量设了25个上限,一次性加载全部控件不仅会出现显示异常,还会占用过多系统资源。

最开始是第一个标签页出现这个问题,但当控件数量超过10个左右时又自行恢复了。现在第三个标签页的问题一直存在,我已经尝试了这些排查手段:

  • 确认面板不是空的,代码逻辑正常执行,控件确实被添加进去了;
  • 添加了按钮,通过编程方式往面板里加更多标签,没有效果;
  • 通过按钮修改面板的停靠属性、可见性、流向、自动换行等设置,只有把FlowDirection改成Left-Right时控件才会显示,但这样控件会超出面板右侧,而且没有水平滚动条,不符合我的需求;
  • 在设计时给这个面板加了一个大按钮,一开始还是看不到动态添加的控件,但注释掉清除控件的代码后,控件能显示,但宽度只能到按钮的宽度,没法填充整个面板的宽度。

环境信息

  • Visual Studio Community 2019
  • Windows Forms应用程序
  • 目标框架:.NET Framework 4
  • 操作系统:Windows 10

异常表现

相关代码

flpLowStock.Controls.Clear()
flpLowStock.SuspendLayout()

            'Make all new controls the same width (Fill the width and leave room for the scroll bar)
            Dim intWidths As Integer = flpLowStock.Width - 23

            'Add controls to Low Stock panel from filters
            intCurCtrlCount = 0
            For Each ucLSItm As ucBStockRepItem In lsLowStockItems
                'Does Current Item Match our Filters?
                Dim bInclude As Boolean = True
                If strFilteredDept <> "" AndAlso strFilteredDept <> ucLSItm.BackstockItem.Department Then
                    bInclude = False
                ElseIf strFilteredCat <> "" AndAlso strFilteredCat <> ucLSItm.BackstockItem.Category Then
                    bInclude = False
                ElseIf strFilteredDT <> "" AndAlso strFilteredDT <> ucLSItm.BackstockItem.DisplayAction Then
                    bInclude = False
                ElseIf strFilteredStyle <> "" AndAlso ucLSItm.BackstockItem.StyleNumber.Contains(strFilteredStyle) = False Then
                    bInclude = False
                End If

                If bInclude Then
                    'Make sure we're not over the limit
                    If intCurCtrlCount <= intItemLimit Then
                        'Move to Backstock
                        If lsLSHeaders.Contains(ucLSItm.BackstockItem.DisplayAction) = False Then
                            'Check Action Header
                            Dim lblNewAction As New Label With {
                                .Name = "lblHeader" & intCurLabel,
                                .Text = ucLSItm.BackstockItem.DisplayAction,
                                .BackColor = Color.Silver,
                                .ForeColor = dicActions(ucLSItm.BackstockItem.DisplayAction),
                                .AutoSize = False,
                                .Width = intWidths,
                                .Height = 23
                            }
                            intCurLabel += 1
                            flpLowStock.Controls.Add(lblNewAction)
                            'Uptdate Anchor AFTER it's added to the panel
                            lblNewAction.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right

                            lsLSHeaders.Add(ucLSItm.BackstockItem.DisplayAction)
                        End If
                        If lsLSHeaders.Contains(ucLSItm.BackstockItem.Department) = False Then
                            'Check Department Header
                            Dim lblNewDept As New Label With {
                                .Name = "lblHeader" & intCurLabel,
                                .Text = ucLSItm.BackstockItem.Department,
                                .BackColor = Color.Green,
                                .AutoSize = False,
                                .Width = intWidths,
                                .Height = 23
                            }
                            intCurLabel += 1
                            flpLowStock.Controls.Add(lblNewDept)
                            'Uptdate Anchor AFTER it's added to the panel
                            lblNewDept.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right

                            lsLSHeaders.Add(ucLSItm.BackstockItem.Department)
                        End If
                        If lsLSHeaders.Contains(ucLSItm.BackstockItem.Category) = False Then
                            'Check Category Header
                            Dim lblNewCat As New Label With {
                                .Name = "lblHeader" & intCurLabel,
                                .Text = ucLSItm.BackstockItem.Category,
                                .BackColor = Color.LightSkyBlue,
                                .AutoSize = False,
                                .Width = intWidths,
                                .Height = 23
                            }
                            intCurLabel += 1
                            flpLowStock.Controls.Add(lblNewCat)
                            'Uptdate Anchor AFTER it's added to the panel
                            lblNewCat.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right

                            lsLSHeaders.Add(ucLSItm.BackstockItem.Category)
                        End If
                        'Add the control
                        ucLSItm.Width = intWidths
                        flpLowStock.Controls.Add(ucLSItm)
                        'Uptdate Anchor AFTER it's added to the panel
                        ucLSItm.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right
                    End If

                    intCurCtrlCount += 1
                End If
            Next

            If lsLowStockItems.Count > intItemLimit Then
                Dim lblLOver As New Label With {
                    .Name = "lblLOver",
                    .Text = "Update some items and I'll show you more.",
                    .BackColor = Color.LightCoral,
                    .AutoSize = False,
                    .Width = intWidths,
                    .Height = 23
                }
                flpLowStock.Controls.Add(lblLOver)
            End If


flpLowStock.ResumeLayout(False)
'Fix the layout and show the scrollbar
flpLowStock.PerformLayout()

内容的提问来源于stack exchange,提问作者MightyFeldar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:57:44