Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 14983

VS 2019 Apply text to PictureBoxes

$
0
0
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


Viewing all articles
Browse latest Browse all 14983

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>