kuhaku kuhaku - 2 months ago 22
C# Question

Bind a PictureBox with a string of a url for LoadAsync

Is there a way to bind a PictureBox to a string so that when the string will change, it will call

LoadAsync()
with the url in the string and load an image?

Currently this is what I have in the auto generated code.

this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.MySource, "ItemImage", true));


What I want instead is:

this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("String", this.MySource, "ImageUrl", true));


MySource
has a property for url string, and I also tried to make it have a field of an
Image
, but a
Bitmap
for example doesn't have a load async feature so I still can't use the url string.

Answer

Make sure the WaitOnLoad property is false (default), thus enabling asynchronous image load, and then bind to ImageLocation property:

this.itemImagePictureBox.WaitOnLoad = false;
this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding(
    "ImageLocation", this.MySource, "ImageUrl", true));
Comments