使用列表框显示图片? [英] Display pictures using a listbox?
问题描述
我正在制作Windows窗体,该窗体允许人们从不同类型的电影中选择电影.现在,我使用列表框来显示电影和流派的列表.当用户选择一种流派时,该流派的电影将显示在另一个列表框中.现在,我想要的是当用户从列表框中选择影片时在图片框中显示图片.我这样做是为了显示电影信息,但是我不确定如何对图片执行此操作.
I''m making a Windows form which allows people to select a film from different genres. Now i''ve used list boxs to display the list of films and genres. When a user selects a genre, the films from that genre are displayed in another list box. Now what I want is for a picture to be displayed in a picture box when the user selects a film from the listbox. I''ve done this for displaying film information, but im not sure how to do this for pictures.
推荐答案
处理ListBox
的SelectedIndexChanged
事件并根据新选择更新PictureBox
.
Handle theListBox
''sSelectedIndexChanged
event and update thePictureBox
according to the new selection.
我已经非常详细地回答了类似的问题,请参见我的代码:
从列表框中显示图像 [ ^ ].
答案是关于WPF的,但是System.Windows.Forms
的实现几乎相同.
—SAKryukov
I''ve already answered the similar question in a very detailed manner, please see my code:
Displaying an image from a list box[^].
The answer is about WPF, butSystem.Windows.Forms
implementation is nearly identical.
—SAKryukov
我想显示我的应用程序示例
首先,我创建一个comboBox(comboBox1)和pictureBox(pictureBox1)
在comboBox1中有以下4个项目
蓝山
日落
睡莲
冬天
然后在comboBox1_SelectedIndexChanged事件中编写以下代码
I want to show sample of my application
First I create one comboBox(comboBox1)and pictureBox(pictureBox1)
In comboBox1 have following 4 items
Blue hills
Sunset
Water lilies
Winter
And then write following code in comboBox1_SelectedIndexChanged event
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string filepath = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\" + comboBox1.SelectedItem.ToString().Trim() + ".jpg";
if(File.Exists(filepath)){
pictureBox1.ImageLocation = filepath;
pictureBox1.Load(filepath);
pictureBox1.Update();
pictureBox1.Refresh();
pictureBox1.Visible = true;
}
}
启动时,我使pictureBox1的可见状态为false.
也许有帮助
Theingi Win
When started i make pictureBox1''s visible is false.
Maybe helpful
Theingi Win
这篇关于使用列表框显示图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!