[RESOLVED] [2005] Creating PicBoxes - any way to highlight selection?
This is more of a UI question.
When I dynamically create my picBoxes is there any way to show which one the user has clicked on by maybe changing its border colour or something similar to let them know it's currently the one being selected before an action happens?
Re: [2005] Creating PicBoxes - any way to highlight selection?
A quick example to illustrate what you can do to get the selected effect
vb Code:
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreatePicBox()
End Sub
Private Sub CreatePicBox()
Dim picBox As PictureBox
Dim x As Integer = 10
Dim y As Integer = 10
For i As Integer = 1 To 3
picBox = New PictureBox
With picBox
.Name = "PictureBox" & i
.Size = New Size(40, 40)
.Location = New Point(x, y)
.BorderStyle = BorderStyle.Fixed3D
x += 50
End With
AddHandler picBox.Click, AddressOf picBox_Click
Me.Controls.Add(picBox)
Next
End Sub
Private Sub picBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Remove handlers from all picture boxes
Dim pb As PictureBox
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is PictureBox Then
pb = DirectCast(ctrl, PictureBox)
RemoveHandler pb.Paint, AddressOf picBox_Paint
pb.Invalidate()
End If
Next
'add handler to the clicked picturebox
pb = DirectCast(sender, PictureBox)
AddHandler pb.Paint, AddressOf picBox_Paint
pb.Invalidate()
End Sub
Private Sub picBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim rect As Rectangle = pb.ClientRectangle
Dim p As Pen = New Pen(Color.Blue, 3)
e.Graphics.DrawRectangle(p, rect)
End Sub
End Class
Re: [2005] Creating PicBoxes - any way to highlight selection?
Thanks Stanav,
that's a very good start. :thumb:
I'll have a play around and see what else I can add. Cheers.