简体   繁体   English

在dataGrid [C#]中选择列表项时,如何在pictureBox中更改图像(Visual Studio)

[英]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.

相关问题 如何使用C#将图像加载到Visual Studio中的PictureBox上 - How to Load an Image onto PictureBox in Visual Studio using C# 通过单击Visual Studio 2012 c#和xaml中的列表视图项来更改图像 - Change image by click of list view item in Visual Studio 2012 c# and xaml 如何更改图片框中的图像 C# - How to change image in picturebox C# 将外部图像分配给Visual Studio 2008(C#)中的Picturebox? - Assign an external image to a Picturebox in Visual Studio 2008 (C#)? Visual C#无法更改picturebox.click上的图像 - Visual C# cannot change picturebox.Image on click 如何才能逐渐且平滑地更改PictureBox的大小? (在Winforms中,C#,Visual Studio 2013) - How can I change PictureBox size incrementally and smoothly ? ( in winforms , c# , visual studio 2013 ) 如何使用C#在Visual Studio中的图片框中访问图像的名称 - How do you access the name of the image in a picturebox in Visual Studio using C# 如何使用 Microsoft Visual Studio 使用 C# windows 窗体将文件中的图像加载到图片框 - How to load image from file into picturebox using C# windows form using Microsoft Visual Studio 如何在边界中保持移动PictureBox C#Visual Studio 2010 - How to keep moving PictureBox in boundaries C# Visual Studio 2010 在Visual Studio C#中的PictureBox中填写Function? - Fill in Function in PictureBox in Visual Studio C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM