Home [C# 筆記][WinForm] ListBox 實現點擊更換圖片
Post
Cancel

[C# 筆記][WinForm] ListBox 實現點擊更換圖片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//用來儲存圖片全路徑
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
    //取得指定文件夾中的所有圖片的全路徑
    string[] path = Directory.GetFiles(@"C:\Users\rivalin\Desktop\images", "*.jpg");
    for (int i = 0; i < path.Length; i++)
    {
        //取得圖片名
        string filename = Path.GetFileName(path[i]);
        //加入listBox中
        listBox1.Items.Add(filename);

        //將圖片路徑加到list泛型集合中
        list.Add(path[i]);
    }
}
/// <summary>
/// 雙擊顯示圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
}
This post is licensed under CC BY 4.0 by the author.