B
B. Cline
Hi,
I'm trying to (re)build a form while it is displayed. I thought
Suspendlayout and Resumelayout would stop my form from refreshing until I
finished building it. Unfortunately it really doesn't. Since my real form is
far more complicated, I'm including a simple example. The flickering is much
more extreme in my real form and to make the situation even more complex
also uses several tabcontrols and datagrids. The datagrids also fill
extremely slowly unless I leave them visible = False until they have
finished
binding. These aren't necessary to show the effect though.
My question is, have I completely misunderstood what suspend and resume
layout do? I know I could temporarily hide the container controls which
makes loading more efficient but looks terrible, but I would prefer to
simply - well - suspend the layout until I'm finished.
Thanks - Any ideas welcome
Ben
Example Form Follows (My real code would be too long and this suffices):
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents chkNapTime As System.Windows.Forms.CheckBox
Friend WithEvents cmdBuildControls As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.cmdBuildControls = New System.Windows.Forms.Button
Me.chkNapTime = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Location = New System.Drawing.Point(24, 24)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(248, 176)
Me.Panel1.TabIndex = 0
'
'cmdBuildControls
'
Me.cmdBuildControls.Location = New System.Drawing.Point(312, 32)
Me.cmdBuildControls.Name = "cmdBuildControls"
Me.cmdBuildControls.Size = New System.Drawing.Size(104, 24)
Me.cmdBuildControls.TabIndex = 1
Me.cmdBuildControls.Text = "Build Controls"
'
'chkNapTime
'
Me.chkNapTime.Checked = True
Me.chkNapTime.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkNapTime.Location = New System.Drawing.Point(312, 88)
Me.chkNapTime.Name = "chkNapTime"
Me.chkNapTime.Size = New System.Drawing.Size(144, 16)
Me.chkNapTime.TabIndex = 2
Me.chkNapTime.Text = "Sleep during process"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Controls.Add(Me.chkNapTime)
Me.Controls.Add(Me.cmdBuildControls)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cmdBuildControls_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdBuildControls.Click
'Shouldn't this stop the controls (and form) from repainting?
Me.SuspendLayout()
Panel1.SuspendLayout()
Panel1.Controls.Clear()
'Pretend to do something
If chkNapTime.Checked Then System.Threading.Thread.Sleep(500)
'Add Controls
For i As Integer = 0 To 4
Dim lbl As Label
Dim txt1 As TextBox
Dim txt2 As TextBox
lbl = New Label
lbl.Location = New Point(5, i * 24)
lbl.Size = New Size(60, 20)
lbl.Text = "My Label " & i.ToString
txt1 = New TextBox
txt1.Location = New Point(65, i * 24)
txt1.Size = New Size(60, 20)
txt1.ReadOnly = True
txt1.Text = "My TextBox (1) " & i.ToString
txt2 = New TextBox
txt2.Location = New Point(130, i * 24)
txt2.Size = New Size(60, 20)
txt2.Text = "My TextBox (2) " & i.ToString
Panel1.Controls.AddRange(New Control() {lbl, txt1, txt2})
Next
'Pretend to do something else
If chkNapTime.Checked Then System.Threading.Thread.Sleep(1000)
'I would expect the form to show the newly added controls here
Panel1.ResumeLayout()
Me.ResumeLayout()
End Sub
End Class
I'm trying to (re)build a form while it is displayed. I thought
Suspendlayout and Resumelayout would stop my form from refreshing until I
finished building it. Unfortunately it really doesn't. Since my real form is
far more complicated, I'm including a simple example. The flickering is much
more extreme in my real form and to make the situation even more complex
also uses several tabcontrols and datagrids. The datagrids also fill
extremely slowly unless I leave them visible = False until they have
finished
binding. These aren't necessary to show the effect though.
My question is, have I completely misunderstood what suspend and resume
layout do? I know I could temporarily hide the container controls which
makes loading more efficient but looks terrible, but I would prefer to
simply - well - suspend the layout until I'm finished.
Thanks - Any ideas welcome
Ben
Example Form Follows (My real code would be too long and this suffices):
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents chkNapTime As System.Windows.Forms.CheckBox
Friend WithEvents cmdBuildControls As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.cmdBuildControls = New System.Windows.Forms.Button
Me.chkNapTime = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Location = New System.Drawing.Point(24, 24)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(248, 176)
Me.Panel1.TabIndex = 0
'
'cmdBuildControls
'
Me.cmdBuildControls.Location = New System.Drawing.Point(312, 32)
Me.cmdBuildControls.Name = "cmdBuildControls"
Me.cmdBuildControls.Size = New System.Drawing.Size(104, 24)
Me.cmdBuildControls.TabIndex = 1
Me.cmdBuildControls.Text = "Build Controls"
'
'chkNapTime
'
Me.chkNapTime.Checked = True
Me.chkNapTime.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkNapTime.Location = New System.Drawing.Point(312, 88)
Me.chkNapTime.Name = "chkNapTime"
Me.chkNapTime.Size = New System.Drawing.Size(144, 16)
Me.chkNapTime.TabIndex = 2
Me.chkNapTime.Text = "Sleep during process"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Controls.Add(Me.chkNapTime)
Me.Controls.Add(Me.cmdBuildControls)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cmdBuildControls_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmdBuildControls.Click
'Shouldn't this stop the controls (and form) from repainting?
Me.SuspendLayout()
Panel1.SuspendLayout()
Panel1.Controls.Clear()
'Pretend to do something
If chkNapTime.Checked Then System.Threading.Thread.Sleep(500)
'Add Controls
For i As Integer = 0 To 4
Dim lbl As Label
Dim txt1 As TextBox
Dim txt2 As TextBox
lbl = New Label
lbl.Location = New Point(5, i * 24)
lbl.Size = New Size(60, 20)
lbl.Text = "My Label " & i.ToString
txt1 = New TextBox
txt1.Location = New Point(65, i * 24)
txt1.Size = New Size(60, 20)
txt1.ReadOnly = True
txt1.Text = "My TextBox (1) " & i.ToString
txt2 = New TextBox
txt2.Location = New Point(130, i * 24)
txt2.Size = New Size(60, 20)
txt2.Text = "My TextBox (2) " & i.ToString
Panel1.Controls.AddRange(New Control() {lbl, txt1, txt2})
Next
'Pretend to do something else
If chkNapTime.Checked Then System.Threading.Thread.Sleep(1000)
'I would expect the form to show the newly added controls here
Panel1.ResumeLayout()
Me.ResumeLayout()
End Sub
End Class