1

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
Phil
  • 321
  • 2
  • 14

2 Answers2

1

An appropriate solution to preventing this type of flickering to occur wouldn't be changing both of the values at once, as one will always be set before the other, but setting the form to use double buffering. This way, your changes are drawn off screen before being copied onto your visible region. You can find this property on your form.

MSDN: For additional information on the DoubleBuffered control property.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • I'm a little confused. I've set the following settings for the panel. **Me.SetStyle** (ControlStyles.AllPaintingInWmPaint Or _ ControlStyles.UserPaint Or _ ControlStyles.DoubleBuffer, True) \n **Me.UpdateStyles** () \n **Me.DoubleBuffered** = True. None of this helps the jitter. – Phil Jul 06 '11 at 16:30
1

Instead of panel.SuspendLayout(), do this.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.

drharris
  • 11,194
  • 5
  • 43
  • 56
  • I've tried this. Out of curiosity I also tried suspending the form layout. None of them worked. – Phil Jul 06 '11 at 18:01
  • Hmm, the only other way I've tried is by sending a WM_REDRAW command via Win32. Here's one way to do it: http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children/487757#487757. It'd be pretty easy to turn these into extension methods on `Control` so you can use it in multiple places. – drharris Jul 06 '11 at 18:05
  • I've just spent the last hour trying to hold off the WM_PAINT message, but oddly enough, neither the panel nor the textbox actually controls the painting of border of the textbox. The resizing still works, just no filler in the panel, or white (including the inner text) in the textbox. I will check out WM_REDRAW - thanks for the suggestion. – Phil Jul 06 '11 at 18:32
  • Good to hear. Don't you just love how .NET doesn't give you direct access to important functionality like this? – drharris Jul 06 '11 at 19:53
  • Yeah, I don't have much experience programming, but the I did become fond of java. .NET annoys me quite often haha. Again though, that's just in my small experience. – Phil Jul 06 '11 at 20:03