[英]How to change an image in a pictureBox when a List item is chosen in a dataGrid [C#] (Visual Studio)
I am working to create something akin to a "Pokedex" in Visual Studio 2012/13 (It basically is a Pokedex at the moment). 我正在努力在Visual Studio 2012/13中创建类似于“ Pokedex”的东西(目前基本上是Pokedex)。 It is a WinForm program, and so far I have a data grid that is listing off the "Pokemon's" list number, its name, and type.
这是一个WinForm程序,到目前为止,我有一个数据网格,该数据网格列出了“ Pokemon's”列表编号,名称和类型。 What I want to do next is have a pictureBox element show the image of the Pokemon (extracted from a list of image links) when that specific dataGrid list item is chosen (So if #4, Charmander, is chosen in the dataGrid, then I want it to show a picture of Charmander in the pictureBox).
我接下来要做的是,当选择了特定的dataGrid列表项时,有一个pictureBox元素显示Pokemon的图像(从图像链接列表中提取)(因此,如果在dataGrid中选择了#4 Charmander,那么我希望它在pictureBox中显示Charmander的图片)。
This here is an example of the visual design of the project so far: 这是到目前为止该项目的视觉设计的一个示例:
The empty space under the richTextBox is where the pictureBox is. richTextBox下的空白空间是pictureBox所在的位置。 Later on the Pokemon's description will also be included in that box when the list item is chosen, but I believe that will be easy to implement once I understand how to call the image at selection.
稍后,当选择了列表项时,口袋妖怪的描述也将包含在该框中,但是我相信一旦理解了如何在选择时调用该图像,这将很容易实现。
All and any help is appreciated! 感谢所有帮助! I am not including code for the project right now as I do not know what is helpful or not.
我现在不包含该项目的代码,因为我不知道有什么用处。 Do let me know what is needed for a better understanding of the project.
让我知道对项目有更好的了解需要什么。
You could add ImageList
control to your form and add your Pokemon pictures to your ImageList. 您可以将
ImageList
控件添加到窗体中,然后将Pokemon图片添加到ImageList中。 You can easily do that using the designer Images Collection property of the ImageList
. 您可以使用
ImageList
的设计器Images Collection属性轻松地做到这一点。
In addition, make sure to set your image indices as your Pokemons in your DataGridView
(so the first image in the collection is Bulbassur, second is Ivysaur etc.) - this will later help you easy access a picture by its index. 另外,请确保在
DataGridView
中将图像索引设置为Pokemons(因此,集合中的第一张图像是Bulbassur,第二张是Ivysaur等)-以后将帮助您通过其索引轻松访问图片。
Next step is to attach CellEnter
event to your DataGridView
. 下一步是将
CellEnter
事件附加到您的DataGridView
。 It will fire when a cell is focused. 当细胞聚焦时会触发。 Add this code to the event handler:
将此代码添加到事件处理程序中:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
pictureBox1.Image = imageList1.Images[e.RowIndex];
}
}
Note that this will only change the image if you select any of the first column (DexNum) cells. 请注意,这仅在选择第一列(DexNum)单元格中的任何一个时才会更改图像。 You could change it to whatever you like, change the condition.
您可以将其更改为所需的任何内容,并更改条件。
I have not made any validation, you should do so, but I'm assuming that the number of rows is equal to the number of images. 我没有进行任何验证,您应该这样做,但是我假设行数等于图像数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.