VB.NET选项卡控件在更改选项卡时暂停绘图

我有一个带有5个标签的标签控件。每个选项卡上都有大量单独的控件。 (范围从3到70的控件,所有标准复选框,文本框,组合框和单选按钮。)

运行时,图形似乎在更改选项卡的过程中冻结了。因此,您最终将旧选项卡的控件的一部分与新选项卡的控件的一部分放在一起。

我希望能够停止绘制,直到所有控件都完全加载并且代码完成运行“得分”代码为止。

我尝试在选项卡控件和单个页面上都使用“挂起/恢复布局”,但似乎对图形没有任何影响。

我也尝试使用在搜索答案时发现的自定义类,但它要么不会暂停绘图,要么会导致表单在视觉上变得不稳定。

Imports System.Runtime.InteropServices

Friend Class DrawingControl
    <DllImport("user32.dll")>
    Public Shared Function SendMessage(ByVal hWnd As IntPtr,ByVal wMsg As Int32,ByVal wParam As Boolean,ByVal lParam As Int32) As Integer
    End Function

    Private Const WM_SETREDRAW As Integer = 11

    Public Shared Sub SuspendDrawing(ByVal parent As Control)
        SendMessage(parent.Handle,WM_SETREDRAW,False,0)
    End Sub

    Public Shared Sub ResumeDrawing(ByVal parent As Control)
        SendMessage(parent.Handle,True,0)
        parent.Refresh()
    End Sub
End Class

***编辑11/22/2019:添加在更改标签时运行的“评分”代码

以下是更改标签页时运行的事件顺序:

1)TabControl取消选择

Private Sub tabDetails_Deselecting(sender As Object,e As TabControlCancelEventArgs) Handles tabDetails.Deselecting
    tabDetails.SuspendLayout()
    pgAcademic.SuspendLayout()
    pgBusiness.SuspendLayout()
    pgLIS.SuspendLayout()
    pgPatient.SuspendLayout()
    pgRegulatory.SuspendLayout()
End Sub

2)选择了TabControl-这里没有代码-但是,这是在选项卡与每个选项卡的控件轮廓“组合”在一起之后。

3)TabControl索引更改

Private Sub tabDetails_SelectedIndexChanged(sender As Object,e As EventArgs) Handles tabDetails.SelectedIndexChanged
    Dim pg As Integer = Me.tabDetails.SelectedIndex
    Select Case pg
        Case 0
            Me.txtPageScore.Text = score_pgPatientCare
        Case 1
            Me.txtPageScore.Text = score_pgBusiness
        Case 2
            Me.txtPageScore.Text = score_pgLIS
        Case 3
            Me.txtPageScore.Text = score_pgAcademic
        Case 4
            Me.txtPageScore.Text = score_pgRegulatory
    End Select
End Sub

4)步骤3触发此代码:

Private Sub UpdateScore()
    Dim intTotalScore As Integer = Vars.intNewScore  'Max Score = 708
    Dim intScore As Integer = 0

    If Vars.DisableEvents Then Exit Sub
    Vars.DisableEvents = True

    score_pgPatientCare = Scoring.UpdateScore(Me.pgPatient)
    score_pgBusiness = Scoring.UpdateScore(Me.pgBusiness)
    score_pgLIS = Scoring.UpdateScore(Me.pgLIS)
    score_pgAcademic = Scoring.UpdateScore(Me.pgAcademic)
    score_pgRegulatory = Scoring.UpdateScore(Me.pgRegulatory)

    intTotalScore += (score_pgPatientCare + score_pgBusiness + score_pgLIS + score_pgAcademic + score_pgRegulatory)

    Using dbTPT As New BGL_ApplicationsEntities

        '** Use this area if needing to calculate more than YES/NO

        '**********************
        '***  Patient Care  ***
        '**********************
        If chkPatientCare_IRB.Checked Then
            If Not txtPatientCare_IRB.Text.Trim = "" Then
                Dim propertyName = "PatientCare_IRB_Approved"
                intScore = CType(weightSettings.Items.Item(propertyName),Integer)
                score_pgPatientCare += intScore
                intTotalScore += intScore
            End If
        End If

        '**********************
        '***    Business    ***
        '**********************
        If chkBusiness_Replace.Checked Then
            If chkBusiness_Outdated.Checked Or
                chkBusiness_Inferior.Checked Or
                chkBusiness_Savings.Checked Then
                intScore = 0
                Dim propertyName = "Business_Replace_Just"
                intScore = CType(weightSettings.Items.Item(propertyName),Integer)
                score_pgBusiness += intScore
                intTotalScore += intScore
            End If
        End If
    End Using

    ssLabel.Text = "Total Score: " + intTotalScore.ToString
    custProgBar.Value = intTotalScore

    Dim pg As Integer = Me.tabDetails.SelectedIndex
    Select Case pg
        Case 0
            Me.txtPageScore.Text = score_pgPatientCare
        Case 1
            Me.txtPageScore.Text = score_pgBusiness
        Case 2
            Me.txtPageScore.Text = score_pgLIS
        Case 3
            Me.txtPageScore.Text = score_pgAcademic
        Case 4
            Me.txtPageScore.Text = score_pgRegulatory
    End Select

    Vars.DisableEvents = False
