+ Reply to Thread
Results 1 to 3 of 3

Thread: Drawstring string disappears when Button on Form

  1. #1
    albundy is offline VB.NET Forum Newbie albundy is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    May 2009
    Posts
    3
    Reputation
    0

    Default Drawstring string disappears when Button on Form

    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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,911
    Reputation
    670

  3. #3
    albundy is offline VB.NET Forum Newbie albundy is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    May 2009
    Posts
    3
    Reputation
    0

    Default

    Hi,
    meanwhile i found a solution to my problem, with help of this thread:

    Keeping Graphics on the form

    my original code was :
    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
    (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:

    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)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts