code_junkie

System.Reflection.PropertyInfo.SetValue() calling default event handler of button [closed]

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. string controlName, value, property;
  2. Control currentControl = null;
  3. System.Reflection.PropertyInfo propertyInfo = null;
  4.  
  5. // run through all rows in the table and set the property
  6. foreach (DataRow r in languageDataset.Tables[_parentForm.Name].Rows)
  7. {
  8. controlName = r["ControlName"].ToString().ToUpper();
  9. value = r["Value"].ToString();
  10. property = r["Property"].ToString();
  11.  
  12. // check all controls on the form
  13. foreach (Control c in formControls)
  14. {
  15. // only change it if its the right control
  16. if (c.Name.ToUpper() == controlName)
  17. {
  18. propertyInfo = c.GetType().GetProperty(property);
  19.  
  20. if (propertyInfo != null)
  21. propertyInfo.SetValue(c, value, null); ******Calls Event Handler?!?!******
  22. //
  23.  
  24. currentControl = c;
  25. break;
  26. }
  27. }
  28. }
  29.  
  30. <SnappletChangePassword>
  31. <ControlName>buttonAcceptPassword</ControlName>
  32. <Property>Text</Property>
  33. <Value>Accept</Value>
  34. </SnappletChangePassword>
  35.  
  36. using System;
  37. using System.Drawing;
  38. using System.Reflection;
  39. using System.Windows.Forms;
  40.  
  41. class Test
  42. {
  43. static void Main()
  44. {
  45. Button goButton = new Button {
  46. Text = "Go!",
  47. Location = new Point(5, 5)
  48. };
  49.  
  50. Button targetButton = new Button {
  51. Text = "Target",
  52. Location = new Point(5, 50)
  53. };
  54. goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
  55. targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");
  56.  
  57. Form f = new Form { Width = 200, Height = 120,
  58. Controls = { goButton, targetButton }
  59. };
  60. Application.Run(f);
  61. }
  62.  
  63. private static void SetProperty(object target, string propertyName, object value)
  64. {
  65. PropertyInfo property = target.GetType().GetProperty(propertyName);
  66. property.SetValue(target, value, null);
  67. }
  68. }
Add Comment
Please, Sign In to add comment