Question Potential form refresh problem

robtyketto

Member
Joined
Jul 23, 2009
Messages
23
Programming Experience
Beginner
Greetings,

I have a form with a background image set.
A panel containing FIVE picture boxes aka the dice
Another panel containing 17 labels aka the score categories

The picture below is a better representation than my muddled words.
onpaintissue.png


When I click the button each picture box is assigned an image (randomly) from an Imagelist and the labels.text property is updated for the labels in the other panel.

Im a newbie to vb.net and really out of depth when it comes to graphics as never stray from the built in controls or automatic form repainting.

When I press the button the form flickers/white flash which I believe might be a form refresh problem.

I believe the onpaint even would be called to redraw all the graphics when ever a property or method is called for a control.

What steps can I take when updating so many items to keep the form redrawing cleanly?

Any advice and tips would be appreciated.

I want to learn more about graphics and once I have the knowledge I would like to be able to change the border style or draw around the border region to increase the thickness of the black line.

As seen in the pic 2 of the dice are highlighted via the borderstyle property which isn't quite as obvious as I would like.

Also if anyone has some beginners threads/tutorials about graphics to learn from that would be good too.

Oh, how do you resolve a thread?

Sorry for ALL the questions in one post!!

Cheers
Rob
 
Most likely you're looking for Double Buffering.

You can activate it for your form by easily set the property.

Bobby

Thanks for the reply.
Found the propery and set it.

It has helped but the flicker occurs albeit less frequently.

On another note I didn't say that all the controls have the transparency set where allowed (see diagram below).

transparentcontrols.png


Now doing some further reading on double buffering.
Don't want to manually do it for controls if I don't need to, seems complicated.
 
Last edited:
Well I read that double buffering on the form may not work within items in a panel.

So I made my own panel, which wasn't as painful as I thought.

VB.NET:
Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.UpdateStyles()

End Sub


Add this
VB.NET:
ControlStyles.AllPaintingInWmPaint
to the user control.

All is fine, can anyone tell me how to resolve the thread?

Also a big thank you Bobby without your help I wouldn't have found a solution, cheers!
 
Last edited:
Hi Rob,

Here's just a few ideas and comments.

1. Try setting the background images of the dice pictureboxes to a die image in the designer, instead of starting "empty". The image could be a blank die, or one of your existing dice. Maybe that will stop the flickering.

2. Don't make controls transparent if it isn't really necessary. Transparency is computationally expensive and slows down the refresh.

3. You can set Me.DoubleBuffered=True for a UserControl, instead of using SetStyles. I agree, panels are a pain when it comes to changing images.

4. To draw a thicker border round a picture box, put something like this in the picture box's Paint event handler.

VB.NET:
 Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Using pn As New Pen(Color.Red, 6)
            e.Graphics.DrawRectangle(pn, PictureBox1.Bounds)
        End Using
    End Sub
Obviously, you can set the colour and thickness of pen pn as you wish.

5. I've no idea what you mean by "resolve a thread". Could you explain, or post some code to show what you mean?

regards, Vic

EDIT: I've had to rethink point 4, about showing a thick border. Obviously, you need to test if each picture box is highlighted, and you need the same action for each picture box. Since you already have a way to give the selected picture boxes a border, you could test the borderstyle in the paint event handler like this:

VB.NET:
   Private Sub AllPictureBoxes_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
       Handles PictureBox1.Paint, PictureBox2.Paint, PictureBox3.Paint, PictureBox4.Paint, PictureBox5.Paint
        Dim pb As PictureBox = CType(sender, PictureBox)
        If pb.BorderStyle = BorderStyle.FixedSingle Then
            Using pen As New Pen(Color.Red, 6)
                e.Graphics.DrawRectangle(pen, pb.ClientRectangle)
            End Using
        End If
    End Sub
Note that I've added all the picture boxes to the Handles clause. That's just to keep the explanation short. Another way would be to make your own control which inherits from PictureBox, give it a Selected property, and program its OnPaint Sub to show a thick border when selected.
 
Last edited:
Thanks for all the tips, apologies for not replying sooner.

I will experiment now and change my user panel control to work without setstyle.
I also did my own user control picturebox control too.

VB.NET:
If Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle Then

            MyBase.OnPaint(pe)

            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, _
            Color.Black, 3, ButtonBorderStyle.Solid, Color.Black, 3, _
            ButtonBorderStyle.Solid, Color.Black, 3, ButtonBorderStyle.Solid, _
            Color.Black, 3, ButtonBorderStyle.Solid)

End If

By resolving the thread, I meant for this thread to be marked 'resolved' or similar as I now have a number of solutions to use, so it no longer requires answering.
 
Last edited:
Back
Top