question

AMERSAID-7084 avatar image
0 Votes"
AMERSAID-7084 asked karenpayneoregon edited

find control in FlowLayoutPanel

hi

I added PictureBox to the FlowLayoutPanel Each image takes a value from the datatable by tag = id column. What I want is to choose the image that has the number that I specify

my code Does not select any image

For Each item As Control In FlowLayoutPanel.Controls

         If TypeOf item Is PictureBox Then
             If item.Tag = "3" Then
                 item.BackgroundImage = Nothing
                 item.Select()
             End If
         End If
     Next
dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AMERSAID-7084 ,

my code Does not select any image

Can you successfully set 'BackgroundImage' property to 'Nothing'?

0 Votes 0 ·

hi
XingyuZhao-MSFT


Can you successfully set 'BackgroundImage' property to 'Nothing'? no


What I do is to click on the image. I add another image and update its description and it appears in a group of 20-30 images inside the tool.
Of course I added the click event and everything is fine.
But after completion,
- I want to select the image that you updated from within the visible group, so the goal is not to change the image, but rather you must select it or focus or shade it to see the change because it must appear again within 20-30 images

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon edited

Hello,

I would use a custom PictureBox so there is no need to cast.

 Public Class MyPictureBox
     Inherits PictureBox
     ''' <summary>
     ''' Primary key from database or DataTable
     ''' </summary>
     ''' <returns></returns>
     Public Property PrimaryKey() As Integer
    
 End Class

Simple demo to create one (and you can get it from the toolbox rather than hand code it)

 Dim myBox = New MyPictureBox
 myBox.PrimaryKey = 3

Find by primary key which is hard code here but the 3 can come from where ever. I check to see if it's found then for this simple example remove the background image.

 Dim pb = FlowLayoutPanel1.Controls.OfType(Of MyPictureBox).FirstOrDefault(Function(pbox) pbox.PrimaryKey = 3)
 If pb IsNot Nothing Then
     pb.BackgroundImage = Nothing
 End If

Second code sample


 Public Class Form1
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Dim pb = FlowLayoutPanel1.Controls.OfType(Of MyPictureBox).FirstOrDefault(Function(pbox) pbox.PrimaryKey = 3)
         If pb IsNot Nothing Then
             Text = $"From button click event: {pb.PrimaryKey}"
         End If
     End Sub
    
     Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
         FlowLayoutPanel1.Controls.OfType(Of MyPictureBox).ToList().ForEach(
             Sub(pb)
                 AddHandler pb.Click, AddressOf MyPictureBox_Click
             End Sub)
     End Sub
    
     Private Sub MyPictureBox_Click(sender As Object, e As EventArgs)
         Dim pb As MyPictureBox = CType(sender, MyPictureBox)
    
         Text = $"From Picture click event: {pb.PrimaryKey}"
    
         If pb.BorderStyle = BorderStyle.None Then
             pb.BorderStyle = BorderStyle.Fixed3D
         Else
             pb.BorderStyle = BorderStyle.None
         End If
    
     End Sub
 End Class



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.