且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态设置图片框imagelocation在运行时?

更新时间:2023-12-06 15:23:16

我看你想做什么,你要显示在列表视图每个图像?

您需要添加一个ImageList控件来存储所有的影像,然后将每个ListView项链接到存储的图像。

  //添加图像列表和ListView控件
    ImageList中imglist =新的ImageList(this.components);
    ListView的LST =新的ListView();
    lst.LargeImageList = imglist;
    this.Controls.Add(LST);
 

这是建立你所需要的控制。然后,在你的循环加载图像到图像列表,给它一个键,并使用该密钥在你列表项:

 的for(int i = 0;我℃的;我++)
    {
        imglist.images.add(image.fromfile(最filename.jpg));
        ListViewItem的ITM =新的ListViewItem();
        字符串键=的String.Format(PIC {0},我);
        itm.text =键;
        itm.imagek​​ey =键;
        lst.items.add(ITM);
    }
 

这是伪code,当然它不会编译原样,但你的想法?

Here i want to set the imagelocation like this:

pic1.ImageLocation = "";
pic2.ImageLocation = "";

and so on...

foreach (ImageResult result in response.Image.Results)
{
    i++;
    PictureBox thumnailBox = new System.Windows.Forms.PictureBox();
    thumnailBox.Name = "pic" + i.ToString();
    //HOW TO DO ??
    //thumnailBox.ImageLocation = result.Thumbnail.Url;
    //listView1.Items.Add(thumnailBox.Name);                     
}

I see what you want to do, you want to show each image in the listview?

You need to add a imagelist control to store all the images, then you link each listview item to the stored image.

    // add imagelist and listview controls
    ImageList imglist = new ImageList( this.components);
    ListView lst = new ListView();
    lst.LargeImageList = imglist;
    this.Controls.Add(lst);

That sets up the controls you need. Then in your loop load the image into the imagelist, give it a key, and use that key in you list item:

    for (int i = 0; i < 0; i++)
    {
        imglist.images.add(image.fromfile("the-filename.jpg"));
        listviewitem itm = new listviewitem();
        string key = string.format("pic{0}", i);
        itm.text = key;
        itm.imagekey = key;
        lst.items.add(itm);
    }

This is pseudo code, of course it won't compile as is, but you get the idea?