Learning - displaying selection in pictureboxes

Shaggycat

Member
Joined
Mar 22, 2006
Messages
8
Programming Experience
Beginner
Problem trying to figure this out, using a combo box selection I need to go to each folder, Cam 1, Cam 2, Cam 4, Cam 6, Cam 7,and Cam 8 and display each picture (from selection) from each folder and display in pictureboxes pcbCam1, pcbCam2, pcbCam4, pcbCam6, pcbCam7, and pcbCam8.

So far the code works for one picture box not the others. Also when I reenter a different date the counter (+1) messes up....HELP



Dim dt As DateTime = DateTime.ParseExact(cmbDate.Items(cmbDate.SelectedIndex), "MMMM d, yyyy", Nothing)
Dim folder As String = "C:\Projects\Dar\Queue Review Files\" & dt.Month & "-" & dt.Day & "\Cam " & Me.cmbDate.SelectedIndex + 1

Dim fileName As String = folder & "\" & Me.cmbTime.Text

Me.pcbCam1.Image = Image.FromFile(fileName)
 
You're specifying the folder by the SelectedIndex of the ComboBox, which means checking only one folder. If you want to use a loop then you'll need the PictureBoxes in an array so you can refer to them by index, plus you'll need to change the name of the folder each iteration:
VB.NET:
Dim pictures As PictureBox() = {Me.pcbCam1, Me.pcbCam2, Me.pcbCam3, Me.pcbCam4, Me.pcbCam5, Me.pcbCam6, Me.pcbCam7, Me.pcbCam8}
Dim dt As DateTime = DateTime.ParseExact(cmbDate.Items(cmbDate.Selected  Index), "MMMM d, yyyy", Nothing)
Dim folderBase As String = String.Format("C:\Projects\Dar\Queue Review Files\{0}-{1}\Cam ", dt.Month, dt.Day)
Dim fileName As String = Me.cmbTime.GetItemText(Me.cmbTime.SelectedItem)

For folderNum As Integer = 1 to 8
    pictures(i - 1).Image = Image.FromFile(String.Concat(folderBase, folderNum, "\", fileName))
Next folderNum
 
combo box and picture box problem

The problem is I don't have a folder called, Cam 3 or Cam 5 and it ends at Cam 8.

Thanks for helping I REALLY appreciate it!!!

<code>
Dim pictures As PictureBox() = {Me.pcbCam1, Me.pcbCam2, Me.pcbCam4, Me.pcbCam6, Me.pcbCam7, Me.pcbCam8}
Dim dt As DateTime = DateTime.ParseExact(cmbDate.Items(cmbDate.SelectedIndex), "MMMM d, yyyy", Nothing)
Dim folderBase As String = String.Format("C:\Projects\Darryl\Queue Review Files\{0}-{1}\Cam ", dt.Month, dt.Day)
Dim fileName As String = Me.cmbTime.GetItemText(Me.cmbTime.SelectedItem)
For folderNum As Integer = 1 To 8
pictures(folderNum - 1).Image = Image.FromFile(
String.Concat(folderBase, folderNum, "\", fileName))
Next folderNum
</code>
 
You could put the numbers that you do have in an array and then iterate over the elements of that array:
VB.NET:
Dim folderNumbers As Integer = {1, 2, 4, 6, 7, 8}

For i As Integer = 0 To folderNumbers.GetUpperBound(0) Step 1
    MessageBox.Show(folderNumbers(i).ToString())
Next i
This is the way you'd get access to the correct numbers in the correct order. You could then use folderNumbers(i) instead of i as the way to specify the folder.
 
Thanks for helping. That is a good idea, thanks for your input. This is what I have now and it works
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dt [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DateTime = DateTime.ParseExact(cmbDate.Items(cmbDate.SelectedIndex), [/SIZE][SIZE=2][COLOR=#800000]"MMMM d, yyyy"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'loads with the default folder files Cam 1
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] path [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"C:\Projects\Dar\Queue Review Files\"[/COLOR][/SIZE][SIZE=2] & dt.Month & [/SIZE][SIZE=2][COLOR=#800000]"-"[/COLOR][/SIZE][SIZE=2] & dt.Day & [/SIZE][SIZE=2][COLOR=#800000]"\Cam 1"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dir [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.DirectoryInfo(path)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] files [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.FileInfo() = dir.GetFiles([/SIZE][SIZE=2][COLOR=#800000]"*.jpg"[/COLOR][/SIZE][SIZE=2])
cmbTime.BeginUpdate()
cmbTime.Items.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] files.Length > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]cmbTime.Items.AddRange(files)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]cmbTime.EndUpdate()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show(ex.Message, [/SIZE][SIZE=2][COLOR=#800000]"Error Querying Files"[/COLOR][/SIZE][SIZE=2], MessageBoxButtons.OK, MessageBoxIcon.Error)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]
 
Back
Top