Control Flickering

himanshubari

Newcomer
Joined
Mar 14, 2005
Messages
9
Hey,

can somebody help me out to avoid the flickering of controls that I am getting while I mobve them on the forma t runtime.
here is the code I have added to the mouse events for a button I hav on my form
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
If e.Button = MouseButtons.Left Then
moving = True
End If
End Sub

Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If moving Then
Button1.Location = New Point(e.X, e.Y)
Button1.Refresh()
Button1.Parent.Refresh()

End If

End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
moving = False
End Sub


I also added these lines after the initialize component call to enable double bufferring
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)


HELP WILL REALLY BE APPRECIATED
THANKS
 
I'm not really sure, but I don't think you need the
Button1.Refresh()
Button1.Parent.Refresh()
lines.

If I'm wrong and you do need them. They're still most likely what's causing the flickering.
 
Hi, typically when moveing controls around at runtime, you would not animate the whole control, but rather draw a rectangle to represent the control and only on the mouse up redraw the control. Try it in the Visual Studio designer!

I did a sample for another forum, this moves a Listbox around at runtime with the mouse, project attached.

Hope this helps
 

Attachments

Last edited by a moderator:
Thanks!!!

Hey !!...I ran the code that demonstrated moving the listbox at run time.
Thanks a lot, I think this techniquewill serve my purpose. Actually I am working on a simple UI configurator that will instantialte the control at runtime and allow it to be moved around on the form. The rectangle technique of yours will work great I guess
Thanks Again!
 
I know that I'm resurrecting an old thread here, but I'm looking for answers and hope I can get some. Keep in mind that I'm self-taught and only started about 2 months ago...so if something is blatant that I'm missing feel free to make me feel like an idiot.

I downloaded the project above and it worked great for my needs (thanks!). However I am adding buttons to a panel that can then be dragged and dropped. I'm trying to get the painting of the rectangle on the panel instead of the form. Also the rectangle location has to be based on the panel.

Here is the code for the painting of the rectangle:
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        If m_selected Then
            ControlPaint.DrawFocusRectangle(e.Graphics, m_selectionRect)
        End If

End Sub

Any help would be appreciated.
 
Back
Top