[英]Change button image after clicking it
i have picturebox with picture cb. 我有图片cb的图片框。
PBr1_1.Image = new Bitmap(@"Logos\\Images\\cb.png");
I'd like to change image to cg.png and do some action when i click this image. 我想将图像更改为cg.png并在单击此图像时执行某些操作。 I was trying something like that but without success: 我正在尝试类似的东西,但没有成功:
private void PBr1_1_Click(object sender, EventArgs e)
{
if (PBr1_1.Image.ToString() == "cb.png")
{
PBr1_1.Image = new Bitmap(@"Logos\\Images\\cg.png");
// Do some stuff.
}
}
And then do the same when i click image with cb. 然后当我用cb单击图像时也这样做。 To visualise this cb is black circle button image, and cg is green one. 要想象这个cb是黑色圆圈按钮图像,cg是绿色的。
How can i do this? 我怎样才能做到这一点?
Jason is right, you should use some kind of temporary storage to save your current bitmap. 杰森是对的,你应该使用某种临时存储来保存你当前的位图。
The Tag property is useful in this kind of situations. Tag属性在这种情况下很有用。 Here a sample code: (Without error handling) 这里是一个示例代码:(没有错误处理)
somewhere in your load event 在你的加载事件的某个地方
PBr1.Tag = "cb.png";`
PBr1_1.Image = new Bitmap(Path.Combine("Logos\\Images", PBr1.Tag.ToString());
and then in button click 然后在按钮单击
private void PBr1_1_Click(object sender, EventArgs e)
{
string imgPath = "Logos\\Images";
PBr1_1.Image.Dispose();
PBr1_1.Tag = (PBr1_1.Tag == "cb.png" ? "cg.png") : "cb.png") ;
Bitmap bm = new Bitmap(Path.Combine(imgPath, PBr1.Tag.ToString());
PBr1_1.Image = bm;
}
Are you sure that "PBr1_1.Image.ToString()" really returns only the image name? 你确定“PBr1_1.Image.ToString()”真的只返回图像名称吗? Maybe you should check this by writing PBr1_1.Image.ToString() to console or something like that 也许你应该通过将PBr1_1.Image.ToString()写入控制台或类似的东西来检查这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.