-
Jul 28th, 2024, 02:29 PM
#1
Thread Starter
New Member
[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.
-
Jul 28th, 2024, 03:01 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2024, 03:03 PM
#3
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 28th, 2024, 10:33 PM
#4
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.
-
Jul 28th, 2024, 10:40 PM
#5
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.
-
Jul 29th, 2024, 02:42 AM
#6
Re: Attempting to draw text in a Graphic window on a form
There’s an error in there…
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 29th, 2024, 04:47 AM
#7
Re: Attempting to draw text in a Graphic window on a form
Originally Posted by .paul.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|