Results 1 to 7 of 7

Thread: [RESOLVED] Attempting to draw text in a Graphic window on a form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    7

    Resolved [RESOLVED] Attempting to draw text in a Graphic window on a form

    Hello everyone.
    I found this code snippet that is supposed to display a string in a graphic window.

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myGraphics As Graphics = Me.CreateGraphics
    Dim myFont As Font
    Dim myBrush As Brush
    myBrush = New Drawing.SolidBrush(Color.DarkOrchid)
    myFont = New System.Drawing.Font("Verdana", 20, FontStyle.Underline)
    myGraphics.DrawString("Visual Basic 2022", myFont, myBrush, 10, 10)
    End Sub

    When I typed it in the area for a button click event, there were two phrases that were flagged:
    the first was "me.CreateGraphics"
    the second was "my.Graphics.Drawstring".
    The system stated that "drawstring" was not a member of myGraphics.
    Does someone have a code snippet that will allow me to draw text in a Graphic window on a form.
    Thanks in advance.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: Attempting to draw text in a Graphic window on a form

    In your Button_Click event, remove all of your code and put Me.Refresh, then add this code…

    Code:
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim myFont As Font = New Font("Verdana", 20, FontStyle.Underline)
        Dim myBrush As Brush = New SolidBrush(Color.DarkOrchid)
        e.Graphics.DrawString("Visual Basic 2022", myFont, myBrush, 10, 10)
    End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: Attempting to draw text in a Graphic window on a form

    That code will draw on your form, but if you have controls at that location, they’ll obscure it…

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: Attempting to draw text in a Graphic window on a form

    DrawString is a member of the System.Drawing.Graphics class, so my best guess as to why you got that error message is that you have defined your own type with the name Graphics and your use of that type name in that code is being interpreted as that type instead. There are a number of ways to test that. You could qualify the type name, i.e. use System.Drawing.Graphics instead of just Grpahics or you could right-click on the type name and navigate to the definition to see what type that is. If you do have a name clash, qualifying one or both types in code is your only real option to get around that, but you should probably change the name of your type to avoid the clash at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: Attempting to draw text in a Graphic window on a form

    As for the code you wrote, there's a lot wrong with it. Firstly, while you don't strictly have to do so, you should always dispose any objects that support it that you create. You're creating Graphics, Font and SolidBrush objects there, all of which can and should be disposed. You're also making your code harder to read by separating the declaration and initialisation of the variables. The code you have should be written like this:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using myGraphics As Graphics = CreateGraphics(),
              myFont As New Font("Verdana", 20, FontStyle.Underline),
              myBrush As New Drawing.SolidBrush(Color.DarkOrchid)
            myGraphics.DrawString("Visual Basic 2022", myFont, myBrush, 10, 10)
        End Using
    End Sub
    By creating an object with a Using statement, you ensure that that object will be disposed at the end of that block.

    That said, you shouldn't be doing it like that. As .paul. alluded to, you should pretty much NEVER call CreateGraphics. ALWAYS draw on a control on its Paint event. That's because some or all of the control surface will be erased and repainted each time that event is raised. If you don't execute your drawing code on that event, your drawing will disappear on that event, e.g. when you minimise and restore the form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,656

    Re: Attempting to draw text in a Graphic window on a form

    There’s an error in there…

    Quote Originally Posted by jmcilhinney
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using myGraphics As Graphics = CreateGraphics(),
              myFont As New Font("Verdana", 20, FontStyle.Underline),
              myBrush As New Drawing.SolidBrush(Color.DarkOrchid)
            myGraphics.DrawString("Visual Basic 2022", myFont, myBrush, 10, 10)
        End Using
    End Sub

    Edit, please disregard my post. The error was mine.
    As jmcilhinney says , I alluded to not using creategraphics. I would’ve said more, but the site was being difficult..
    Last edited by .paul.; Jul 29th, 2024 at 02:49 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,663

    Re: Attempting to draw text in a Graphic window on a form

    Quote Originally Posted by .paul. View Post
    There’s an error in there…




    Edit, please disregard my post. The error was mine.
    As jmcilhinney says , I alluded to not using creategraphics. I would’ve said more, but the site was being difficult..
    I'm guessing that you missed the commas that made those three lines all part of the same Using statement. When I read your quote of my post, I did too. I thought "why did I omit the Dim on those two lines". I need a nap.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width