Vb.net 调整窗体大小后,未选定选项卡上的VB网络图表未正确定位

Vb.net 调整窗体大小后,未选定选项卡上的VB网络图表未正确定位,vb.net,charts,tabs,resize,Vb.net,Charts,Tabs,Resize,我的表格上有5个标签。每个选项卡有4个图表,大小和间距相等。调整窗体大小时,将发生以下情况: 所选选项卡上的图表(可见)将正确调整大小和位置(良好) 未选定选项卡上的图表将正确调整大小,但不会正确重新定位(错误) 如果我在4个未选择的选项卡中选择一个(称为选项卡A),然后选择任何其他选项卡,然后返回选项卡A,图表将正确重新定位 我没有使用锚或码头,但通过代码这样做。我最初尝试锚定(将每个图表锚定到选项卡页面的4个角上,但当我尝试调整它们的大小时,它不起作用)。Dock不适用,因为我在每个选项

我的表格上有5个标签。每个选项卡有4个图表,大小和间距相等。调整窗体大小时,将发生以下情况:

  • 所选选项卡上的图表(可见)将正确调整大小和位置(良好)
  • 未选定选项卡上的图表将正确调整大小,但不会正确重新定位(错误)
  • 如果我在4个未选择的选项卡中选择一个(称为选项卡A),然后选择任何其他选项卡,然后返回选项卡A,图表将正确重新定位
我没有使用锚或码头,但通过代码这样做。我最初尝试锚定(将每个图表锚定到选项卡页面的4个角上,但当我尝试调整它们的大小时,它不起作用)。Dock不适用,因为我在每个选项卡页上有4个图表

这是我的代码:

Dim origWidth As Integer = 1140
Dim origHeight As Integer = 900
Dim origChrtWidth As Integer = 495
Dim origChrtHeight As Integer = 325
Dim chrt As Chart

Private Sub myForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

    ' For each tab in tab control
    For Each c As Control In Me.tabCharts.Controls

        ' If tab has child
        If c.HasChildren Then

            Dim childControl As Control

            ' For each child control in tab
            For Each childControl In c.Controls

                chrt = TryCast(childControl, Chart)

                ' If child control is chart
                If (chrt IsNot Nothing) Then

                    chrt.Width = origChrtWidth + (Me.Width - origWidth) / 2
                    chrt.Height = origChrtHeight + (Me.Height - origHeight) / 2

                    Me.Refresh()

                    ' Charts are all named chart11, chart12, ..., chart21, chart22, etc.
                    ' Where 1st # is the tab, 2nd # is the chart
                    Select Case Integer.Parse(chrt.Name.Substring(chrt.Name.Length - 1, 1))

                        Case 1
                            chrt.Left = 7
                            chrt.Top = 10

                        Case 2
                            chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
                            chrt.Top = 10

                        Case 3
                            chrt.Left = 7
                            chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7

                        Case 4
                            chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
                            chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7

                    End Select

                    Me.Refresh()

                End If ' If child control is chart
            Next ' For each child control in tab
        End If ' If tab has child

    Next ' For each tab

End Sub
如上所述,上述操作仅适用于所选选项卡及其图表。因此,我添加了处理选项卡选择事件的代码。如果选中该选项卡,则应重新定位图表,但这也不起作用。只有在选择了未选择的选项卡,然后选择了另一个选项卡,并返回到未选择的选项卡之后,图表才会正确更新

Private Sub tabCharts_Selected(sender As Object, e As TabControlEventArgs) Handles tabCharts.Selected

    ' For each tab in tab control
    For Each c As Control In Me.tabCharts.Controls

        ' If tab has child
        If c.HasChildren And c.TabIndex = tabCharts.SelectedIndex Then

            Dim childControl As Control

            ' For each child control in tab
            For Each childControl In c.Controls

                chrt = TryCast(childControl, Chart)

                ' If child control is chart
                If (chrt IsNot Nothing) Then

                        ' Charts are all named chart11, chart12, ..., chart21, chart22, etc.
                        ' Where 1st # is the tab, 2nd # is the chart
                        Select Case Integer.Parse(chrt.Name.Substring(chrt.Name.Length - 1, 1))

                            Case 1
                                chrt.Left = 7
                                chrt.Top = 10

                            Case 2
                                chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
                                chrt.Top = 10

                            Case 3
                                chrt.Left = 7
                                chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7

                            Case 4
                                chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
                                chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7

                        End Select

                        Me.Refresh()

                End If ' If child control is chart
            Next ' For each child control in tab
        End If ' If tab has child

    Next ' For each tab

End Sub
我尝试添加一个循环来重新定位两次。不起作用

我尝试了一种解决方法,在选择一个选项卡时,表单将自身大小调整为+1和-1,以调整所选选项卡上图表的大小和位置。大多数情况下都有效,但当表单执行此操作时,会有一个快速的转换。另外,当表单最大化时,它的大小无法更改,因此我必须将其设置为正常,更改+1-1,然后返回最大化,这不够快。用户将看到恼人的闪烁。这是代码

我更愿意解决问题,而不是使用变通方法。任何帮助都将不胜感激。谢谢

Private Sub tabCharts_Selected(sender As Object, e As TabControlEventArgs) Handles tabCharts.Selected

    Dim flagMax As Boolean

    If Me.WindowState = FormWindowState.Maximized Then
        Me.WindowState = FormWindowState.Normal
        flagMax = True
    End If

    If Me.Width = 1140 Then
        Me.Width = Me.Width + 1
        Me.Width = Me.Width - 1
    Else
        Me.Width = Me.Width - 1
        Me.Width = Me.Width + 1
    End If

    If flagMax = True Then
        Me.WindowState = FormWindowState.Maximized
    End If

我想出了解决问题的办法。我没有对位置和大小进行编码,而是在每个选项卡页面中添加了一个表布局面板,并将每个选项卡中的4个图表放在面板中

“表格布局”面板在各个方向上固定,而图表本身则固定以填充面板的每个单元格。工作起来很有魅力