Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Creating PicBoxes - any way to highlight selection?

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [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?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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:
    1. Public Class Form3
    2.  
    3.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         CreatePicBox()
    5.     End Sub
    6.  
    7.     Private Sub CreatePicBox()
    8.         Dim picBox As PictureBox
    9.         Dim x As Integer = 10
    10.         Dim y As Integer = 10
    11.         For i As Integer = 1 To 3
    12.             picBox = New PictureBox
    13.             With picBox
    14.                 .Name = "PictureBox" & i
    15.                 .Size = New Size(40, 40)
    16.                 .Location = New Point(x, y)
    17.                 .BorderStyle = BorderStyle.Fixed3D
    18.                 x += 50  
    19.             End With
    20.             AddHandler picBox.Click, AddressOf picBox_Click
    21.             Me.Controls.Add(picBox)
    22.         Next
    23.     End Sub
    24.  
    25.     Private Sub picBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    26.         'Remove handlers from all picture boxes
    27.         Dim pb As PictureBox
    28.         For Each ctrl As Control In Me.Controls
    29.             If TypeOf ctrl Is PictureBox Then
    30.                 pb = DirectCast(ctrl, PictureBox)
    31.                 RemoveHandler pb.Paint, AddressOf picBox_Paint
    32.                 pb.Invalidate()
    33.             End If
    34.         Next
    35.         'add handler to the clicked picturebox
    36.         pb = DirectCast(sender, PictureBox)
    37.         AddHandler pb.Paint, AddressOf picBox_Paint
    38.         pb.Invalidate()
    39.     End Sub
    40.  
    41.     Private Sub picBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    42.         Dim pb As PictureBox = DirectCast(sender, PictureBox)
    43.         Dim rect As Rectangle = pb.ClientRectangle
    44.         Dim p As Pen = New Pen(Color.Blue, 3)
    45.         e.Graphics.DrawRectangle(p, rect)
    46.     End Sub
    47. End Class

  3. #3

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Creating PicBoxes - any way to highlight selection?

    Thanks Stanav,

    that's a very good start.

    I'll have a play around and see what else I can add. Cheers.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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