Results 1 to 26 of 26

Thread: [RESOLVED] Stop Flicker of the windows form in visual basic 2010

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Resolved [RESOLVED] Stop Flicker of the windows form in visual basic 2010

    Name:  image_2021-01-12_171857.png
Views: 1256
Size:  95.1 KB


    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.

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Exclamation Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by Delaney View Post
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  9. #9
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    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.

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    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

  12. #12

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    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.

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by boops boops View Post
    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.

  14. #14

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Red face Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by Delaney View Post
    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

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  16. #16

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by passel View Post
    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 ?

  17. #17
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  18. #18
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by lokesh R View Post
    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

  19. #19
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  20. #20
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  21. #21

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by boops boops View Post
    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. . . . >

  22. #22
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.

  23. #23

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by Delaney View Post
    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. . . . >

  24. #24

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by VB.NET Developer View Post
    if your problem is solved, dont forget to mark this thread as resolved.
    Yes, I will !
    < / L R. . . . >

  25. #25

    Thread Starter
    Member
    Join Date
    Jan 2021
    Location
    Kepler-452b
    Posts
    37

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by VB.NET Developer View Post
    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. . . . >

  26. #26
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Stop Flicker of the windows form in visual basic 2010

    Quote Originally Posted by lokesh R View Post
    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
  •  



Click Here to Expand Forum to Full Width