Can you show us your Paint event handler?
|
|
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
Can you show us your Paint event handler?
2007, 2008, 2009, 2010
Why is my data not saved to my database? | Communicating between multiple forms
MSDN: Data Walkthroughs | "How Do I?" Videos
My Blog: Custom Events | Dynamic GDI+ Drawing
Hi,
meanwhile i found a solution to my problem, with help of this thread:
Keeping Graphics on the form
my original code was :
(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?)Code: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
then i noticed, i have two Graphics objects "w", and "e"
and changed it like this:
Code:' 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)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks