在 C# 中,单击后更改按钮的背景图像
in C#, changing background image of a button after click
这段代码没有报错,应该work.The按钮背景图片没有变化。知道哪里出了问题吗?
void MyHandler(object sender, EventArgs e, string val)
{
//Process
Process.Start(@val);
//Change button bkground image
var button = (Button)sender;
button.BackgroundImage = global::Test.Properties.Resources.impOK;
}
编辑 正在从另一个事件处理程序创建的按钮调用事件 MyHandler。
private async void button3_Click(object sender, EventArgs e)
{
...
foreach (KeyValuePair<string, string> pair in printerDic)
{
//Init
String val = pair.Value;
String ky = pair.Key;
//Button
Button bt_imp = new Button();
if (List_localServPrnLink.Contains(val))
{
bt_imp.BackgroundImage = global::Test.Properties.Resources.impOK;
}
else
{
bt_imp.BackgroundImage = global::Test.Properties.Resources.impX;
}
bt_imp.Size = new System.Drawing.Size(30, 30);
bt_imp.Location = new Point(horizotal, vertical - bt_imp.Height / 2);
bt_imp.Click += (s, ea) => { MyHandler(sender, e, val); };//passing printer instal link when click
...
vertical += 30;//Prochaine Ligne...
this.Invoke((MethodInvoker)delegate { // runs on UI thread,
tabPage1.Controls.Add(bt_imp);
tabPage1.Controls.Add(lb_impBt);
});
}
...
}
您在 button3_Click
处理程序中的这一行不正确:
bt_imp.Click += (s, ea) => { MyHandler(sender, e, val); };
它现在正在做的是从 调用 方法中捕获 sender/event 参数。你要做的是让MyHandler
从bt_imp
的点击中得到sender/arguments,所以你需要把它改成:
bt_imp.Click += (s, ea) => { MyHandler(s, ea, val); };
你的匿名方法中有哪些命名参数。
您需要做的第二件事是确保在 UI 线程上调用 UI 更改。您在 button3_Click
处理程序中做得很好,但在 MyHandler
例程中错过了它,因此只需在 UI 线程上调用该后台更改:
this.Invoke((MethodInvoker)delegate { // runs on UI thread,
button.BackgroundImage = global::Test.Properties.Resources.impOK;
});
这应该可以解决。
这段代码没有报错,应该work.The按钮背景图片没有变化。知道哪里出了问题吗?
void MyHandler(object sender, EventArgs e, string val)
{
//Process
Process.Start(@val);
//Change button bkground image
var button = (Button)sender;
button.BackgroundImage = global::Test.Properties.Resources.impOK;
}
编辑 正在从另一个事件处理程序创建的按钮调用事件 MyHandler。
private async void button3_Click(object sender, EventArgs e)
{
...
foreach (KeyValuePair<string, string> pair in printerDic)
{
//Init
String val = pair.Value;
String ky = pair.Key;
//Button
Button bt_imp = new Button();
if (List_localServPrnLink.Contains(val))
{
bt_imp.BackgroundImage = global::Test.Properties.Resources.impOK;
}
else
{
bt_imp.BackgroundImage = global::Test.Properties.Resources.impX;
}
bt_imp.Size = new System.Drawing.Size(30, 30);
bt_imp.Location = new Point(horizotal, vertical - bt_imp.Height / 2);
bt_imp.Click += (s, ea) => { MyHandler(sender, e, val); };//passing printer instal link when click
...
vertical += 30;//Prochaine Ligne...
this.Invoke((MethodInvoker)delegate { // runs on UI thread,
tabPage1.Controls.Add(bt_imp);
tabPage1.Controls.Add(lb_impBt);
});
}
... }
您在 button3_Click
处理程序中的这一行不正确:
bt_imp.Click += (s, ea) => { MyHandler(sender, e, val); };
它现在正在做的是从 调用 方法中捕获 sender/event 参数。你要做的是让MyHandler
从bt_imp
的点击中得到sender/arguments,所以你需要把它改成:
bt_imp.Click += (s, ea) => { MyHandler(s, ea, val); };
你的匿名方法中有哪些命名参数。
您需要做的第二件事是确保在 UI 线程上调用 UI 更改。您在 button3_Click
处理程序中做得很好,但在 MyHandler
例程中错过了它,因此只需在 UI 线程上调用该后台更改:
this.Invoke((MethodInvoker)delegate { // runs on UI thread,
button.BackgroundImage = global::Test.Properties.Resources.impOK;
});
这应该可以解决。