1

I am (an amateur) trying to convert a VBA Outlook macro into VB.net. It basically works, but I have problems with flickering screens when forms are closed or redrawn. The first of these problems is this (simplified version)…

I have Form1 with Listview0, Listview1, Listview2 and Button0 on it. The listviews are set to Details view. Initially, only LV0 and LV1 are visible. Each of them has about 20 rows of strings in them.

The form is loaded using .Showdialog. The user clicks in LV1, whereupon LV2 may or may not be made visible, depending on which row of LV1 was clicked. Irrespective of whether LV2 is shown or not, Form1 has both its Width and its Height changed.

And that last fact seems to be the problem. It immediately causes the interiors of LV0 and LV1 to go entirely white (so the gridlines disappear, as well as the contents). The code runs its course and, presumably when Form1 is repainted (but I don’t know this for sure), the interiors of LV0 and LV1 reappear. The time between their disappearance and reappearance is enough to create the flickering effect.

This does not happen in the VBA macro version. Note that it also does not happen in the VB.net version if you change just one of the dimensions of Form1 (ie just the width or just the height). It occurs only when you change both.

I have tried putting the following in my Form1 class (from here: How to prevent WinForms app from flickering)

Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H2000000
            Return cp
        End Get
End Property 

This reduces the flickering when both dimensions are changed, but results in a bit of flickering when only one dimension is changed, which occurs a lot more often than both dimensions changing, so isn’t a solution.

I have also set Form1.Doublebuffered to True, but that doesn’t seem to do anything.

Any suggestions gratefully received.

AR43
  • 11
  • 1
  • 1
    https://stackoverflow.com/a/8378761/17034 – Hans Passant Mar 06 '21 at 14:33
  • @HansPassant, thank you so much - seems to work perfectly. I didn't know if I could somehow use that code alongside my VB, but managed to translate it into VB well enough to make it work. I had seen your name in connection with other flickering questions, but didn't find that solution. Apologies you had to direct me to it. – AR43 Mar 07 '21 at 22:16

0 Answers0