in C#, changing background image of a button after click -
this code not report error , should have work.the button background image not change. idea of wrong?
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; }
edit event myhandler being called buttons created event handler.
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); }); }
... }
this line have in button3_click
handler incorrect:
bt_imp.click += (s, ea) => { myhandler(sender, e, val); };
what doing right capturing sender/event arguments calling method. want let myhandler
sender/arguments bt_imp
click, need change to:
bt_imp.click += (s, ea) => { myhandler(s, ea, val); };
which named parameters in anonymous method.
the second thing need make sure invoke ui changes on ui thread. quite in button3_click
handler, missed in myhandler
routine, invoke background change on ui thread:
this.invoke((methodinvoker)delegate { // runs on ui thread, button.backgroundimage = global::test.properties.resources.impok; });
and should take care of it.
Comments
Post a Comment