2010年6月25日 星期五

簡易秀照片程式

在搜尋資料時,發現這個範例原處
但有遺漏,不知是故意的還是忘記了? 這若是初學者,可能run 不出來就放棄了
花了一點時間,把此範例調整並增加遺漏的部分

底下為我修正過後的結果圖:


原始程式碼:
Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Dim header1, header2 As ColumnHeader
header1 = New ColumnHeader
header2 = New ColumnHeader
header1.Text = "File name"
header1.TextAlign = HorizontalAlignment.Left
header1.Width = 160

header2.TextAlign = HorizontalAlignment.Left
header2.Text = "Location"
header2.Width = 300
ListView1.Columns.Add(header1)
ListView1.Columns.Add(header2)
ListView1.View = View.Details
ListView1.GridLines = True
'--
Dim path As String = "C:\Documents and Settings\My Documents\My Pictures\"
Dim pic As String = "*.jpg"
Dim dirInfo As New System.IO.DirectoryInfo(path)
Dim file As System.IO.FileInfo

Dim files() As System.IO.FileInfo = dirInfo.GetFiles(pic)
ListView1.Items.Clear()
If Not (files Is Nothing) Then
For Each file In files
Dim item As New ListViewItem(file.Name)
item.SubItems.Add(file.FullName)
ListView1.Items.Add(item)
Next
End If
End Sub

Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
Dim selection As ListViewItem = ListView1.GetItemAt(e.X, e.Y)
If Not (selection Is Nothing) Then
PictureBox1.Image = System.Drawing.Image.FromFile(selection.SubItems(1).Text)
End If
End Sub

End Class

沒有留言:

張貼留言