Drawstring string disappears when Button on Form

albundy

New member
Joined
May 2, 2009
Messages
3
Programming Experience
3-5
Hi,

I'm drawing a game board with GDI in VB2005. (it's like a gomoku board)
First i draw a background with FillRectangle, then I draw the grid with
DrawLine, then I draw some row and column letters with DrawString.
They are all in the Paint event of the Form.

Everything works fine, until i put some Buttons on the Form (not at runtime).
Then the column and row letters go behind the background rectangle (so they can't be seen :)), when the form is shown. The grid remains on top of the background rectangle.

I would like the letters to stay on top of the other graphics.
Thanks in advance for the help
 
Hi,
meanwhile i found a solution to my problem, with help of this thread:

http://www.vbdotnetforums.com/graphics-gdi/33247-keeping-graphics-form.html

my original code was :
VB.NET:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        ' grid back---------------------------------------------
        Dim w As Graphics = Me.CreateGraphics
        w.FillRectangle(Brushes.Beige, 20, 20, 20 + 390, 20 + 390)
        w.Dispose()


        ' letters-----------------------------------------------
        Dim fontObj As Font
        fontObj = New System.Drawing.Font("Times", 10, FontStyle.Bold)
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Far
        'stringFormat.LineAlignment = StringAlignment.Near
        Dim i As Integer
        Dim f As Integer
        Dim mystring1 = New String() {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"}
        Dim mystring2 = New String() {"19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"}
        For i = 0 To 18
            e.Graphics.DrawString(mystring1(i), fontObj, Brushes.Chocolate, 45 + i * 20, 410)
        Next
        For f = 0 To 18
            e.Graphics.DrawString(mystring2(f), fontObj, Brushes.Chocolate, 40, 35 + f * 20, stringFormat)
        Next
    End Sub
    'Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    'End Sub
End Class

(when i put a button on the form, or resized it, the letters disappeared, i guess a new rectangle was drawn on top of all graphics, but no new letters?)
then i noticed, i have two Graphics objects "w", and "e"
and changed it like this:

VB.NET:
' grid back---------------------------------------------
        e.Graphics.FillRectangle(Brushes.Beige, 20, 20, 20 + 390, 20 + 390)


now it works fine, but i don't understand why in the first version only the rectangle is redrawn on form resize (or putting a button on the form)
 
Back
Top