在用户控件中的运行时更改按钮背景 [英] change button background at runtime in user control

查看:71
本文介绍了在用户控件中的运行时更改按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控件初始化后,我试图在运行时更改按钮背景,但是按钮背景并未改变任何想法,我使用了以下代码

我从xml文件读取了图像路径,可以确定路径和文件名


btn1.BackgroundImage = System.Drawing.Image.FromFile(imagepath + x.Attribute(XName.Get("Image")).Value);

I tried to change the button background at runtime after the controls has been initialized but the button background does not changed any idea i used the following piece of code

I read the image path from xml file it is ok with path and file name


btn1.BackgroundImage = System.Drawing.Image.FromFile(imagepath + x.Attribute(XName.Get("Image")).Value);

推荐答案

对我来说很好.
因此:要做的第一件事就是检查您的代码是否正在执行.
在上面显示的行上放置一个断点,并确保它被命中.
如果是,则创建一个包含完整信息的字符串:
It works fine for me.
So: the first thing to do is to check that your code is being executed.
Put a breakpoint on the line you show above, and make sure it is hit.
If it is, then create a string which holds the full info:
string path = imagepath + x.Attribute(XName.Get("Image")).Value;

在调试器中查看路径",并确保该文件存在并且可以从该PC完全访问. br/> 如果还可以,请查看BackgroundImage属性,并确保将其设置为图像.
然后仔细检查btn1是否确实在显示器上!

其中一个应该让您知道问题出在哪里:如果不是,请告诉我们您在上述每个阶段都能获得什么,我们将看到我们可以做的.

Look at "path" in the debugger, and make sure that the file exists and is fully reachable from that PC.
If that''s all ok, look at the BackgroundImage property and make sure it is set to an image.
Then double check that btn1 is actually on the display!

One of those should give you an idea what the problem is: if it doesn''t, then tell us what you get at each stage above, and we will see what we can do.


winforms或wpf?
试试这个:
` http://msdn.microsoft.com/en-us/library /system.windows.forms.control.update.aspx [ ^ ]
您可以通过调用update方法来强制"控件重绘.还有"startrender"和"stoprender"之类的东西,但我忘记了确切的名称和用法.
winforms or wpf?
try this:
`http://msdn.microsoft.com/en-us/library/system.windows.forms.control.update.aspx[^]
you can "force" a control to repaint by calling the update method. there is also something like "startrender" and "stoprender", but i forgot the exact name and usage.


这篇关于在用户控件中的运行时更改按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