-
Jan 12th, 2021, 06:52 AM
#1
Thread Starter
Member
[RESOLVED] Stop Flicker of the windows form in visual basic 2010
HI
Hope you all are doing fine.
I have been trying to make a sifi look form for my project. it works however it flickers as i scroll it down or up. below mentioned is the code for the same and a image of it is attached.
i have also tried double buffered but it is still flickering.
Code:
Public Class Info_Tab
Private Sub Info_Tab_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
Me.Close()
End Sub
Private Sub Info_Tab_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim gp As New Drawing2D.GraphicsPath
Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180))
Dim A As New Point(50, 500)
Dim B As New Point(50, 50)
Dim C As New Point(100, 50)
Dim D As New Point(130, 80)
Dim F As New Point(420, 80)
Dim Gg As New Point(450, 50)
Dim H As New Point(500, 50)
Dim I As New Point(500, 500)
Dim myPoints As Point() = {A, B, C, D, F, Gg, H, I}
gp.AddPolygon(myPoints)
e.Graphics.FillPath(Brushes.Transparent, gp)
Me.Region = New Region(gp)
End Sub
Private Sub Info_Tab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub
Last edited by dday9; Jan 12th, 2021 at 04:29 PM.
-
Jan 12th, 2021, 08:43 AM
#2
Re: Stop Flicker of the windows form in visual basic 2010
On windows 7, VS2010, I don't have the flickering. (I put a list box with 50 lines)
What controls do you have in it ?
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jan 12th, 2021, 08:55 AM
#3
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by Delaney
On windows 7, VS2010, I don't have the flickering. (I put a list box with 50 lines)
What controls do you have in it ?
I am running this on windows-10 and the number of controls go at runtime that form 20 to 50 labels. you see the most important part is the shape of the form and it keeps it but when i click the scrollbars or navigate in there then it shows the flickering and it goes back to the default windows form size.
-
Jan 12th, 2021, 10:14 AM
#4
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
I am running it on windows-10 > visual studio 2010 :>> it has labels created at run time that ranges from 20 to 50. The shaped form works well but when I click on scrollbars it flickers and the code looses this sifi shape
-
Jan 12th, 2021, 10:22 AM
#5
Re: Stop Flicker of the windows form in visual basic 2010
It certainly sounds like you are capable of triggering a whole lot of Paint events for all the controls, and that will cause flickering. Flickering is just the result of not all the controls being able to redraw in a single screen refresh, which is likely happening at around 60 Hz, so you don't have be wasting too much time for it to become visible.
Debugging this might get a bit tricky, because if you put breakpoints in event handlers...it won't flicker, since nothing will be happening while execution is stopped at the breakpoint. Based on the description, I would expect that when the scrollbar is interacted with, it raises an event which causes something to happen on all the other controls, thereby causing the other controls to paint, which means that you get a whole cascade of paint events. So, the first place to look is any events triggered by the scrollbar. What events of that are you handling, and what is being done?
My usual boring signature: Nothing
-
Jan 12th, 2021, 11:05 AM
#6
Re: Stop Flicker of the windows form in visual basic 2010
Since you're redefining a region to shape the form, perhaps you need to override the OnPaint event and handle the drawing there, so you can do the painting in one pass. If you do the painting in the paint event, the default painting of the form is already done before the secondary Paint Event is raised for you to complete your portion of the Paint event.
I'm a little surprised that you get flashing in Win10, since desktop compositing is turned on by default and you have to go out of your way to disable it. The desktop compositing removes the need to do double-buffering in a lot of cases, as the form window is always drawn and maintained in memory and it, along with all other windows, are composited to the screen from those maintained memory images.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 12th, 2021, 12:23 PM
#7
Re: Stop Flicker of the windows form in visual basic 2010
You can still get flickering, if the draw takes too long. Lots of controls clear to white, then draw. The white shouldn't ever be seen, because the new drawing comes in fast enough that the white never gets drawn, but if compositing the new image takes too long, there is a time when the valid representation of the control is white. Desktop compositing shouldn't impact that, because the flicker is due to the control being drawn correctly twice, but there being a significant difference between the first and second drawing.
My usual boring signature: Nothing
-
Jan 12th, 2021, 02:23 PM
#8
Re: Stop Flicker of the windows form in visual basic 2010
You said you are double buffering but you are not. SetStyle.OptimizedDoubleBuffer only works in combination with SetStyle.AllPaintingInWMPaint and SetStyle.UserPaint. But that's been obsolete for so long that most people have forgotten it. Just set the form's DoubleBuffered property to True in the Designer or the Load event. It does exactly the same job as all three SetStyles.
BB
-
Jan 12th, 2021, 04:25 PM
#9
Re: Stop Flicker of the windows form in visual basic 2010
By the way, do you need the 20 to 50 labels ? maybe you can replace them by one listbox.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jan 13th, 2021, 12:05 AM
#10
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Okay - here are things that i have now
- I have reduced the label control to only 1 that is on design time
-yes, that double buffered is se to true from IDE- VB2010 see the attached
and as you said about these two new properties i have put them in the load of form as below.
Private Sub Info_Tab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
is that the right way ??
And now flickering is reduced some how but it is still there. after 30 or 40 clicks it still goes back to flickering.
-
Jan 13th, 2021, 12:06 AM
#11
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
you are right - i have reduced the number of labels to 1 only - and all the the text now in one label control
-
Jan 13th, 2021, 12:07 AM
#12
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
umm okay , so specifically where i must look for and please make me understand in the code.
-
Jan 13th, 2021, 12:19 AM
#13
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by boops boops
You said you are double buffering but you are not. SetStyle.OptimizedDoubleBuffer only works in combination with SetStyle.AllPaintingInWMPaint and SetStyle.UserPaint. But that's been obsolete for so long that most people have forgotten it. Just set the form's DoubleBuffered property to True in the Designer or the Load event. It does exactly the same job as all three SetStyles.
BB
Okay - here are things that i have now
- I have reduced the label control to only 1 that is on design time
-yes, that double buffered is se to true from IDE- VB2010 see the attached
and as you said about these two new properties i have put them in the load of form as below.
Private Sub Info_Tab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
is that the right way ??
And now flickering is reduced some how but it is still there. after 30 or 40 clicks it still goes back to flickering.
-
Jan 13th, 2021, 12:20 AM
#14
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by Delaney
By the way, do you need the 20 to 50 labels ? maybe you can replace them by one listbox.
you are right - i have reduced the number of labels to 1 only - and all the the text now in one label control
-
Jan 13th, 2021, 12:20 AM
#15
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by Shaggy Hiker
You can still get flickering, if the draw takes too long. Lots of controls clear to white, then draw. The white shouldn't ever be seen, because the new drawing comes in fast enough that the white never gets drawn, but if compositing the new image takes too long, there is a time when the valid representation of the control is white. Desktop compositing shouldn't impact that, because the flicker is due to the control being drawn correctly twice, but there being a significant difference between the first and second drawing.
umm okay , so specifically where i must look for and please make me understand in the code.
-
Jan 13th, 2021, 12:22 AM
#16
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by passel
Since you're redefining a region to shape the form, perhaps you need to override the OnPaint event and handle the drawing there, so you can do the painting in one pass. If you do the painting in the paint event, the default painting of the form is already done before the secondary Paint Event is raised for you to complete your portion of the Paint event.
I'm a little surprised that you get flashing in Win10, since desktop compositing is turned on by default and you have to go out of your way to disable it. The desktop compositing removes the need to do double-buffering in a lot of cases, as the form window is always drawn and maintained in memory and it, along with all other windows, are composited to the screen from those maintained memory images.
okay , so can you tell me how to do that in code ?
-
Jan 13th, 2021, 05:12 AM
#17
Re: Stop Flicker of the windows form in visual basic 2010
Each time you change the contains of a control, the default paint event is called (even if it is empty and if you didn't set it as all the event exist), the more controls you have, the more paint events are called and it takes times (even if the event is empty) so the flickering. that's what has explained Shaggy. It is the reason why you always must use the less number of controls as possible. If you have only one Listbox or one Label, you call the paint event of only that control so only one time instead of 20-50 times.
The paint event of the form is also toggled when you use the scroll bar, in this case you have to redraw each control and the shape of the form each time you use it .... to see the impact, put a messagebox.show("paint event") in the paint event, you will see what provoke the scroll bar...
Last edited by Delaney; Jan 13th, 2021 at 05:17 AM.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jan 13th, 2021, 05:17 AM
#18
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by lokesh R
Okay - here are things that i have now
- I have reduced the label control to only 1 that is on design time
-yes, that double buffered is se to true from IDE- VB2010 see the attached
and as you said about these two new properties i have put them in the load of form as below.
Private Sub Info_Tab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
is that the right way ??
And now flickering is reduced some how but it is still there. after 30 or 40 clicks it still goes back to flickering.
If you set DoubleBuffered for the form to True in the IDE, you don't need the SetStyle statements. As I said, they do the same job.
If the form still flickers, at least you know that the cause is something else.
It's strange that the flickering builds up over time. I wonder if it's changing the Region in the Paint Event handler that causes it? After all, changing the Region forces a new paint event, so the Message Queue gets overfilled with Paint messages. Perhaps that can cause flickering.
So I suggest you declare your Region as a variable at Form Level, and put the code for creating the Region in a separate Function or Sub. Then call the Function or Sub from the Load event or some other event such as Form.SizeChanged. I haven't tried that out and I don't have time now, so maybe someone can suggest a better way to do it.
EDIT: ignore the above suggestions, see post #20 instead.
BB
Last edited by boops boops; Jan 13th, 2021 at 06:07 AM.
-
Jan 13th, 2021, 05:33 AM
#19
Re: Stop Flicker of the windows form in visual basic 2010
What is strange is: I have recreated the form with the code of the OP, put 50 labels at runtime, I don't have the double buffering set to true, I am on a old windows 7, 32Bits, no great graphic card (chip included on the motherboard) and it is a pentium dual core and I don't have the flickering. I have to test it on windows 10 to check.
EDIT :
Tested on windows 10, the exact same project, I don't have either the flickering. Do you have something else in you project beside the code shown at first ?
Last edited by Delaney; Jan 13th, 2021 at 05:38 AM.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jan 13th, 2021, 06:02 AM
#20
Re: Stop Flicker of the windows form in visual basic 2010
Hi Lokesh, I can suggest a fairly easy way to change your code from post 1 above.
1. Copy all the code (including the Region statement) from the Paint event to the Load event.
2. Delete the e.Graphics.FillPath statement. Normally you would leave that in the Paint event, but you don't need it at all if your are filling with Brushes.Transparent, because that does nothing at all.
3. Delete the Paint event.
The idea of this is to set the shape of the Form just once, in the Load sub, instead of in the Paint sub which fires almost every time you change something. If you later want to make changes to the shape or position of the Form at run time, it won't be hard to add extra code for that.
BB
-
Jan 13th, 2021, 09:05 AM
#21
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by boops boops
Hi Lokesh, I can suggest a fairly easy way to change your code from post 1 above.
1. Copy all the code (including the Region statement) from the Paint event to the Load event.
2. Delete the e.Graphics.FillPath statement. Normally you would leave that in the Paint event, but you don't need it at all if your are filling with Brushes.Transparent, because that does nothing at all.
3. Delete the Paint event.
The idea of this is to set the shape of the Form just once, in the Load sub, instead of in the Paint sub which fires almost every time you change something. If you later want to make changes to the shape or position of the Form at run time, it won't be hard to add extra code for that.
BB
WOOOOOOW >>> It worked . Thank you so much.
< / L R. . . . >
-
Jan 13th, 2021, 09:07 AM
#22
Hyperactive Member
Re: Stop Flicker of the windows form in visual basic 2010
if your problem is solved, dont forget to mark this thread as resolved.
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Jan 13th, 2021, 09:08 AM
#23
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by Delaney
What is strange is: I have recreated the form with the code of the OP, put 50 labels at runtime, I don't have the double buffering set to true, I am on a old windows 7, 32Bits, no great graphic card (chip included on the motherboard) and it is a pentium dual core and I don't have the flickering. I have to test it on windows 10 to check.
EDIT :
Tested on windows 10, the exact same project, I don't have either the flickering. Do you have something else in you project beside the code shown at first ?
Yes, I have 15 forms in my project and other controls as well. I will post a video once i have created it all. for the time being "Boops Boops" has suggested the code replacement into load event - and that is working perfectly.
Thanks to all you guys !!!
< / L R. . . . >
-
Jan 13th, 2021, 09:10 AM
#24
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by VB.NET Developer
if your problem is solved, dont forget to mark this thread as resolved.
Yes, I will !
< / L R. . . . >
-
Jan 13th, 2021, 09:12 AM
#25
Thread Starter
Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by VB.NET Developer
if your problem is solved, dont forget to mark this thread as resolved.
Forgot to ask - how do i mark it as "resolved" ?
< / L R. . . . >
-
Jan 13th, 2021, 09:15 AM
#26
Hyperactive Member
Re: Stop Flicker of the windows form in visual basic 2010
Originally Posted by lokesh R
Forgot to ask - how do i mark it as "resolved" ?
Under thread title menu (thread tools) there is "mark thread resolved" option.
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|