-
May 1st, 2021, 10:54 AM
#1
Thread Starter
New Member
[RESOLVED] Apply text to PictureBoxes
Using : Microsoft Visual Studio Community 2019 - Version 16.9.4
I have a problem adding text to PictureBoxes in my application. The Form contains a grid formed by PictureBoxes inside a GroupBox. When a PictureBox is clicked, the background colour of that PictureBox is changed. Currently I have the grid reference of the PictureBox (e.g. 0-3) written to each PictureBox. This will change in later development.
My Form design only contains a single button 'btnClear' to change the background colour to white on all PictureBoxes.
The problems are :
- The text is not immediately visible. If the Form is moved a lot or dragged off the screen and back the text will appear.
- When the text does appear it can look thick and blurry as if the text has been redrawn a number of times.
- When a PictureBox is clicked, the background colour is changed. This removes the text from the PictureBox.
- When 'btnClear' is clicked the background colour of all PictureBoxes is changed to white. This removes the text on all PictureBoxes.
From what I have found looking into this it seems I need to use System.Windows.Forms.PaintEventArgs, but I am not sure how and where to implement that in my app.
Also, how can the colour of the text be changed on a PictureBox in a PictureBox_Click event?
There is another thing I would like help with. I could not find a way to loop through all PictureBoxes in the GroupBox directly from a Sub so I found a workaround - see Subs UpdateBoxText and btnClear_Click. My workaround loops through all Form Controls, and if it is a GroupBox, it then loops through all ChildControls (in this case PictureBoxes). It works, but it seams the wrong way to go about it. Also, I plan on using another GroupBox containing PictureBoxes to the same form in later development which will break my workaround. Can someone show me a better way to do this please?
Can you help make the text persistent, change the colour of the text on a single PictureBox on click event, and advise a better way to loop through my PictureBoxes please?
Thanks for taking the time to look at this.
My code is below:
Code:
Public Class Form1
Private Sub UpdateBoxText()
Dim PBx As Integer
Dim PBy As Integer
Dim PBTextGraphics As Graphics
Dim PBTextString As String = ""
For Each Control As Control In Me.Controls 'Loop through all controls in Form1
If TypeOf Control Is GroupBox Then 'If control is GroupBox
For Each ChildControl As Control In Control.Controls 'Loop through controls within GroupBox (PictureBoxes)
PBx = ChildControl.Location.X \ ChildControl.Size.Width 'Get current PictureBox grid reference X
PBy = ChildControl.Location.Y \ ChildControl.Size.Height 'Get current PictureBox grid reference Y
PBTextString = PBx & "-" & PBy 'Create text string to display in PictureBox
'Draw text string to current PictureBox
PBTextGraphics = ChildControl.CreateGraphics
PBTextGraphics.DrawString(PBTextString, New Font("Arial", 10), New SolidBrush(Color.Black), 3, 3)
PBTextGraphics.Dispose()
Next
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Paint, MyBase.Load
' Create GroupBox
Dim GBox As New GroupBox
GBox.Location = New Point(13, 6)
GBox.Width = 255
GBox.Height = 262
GBox.BackColor = Color.White
Me.Controls.Add(GBox)
' Create grid of picture boxes and add to groupbox
For PBRow As Integer = 0 To 4
For PBCol As Integer = 0 To 4
Dim PBox As New PictureBox
PBox.BorderStyle = BorderStyle.FixedSingle
PBox.BackColor = Color.White
PBox.Size = New Size(50, 50)
PBox.Location = New Point((PBCol * PBox.Size.Width) + 2, (PBRow * PBox.Size.Height) + 9)
AddHandler PBox.Click, AddressOf PictureBox_Click
GBox.Controls.Add(PBox)
Next
Next
UpdateBoxText()
End Sub
Private Sub PictureBox_Click(sender As Object, e As EventArgs)
Dim PBox = DirectCast(sender, PictureBox)
PBox.BackColor = Color.LightCyan 'Colour clicked PictureBox
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
For Each Control As Control In Me.Controls 'Loop through all controls in Form1
If TypeOf Control Is GroupBox Then 'If control is GroupBox
For Each ChildControl As Control In Control.Controls 'Loop through controls within GroupBox (PictureBoxes)
ChildControl.BackColor = Color.FromArgb(255, 255, 255) 'Set PictureBox background colour to white
Next
End If
Next
End Sub
End Class
-
May 1st, 2021, 11:29 AM
#2
Re: Apply text to PictureBoxes
You need to write the text using e.graphics in the picturebox’ paint event, if you really are drawing text on a PictureBox. Looks to me that you’re using textboxes above pictureboxes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 1st, 2021, 02:02 PM
#3
Thread Starter
New Member
Re: Apply text to PictureBoxes
Originally Posted by .paul.
You need to write the text using e.graphics in the picturebox’ paint event, if you really are drawing text on a PictureBox. Looks to me that you’re using textboxes above pictureboxes
If it looks like I am using textboxes, then it is only due to my bad attempt at implementing this.
I've commented out the UpdateBoxText Sub and tried to create a paint event for my pictureboxes :
Code:
Private Sub PBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim PBTextString As String = "ABC"
e.Graphics.DrawString(PBTextString, New Font("Arial", 10), New SolidBrush(Color.Black), 3, 3)
e.Graphics.Dispose()
End Sub
I get a letter 'A' only displayed outside of my group box in the top left corner. I assume it is using the '3, 3' co-ordinates in context with the form for this.
I'll keep trying. Any further hints would be appreciated in the meantime.
-
May 1st, 2021, 02:19 PM
#4
Re: Apply text to PictureBoxes
???
Code:
For Each Control As Control In Me.Controls 'Loop through all controls in Form1
If TypeOf Control Is GroupBox Then 'If control is GroupBox
For Each ChildControl As Control In Control.Controls 'Loop through controls within GroupBox (PictureBoxes)
PBx = ChildControl.Location.X \ ChildControl.Size.Width 'Get current PictureBox grid reference X
PBy = ChildControl.Location.Y \ ChildControl.Size.Height 'Get current PictureBox grid reference Y
ChildControl.Tag = PBx & "-" & PBy 'Create text string to display in PictureBox
AddHandler DirectCast(ChildControl, PictureBox).Paint, Addressof PictureBoxes_Paint
Next
End If
Next
Code:
Private Sub PictureBoxes_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString(sender.Tag.ToString, New Font("Arial", 10), New SolidBrush(Color.Black), 3, 3)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 1st, 2021, 02:31 PM
#5
Re: Apply text to PictureBoxes
Originally Posted by BlueCheese
Code:
Private Sub PBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim PBTextString As String = "ABC"
e.Graphics.DrawString(PBTextString, New Font("Arial", 10), New SolidBrush(Color.Black), 3, 3)
e.Graphics.Dispose()
End Sub
I get a letter 'A' only displayed outside of my group box in the top left corner. I assume it is using the '3, 3' co-ordinates in context with the form for this.
It's drawing on the form - behind the controls
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 1st, 2021, 04:10 PM
#6
Thread Starter
New Member
Re: Apply text to PictureBoxes
Originally Posted by .paul.
???
Code:
For Each Control As Control In Me.Controls 'Loop through all controls in Form1
If TypeOf Control Is GroupBox Then 'If control is GroupBox
For Each ChildControl As Control In Control.Controls 'Loop through controls within GroupBox (PictureBoxes)
PBx = ChildControl.Location.X \ ChildControl.Size.Width 'Get current PictureBox grid reference X
PBy = ChildControl.Location.Y \ ChildControl.Size.Height 'Get current PictureBox grid reference Y
ChildControl.Tag = PBx & "-" & PBy 'Create text string to display in PictureBox
AddHandler DirectCast(ChildControl, PictureBox).Paint, Addressof PictureBoxes_Paint
Next
End If
Next
Code:
Private Sub PictureBoxes_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString(sender.Tag.ToString, New Font("Arial", 10), New SolidBrush(Color.Black), 3, 3)
End Sub
Thank you Paul for your help. My biggest issues are resolved now.
-
May 2nd, 2021, 03:08 PM
#7
Thread Starter
New Member
Re: Apply text to PictureBoxes
The text issue is resolved.
By removing the code to create the GroupBox and adding a GroupBox to the Form design instead, I was able to loop through my PictureBoxes the way I intended.
Code :
Code:
Public Class Form1
Private Sub PictureBoxes_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim PBox = DirectCast(sender, PictureBox)
Dim PBFont As New Font("Arial", 10)
Dim PBString As String = sender.Tag.ToString
If PBox.BackColor.Equals(Color.White) Then : e.Graphics.DrawString(PBString, PBFont, New SolidBrush(Color.Gray), 2, 2) 'Draw gray text to PictureBox with white BackColor
Else : e.Graphics.DrawString(PBString, PBFont, New SolidBrush(Color.Black), 2, 2) 'Draw black text to PictureBox with any other BackColor
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Paint, MyBase.Load
' Create grid of PictureBoxes and add to GroupBox
For PBRow As Integer = 0 To 4
For PBCol As Integer = 0 To 4
Dim PBox As New PictureBox
PBox.BorderStyle = BorderStyle.FixedSingle
PBox.BackColor = Color.White
PBox.Size = New Size(50, 50)
PBox.Location = New Point((PBCol * PBox.Size.Width) + 2, (PBRow * PBox.Size.Height) + 9)
PBox.Tag = PBRow & "-" & PBCol 'Add grid reference to PictureBox Tag
AddHandler PBox.Click, AddressOf PictureBox_Click
AddHandler DirectCast(PBox, PictureBox).Paint, AddressOf PictureBoxes_Paint
GBox.Controls.Add(PBox) 'Add PictureBox to GroupBox
Next
Next
End Sub
Private Sub PictureBox_Click(sender As Object, e As EventArgs)
Dim PBox = DirectCast(sender, PictureBox)
PBox.BackColor = Color.LightCyan 'Colour clicked PictureBox
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Loop though all PictureBoxes in GroupBox and change BackColor to white
For Each PBox As PictureBox In Me.GBox.Controls.OfType(Of PictureBox)()
PBox.BackColor = Color.White
Next
End Sub
End Class
Last edited by BlueCheese; May 2nd, 2021 at 04:09 PM.
-
May 2nd, 2021, 03:17 PM
#8
Re: Apply text to PictureBoxes
As PBox Is already a PictureBox, there is no need for the DirectCast in the AddHandler line. You can just use...
PBox.Paint, as you did PBox.Click for the click Addhandler line
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 2nd, 2021, 03:21 PM
#9
Re: Apply text to PictureBoxes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 3rd, 2021, 10:23 AM
#10
Thread Starter
New Member
Re: Apply text to PictureBoxes
I see. I've amended my code. Thanks again.
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
|