End Sub

5)上面的代码为每个标签触发该代码:

Module Scoring
    Public Function UpdateScore(ByVal tab As TabPage) As Integer
        Dim intPgScore As Integer = 0
        Dim intScore As Integer = 0
        Dim intMult As Integer = 0
        Dim arrPropName() As String
        Dim strPropName As String
        Dim strYesno As String = Nothing

        'If Vars.DisableEvents Then Return 0
        'Vars.DisableEvents = True

        Using dbTPT As New BGL_ApplicationsEntities
            If tab.Name = "pgPatient" Then
                For Each pan In tab.ChildControls(Of Panel)
                    If pan.Name.ToString.Substring(0,6) = "chkgrp" Then
                        Dim cuScores() As Integer = {0,0}
                        Dim i As Integer = 0
                        For Each chk As CheckBox In pan.ChildControls(Of CheckBox)
                            If chk.Checked Then
                                arrPropName = Split(chk.Name.Replace("chk","").Replace("CU","PatientCare"),"_")
                                strPropName = arrPropName(0) + "_" + arrPropName(1)
                                Dim w = dbTPT.tbl_Weights.FirstOrDefault(Function(n) n.Weight_Name = strPropName)
                                If w Isnot Nothing Then
                                    intScore = CType(frmNewProject.weightSettings.Items.Item(arrPropName(0) + "_" + arrPropName(1)),Integer)
                                    intMult = CType(frmNewProject.weightSettings.Items.Item(arrPropName(0) + "_" + arrPropName(2)),Integer)
                                    cuScores(i) = intScore * intMult
                                End If
                            Else
                                cuScores(i) = 0
                            End If
                            i += 1
                        Next

                        For Each txt In pan.ChildControls(Of TextBox)
                            txt.Text = cuScores.Max.ToString
                        Next

                        intPgScore += cuScores.Max
                    End If
                Next
            End If

            For Each chk As CheckBox In tab.ChildControls(Of CheckBox)
                intScore = 0
                arrPropName = Split(chk.Name.Replace("chk",""),"_")
                If Not arrPropName(0) = "CU" Then
                    If chk.Checked Then strYesno = "_Yes" Else strYesno = "_No"

                    strPropName = arrPropName(0) + "_" + arrPropName(1) + strYesno
                    Dim w = dbTPT.tbl_Weights.FirstOrDefault(Function(n) n.Weight_Name = strPropName)
                    If w Isnot Nothing Then
                        intScore = CType(frmNewProject.weightSettings.Items.Item(strPropName),Integer)
                    End If
                End If

                intPgScore += intScore
            Next

            For Each rad As RadioButton In tab.ChildControls(Of RadioButton)
                intScore = 0
                arrPropName = Split(rad.Name.Replace("rad","_",2)
                strPropName = arrPropName(0) + "_" + arrPropName(1)
                If rad.Checked Then
                    Dim w = dbTPT.tbl_Weights.FirstOrDefault(Function(n) n.Weight_Name = strPropName)
                    If w Isnot Nothing Then
                        intScore = CType(frmNewProject.weightSettings.Items.Item(arrPropName(0) + "_" + arrPropName(1)),Integer)
                    End If
                End If

                intPgScore += intScore
            Next

            For Each cbox As ComboBox In tab.ChildControls(Of ComboBox)
                intScore = 0
                If cbox.SelectedIndex > -1 Then
                    If cbox.SelectedIndex = 0 Then
                        intScore = 0
                    Else
                        strPropName = cbox.Name.Replace("cbox","") + cbox.SelectedValue
                        Dim w = dbTPT.tbl_Weights.FirstOrDefault(Function(n) n.Weight_Name = strPropName)
                        If w Isnot Nothing Then
                            intScore = CType(frmNewProject.weightSettings.Items.Item(strPropName),Integer)
                        End If
                    End If

                    intPgScore += intScore
                End If
            Next
        End Using

        Return intPgScore

    End Function
End Module

6)选中的选项卡已绘制

Private Sub Paint_Tab(sender As Object,e As PaintEventArgs) Handles _
        pgAcademic.Paint,pgBusiness.Paint,pgLIS.Paint,pgPatient.Paint,pgRegulatory.Paint

    tabDetails.ResumeLayout()
    pgAcademic.ResumeLayout()
    pgBusiness.ResumeLayout()
    pgLIS.ResumeLayout()
    pgPatient.ResumeLayout()
    pgRegulatory.ResumeLayout()
End Sub
nothingzz 回答:VB.NET选项卡控件在更改选项卡时暂停绘图

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3055750.html

大家都在问