winforms - Toggle the picture of picture box with picture box on_click c# -
i have 1 image in picture box using resource want change image when click icon.png should changed icon1.png within picturebox when click again picture box should changed icon.png
private void picturebox10_click(object sender, eventargs e) { if (picturebox10.imagelocation != @"icon1.png") { var image = image.fromfile(@"icon1.png"); picturebox10.image = image; } if (picturebox10.imagelocation == @"icon1.png") { var image = image.fromfile(@"icon.png"); picturebox10.image = image; } }
but not working please me out of this.
you're getting null image location it's not set when you're assigning picture image property. there few ways fix this:
change assignment assign using imagelocation
picturebox10.imagelocation = @"icon1.png";
change check see if image property equal new image
picturebox10.image == image.fromfile(@"icon.png");
set image location @ same time set image property
picturebox10.image == image.fromfile(@"icon.png"); picturebox10.imagelocation = @"icon.png" ;
i feel second might not come equal, want try first 1 or third
suggested code:
private void picturebox10_click(object sender, eventargs e) { if (picturebox10.imagelocation != @"icon1.png") { picturebox10.imagelocation = @"icon1.png" } if (picturebox10.imagelocation == @"icon1.png") { picturebox10.imagelocation = @"icon.png"; } }
or:
private void picturebox10_click(object sender, eventargs e) { if (picturebox10.imagelocation != @"icon1.png") { var image = image.fromfile(@"icon1.png"); picturebox10.image = image; picturebox10.imagelocation = @"icon1.png"; } if (picturebox10.imagelocation == @"icon1.png") { var image = image.fromfile(@"icon.png"); picturebox10.image = image; picturebox10.imagelocation = @"icon.png"; } }
you need update intial property setting set imagelocation , not image property or set imagelocation @ same time set image file
edit
off top of head, set property initially, can (source):
protected override void onload(eventargs e){ picturebox10.imagelocation = @"icon.png"; }
though can't remember if picturebox have been created then, if not use onshown event instead (source)
edit 2
here way create event , set property, first follow steps here add event onshown form. need click on form , not controls inside form find event.
once done, inside event add following code:
picturebox10.imagelocation = @"icon.png";
that should resolve issue
Comments
Post a Comment