I need help with a picture box problem

L

Larry

I've set up a list of image filesnames in a listview control(lvSrcFileList),
I'm using the selectedIndexChange event to select which file is being
displayed in a picturebox control(pbImagePreview). The first time a filename
is selected it works fine but if I try to select another filename the
program bombs with an exception error; flagging the line below "curntImage =
Image.FromFile(cpath). The listview is set for a single selection.
curntImage is declared global to the class as type image and curntSrcDir is
global and of type string. What I've found is that when I click on the
second item in the listview the event occurs but the selected items
collection acts as if it's empty. That isn't what I would expect and I not
sure why or what to do about it.

Here's the code I'm using:
Private Sub lvSrcFileList_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lvSrcFileList.SelectedIndexChanged

' show a preview of the image file selected
Dim path, cpath As String
path = curntSrcDir & "\"
Dim it As ListViewItem
For Each it In Me.lvSrcFileList.SelectedItems
cpath = path & it.Text
Next
curntImage = Image.FromFile(cpath)
Me.pbImagePreview.Image = curntImage
Me.pbImagePreview.Show()
End Sub



-Larry
 
C

Cor

Hi Larry,

You say there is an exception can you tell us what it says, maybe that we
with that can help you finding your problem? (There are some strange things
in the code, but I would not say it could not work, assuming the first
column of your listview is the filename (are there more columns (subitems)
by the way?).

Cor
 
L

Larry

The exception was one of those wonderfully vage exceptions, something about
an "undefined operation". After some more probing at the code I have
determined that the problem was that the indexchanged event is actually
called twice when you move to a different item in the listview; once for
removing the original selection and a second time for seleting the "new"
selection. To me this was an unexpected behavior for the listview component
because in asp.net I beliieve I would get only one call to the change index
event for the list view component for this operation. My code below would
bomb because as on the first call to the event when changing the selected
index there would be no selected items (hence cpath would not be resolved to
a valid filename) for the image.fromfile method to work with.

While I can understand that technically chaning the selected item in the
list view is a two step process (unselecting the first item and then
selecting the second one); from an user's operational point of view its only
one, and results in needless loops and additional code to handle the
unselect portion as well as the actual change in the selected item. So the
floowing is what I ended up with; it may not be the most eliquent but it
works!

Private Sub lvSrcFileList_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lvSrcFileList.SelectedIndexChanged
' show a preview of the image file selected
Dim path, cpath As String
Dim it As ListViewItem
For Each it In Me.lvSrcFileList.SelectedItems
path = it.Text
Next
If Not IsNothing(path) Then
cpath = curntSrcDir & "\" & path
curntImage = Image.FromFile(cpath)
Me.pbImagePreview.Image = resizedThumbnail(curntImage)
Me.pbImagePreview.Show()
End If
End Sub

-Larry
 
P

Peter Huang

Hi Larry,

I glad that you resolved the problem.
But based on my test, the SelectedIndexChanged will only be called once
when I click another item in the checkedlistbox.
Here is my code, you may try to have test.
BTW I am using VS.NET 2003

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
Me.SuspendLayout()
'
'CheckedListBox1
'
Me.CheckedListBox1.Location = New System.Drawing.Point(40, 8)
Me.CheckedListBox1.Name = "CheckedListBox1"
Me.CheckedListBox1.Size = New System.Drawing.Size(128, 214)
Me.CheckedListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.CheckedListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 5
Me.CheckedListBox1.Items.Add("item" + i.ToString(), True)
Next
j = 1
End Sub
Dim j As Integer
Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
CheckedListBox1.SelectedIndexChanged
Console.WriteLine(j.ToString() + ": hello")
j = j + 1
End Sub
End Class
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top