binding a variable in a loop
I am new to VB and having a problem.On a form I have 4 picture boxes (named picbox1, picbox2, picbox3, picbox4).From a button I am trying to populate the four pictures.The file browser comes up fine and I can select my files.My problem comes in the loop when I try to populate the picture boxes.
I have a variable named loopphoto.On the first pass I am able to bind loopphoto to picbox1 without any problems.On the second pass of the loop I am unable to rebind loopphoto to picbox2.
Currently I need the variable to loop 1 to 4, passing the respective photos into the respective picture boxes.How do I do this?In the future I will need to change the code to loop 1 to X and create 1 to X picture boxes respectively.
[1127 byte] By [
RumTum] at [2007-12-24]
Can you show some code on what you have tried.
Setting the properies for specific controls in a loop is easily achieved using something similar to the following. The following sets the textbox textbox property and the picturebox borderstyle property for contols Textbox1, Textbox2, Picturebox1, Picturebox2.
When you talk about bind - are you talking databinding or simply setting the picturebox property to load an specified image. Either way you should just be able to add the code within the loop using something like
Me.Controls("Textbox" & i.ToString)
to refer to a specific set of controls - in this case Textbox1
'//Using loop to set properties
For i As Integer = 1 To 2
CType(Me.Controls("Textbox" & i.ToString), TextBox).Text = "test" & i.ToString
CType(Me.Controls("Picturebox" & i.ToString), PictureBox).BorderStyle = i
Next
Thank you that fixed my original problem. I am still stugling with the second part of the problem the 1 to X creating new Picture Boxs. This is my current code.
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles OpenFileDialog1.FileOk
Me.Activate()
Dim file, files() As String
files = OpenFileDialog1.FileNames
Dim Counter1 As Integer = 1
' Open each file and display the image in PictureBox1.
' Call Application.DoEvents to force a repaint after each file is read.
For Each file In files
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(file)
Dim fileStream As System.IO.FileStream = fileInfo.OpenRead()
CType(Me.Controls("PictureBox" & Counter1.ToString),PictureBox).Image _
=System.Drawing.Image.FromStream(fileStream)
Application.DoEvents()
fileStream.Close()
Counter1 = Counter1 + 1
Next
End Sub
How do I word my Dim to create a new picture box?
Dim Counter1 As Integer = 0
Dim pb as picturebox
' Open each file and display the image in PictureBox1.
' Call Application.DoEvents to force a repaint after each file is read.
For Each str as string In files
select case counter1
case 0
pb = picturebox1
case 1
pb = picturebox2
case 2
pb = picturebox3
case 3
pb = picturebox4
end select
pb.image = System.Drawing.Image.FromFile(str)
Counter1 = Counter1 + 1
Next
End Sub
Adding a new PictureBox for each image selected is pretty easy. The thing you have to decide is how the PictureBoxes will be arranged (that is, in a single column top to bottom, or multiple columns, or each on its own page of a TabPage control, etc.)
Here's an example that takes your basic code (simplifies it a little - the extra FileInfo and FileStream aren't necessary) and modifies it to work in the 1 to x scenario. Each picture box is added to the form underneath the pervious one:
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim lastpb As PictureBox = Nothing
For Each file As String In Me.OpenFileDialog1.FileNames
If lastpb Is Nothing Then
Me.PictureBox1.Image = Image.FromFile(file)
lastpb = Me.PictureBox1
Else
Dim pb As New PictureBox
pb.SizeMode = PictureBoxSizeMode.AutoSize
pb.Image = Image.FromFile(file)
pb.Left = lastpb.Left
'puts 4 pixels between each picturebox
pb.Top = lastpb.Top + lastpb.Height + 4
Me.Controls.Add(pb)
lastpb = pb
End If
Next
End If
These pictureboxes are set to AutoSize and the form was set to AutoScroll.
HTH, GL
-EDIT-
This example assumes there is already one PictureBox on the form, all additional PictureBoxes will be created dynamically. You don't have to do it this way. You could start with 0 picture boxes on the form and add them all via code. This example should make that process fairly clear.
Thank you.
My answer was not as clean as yours.