Edit: Assume a TextBox as the control for the example – where height is not an issue
So I am using the mouse to change a control’s width and location at runtime (through grabbing a handle with a mouse, much the same way that you can change it at design time). However, I am noticing some resizing issues, which I thought would be fixed by calling
<panel>.SuspendLayout
<control>.location = new Point(x, y)
<control>.width = newWidth
<panel>.ResumeLayout
Now both the location and width change correctly, but because the location changes first – you see a blur of the textbox changing width after it moves. Now reading through Suspend and ResumeLayout, I guess they are supposed to be called before the controls are constructed. With that being the case: how do I make sure the location and width change at once to avoid the blur?
Edit: Solved – The following code allowed me to suspend drawing the control before the properties were set, then resume afterward. As opposed to suspending the layout*
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, _
ByVal wMsg As Integer, _
ByVal wParam As Integer,
ByVal lParam As Integer) As Integer
Private Const WM_SETREDRAW As Integer = 11
' Extension methods for Control
<Extension()>
Public Sub ResumeDrawing(ByVal Target As Control, ByVal Redraw As Boolean)
SendMessage(Target.Handle, WM_SETREDRAW, 1, 0)
If Redraw Then
Target.Refresh()
End If
End Sub
<Extension()>
Public Sub SuspendDrawing(ByVal Target As Control)
SendMessage(Target.Handle, WM_SETREDRAW, 0, 0)
End Sub
<Extension()>
Public Sub ResumeDrawing(ByVal Target As Control)
ResumeDrawing(Target, True)
End Sub
Instead of
panel.SuspendLayout(), dothis.SuspendLayout(). Suspend and Resume are guaranteed to traverse down the actual object tree, but I’ve never had luck with it trickling down the container tree.