C# 窗体加载时设置控件背景图:在 Load 事件中还是切换模块中?
这段代码可以放在窗体的 'Load' 事件中,以确保在窗体加载时设置 'uiHeaderButton' 控件的背景图。
例如,将代码放置在窗体的 'Load' 事件处理程序中:
private void Form_Load(object sender, EventArgs e)
{
string currentDirectory = Directory.GetCurrentDirectory();
string imagePath = Path.Combine(currentDirectory, "background.jpg");
if (File.Exists(imagePath))
{
uiHeaderButton1.BackgroundImage = Image.FromFile(imagePath);
}
}
这样,当窗体加载时,会检查当前目录下是否存在名为 'background.jpg' 的图片文件,并将其设置为 'uiHeaderButton1' 控件的背景图。请确保在窗体加载时执行此代码,以确保背景图的正确设置。
原文地址: https://www.cveoy.top/t/topic/RE9 著作权归作者所有。请勿转载和采集!