Hi

I Have a form that contains around 30 Labels and 30 Pictureboxes.

I have a piece of code that on form load that steps through the labels and changes the text to a file name from a directory.

This all works fairly well however:

I need to be able to get the picture box next to it to be the image of that file.

EG.

For RELREF = 1 to 30 step 1
picturebox(RELREF).backgroundimage=image.fromfile("c\-\desktop\" & label(RELREF).text)
RELREF = RELREF + 1
Next RELREF

Is this possible and can it be done without writting the code version of war and peace.

In short i need picturebox(1) background image to be label(1) filename....

Recommended Answers

All 5 Replies

See if this helps.

Public Class Form1
    Private myImagesFolder As String = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myImagesFolder = "C:\TEMP\" '// your Folder Path.
        For RELREF As Integer = 1 To 30 '// Loop from 1 to 30.
            For Each pb As Control In Me.Controls '// Loop thru all Controls in Form1.
                If TypeOf (pb) Is PictureBox AndAlso pb.Name = "PictureBox" & RELREF Then '// check if Control is PictureBox and Name is "=" to...
                    For Each lbl As Control In Me.Controls '// Loop thru all Controls Again to locate Labels.
                        If TypeOf (lbl) Is Label AndAlso lbl.Name = "Label" & RELREF Then '// check if Control is Label and Name is "=" to...
                            pb.BackgroundImage = Image.FromFile(myImagesFolder & lbl.Text) '// Add Image.
                            Exit For '// Exit Loop once locating Label that Name .EndsWith # as PictureBox.
                        End If
                    Next
                    Exit For '// Exit Loop once locating PictureBox that Name .EndsWith # as RELREF.
                End If
            Next
        Next
    End Sub
End Class

codeorder

Thanks for that i will give it a try straight away.

Glad to see that i wasn't a million miles away with my thinking.

codeorder

i seem to be getting an out of memory message
saying unhandled.

any suggestions - should i gc.collect after each correctly linked label and box ??

and this was a great help and i will close this thread once done.
I will also review and close other threads .

I believe only my first thread is ready to close at this time.

Please post code that causes this new issue.

codeorder.

have sorted the code loop with a file checker that seems to control the unhandled error.

For RELREF = 1 To 30 '// Loop from 1 to 30.

For Each pb As Control In GroupBox4.Controls '// Loop thru all Controls in Form1.
If TypeOf (pb) Is PictureBox AndAlso pb.Name = "LiveImage" & RELREF Then '// check if Control is PictureBox and Name is "=" to...
For Each lbl As Control In GroupBox3.Controls '// Loop thru all Controls Again to locate Labels.
If TypeOf (lbl) Is Label AndAlso lbl.Name = "LiveId" & RELREF Then '// check if Control is Label and Name is "=" to...
If System.IO.File.Exists(PATH & lbl.Text & IDENTFILE) Then
Dim objReader0 As New System.IO.StreamReader(PATH & lbl.Text & IDENT)
Dim Chosen = PATH & lbl.Text & IDENT & objReader0.ReadToEnd
If System.IO.File.Exists(Chosen) Then
pb.BackgroundImage = Image.FromFile(Chosen) '// Add Image.
pb.BackgroundImageLayout = ImageLayout.Stretch
End If
End If
Exit For '// Exit Loop once locating Label that Name .EndsWith # as PictureBox.
End If
Next
Exit For '// Exit Loop once locating PictureBox that Name .EndsWith # as RELREF.
End If
Next
Next

so i thank you kindly for the assistance and look forward to hearing from you again on any other threads i may post.


thank you so much once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.