Winforms 调用按钮的默认事件处理程序的System.Reflection.PropertyInfo.SetValue()

Winforms 调用按钮的默认事件处理程序的System.Reflection.PropertyInfo.SetValue(),winforms,reflection,c#-2.0,Winforms,Reflection,C# 2.0,所以我不确定为什么会发生这种情况,但我正在运行一些数据行,其中有我想要设置的控件名称、属性和值。除了设置按钮的文本属性外,其他一切都正常工作。由于某种原因,单击事件称为 以下是我得到的一些代码: string controlName, value, property; Control currentControl = null; System.Reflection.PropertyInfo propertyInfo = null; // run through all rows in the

所以我不确定为什么会发生这种情况,但我正在运行一些数据行,其中有我想要设置的控件名称、属性和值。除了设置按钮的文本属性外,其他一切都正常工作。由于某种原因,单击事件称为

以下是我得到的一些代码:

string controlName, value, property;
Control currentControl = null;
System.Reflection.PropertyInfo propertyInfo = null;

// run through all rows in the table and set the property
foreach (DataRow r in languageDataset.Tables[_parentForm.Name].Rows)
{
  controlName = r["ControlName"].ToString().ToUpper();
  value = r["Value"].ToString();
  property = r["Property"].ToString();

  // check all controls on the form
  foreach (Control c in formControls)
  {
    // only change it if its the right control
    if (c.Name.ToUpper() == controlName)
    {
      propertyInfo = c.GetType().GetProperty(property);

      if (propertyInfo != null)
        propertyInfo.SetValue(c, value, null);  ******Calls Event Handler?!?!******
      //

      currentControl = c;
      break;
    }
  }
}
那么,为什么在设置值时它会调用事件处理程序呢?下面是我设置的导致此问题的原因:

<SnappletChangePassword>  
  <ControlName>buttonAcceptPassword</ControlName>
  <Property>Text</Property>  
  <Value>Accept</Value>
</SnappletChangePassword>

按钮密码
正文
接受

我无法用一个简单但完整的程序重现这一点:

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Button goButton = new Button { 
            Text = "Go!",
            Location = new Point(5, 5)
        };

        Button targetButton = new Button {
            Text = "Target",
            Location = new Point(5, 50)
        };
        goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
        targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");

        Form f = new Form { Width = 200, Height = 120,
                Controls = { goButton, targetButton }
        };
        Application.Run(f);
    }

    private static void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }
}

你能拿出一个同样完整的程序来演示它吗?

我不能用一个简单的简短但完整的程序来重现这个过程:

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Button goButton = new Button { 
            Text = "Go!",
            Location = new Point(5, 5)
        };

        Button targetButton = new Button {
            Text = "Target",
            Location = new Point(5, 50)
        };
        goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
        targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");

        Form f = new Form { Width = 200, Height = 120,
                Controls = { goButton, targetButton }
        };
        Application.Run(f);
    }

    private static void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }
}

你能拿出一个同样完整的程序来演示它吗?

遗憾的是,不,我也不能复制它。我不确定是什么原因造成的,但我所做的只是删除按钮并将其放回那里

不知道是什么,但谢谢你的密码


你不是在.Net2.0中写的,是吗?

遗憾的是,我也不能复制它。我不确定是什么原因造成的,但我所做的只是删除按钮并将其放回那里

不知道是什么,但谢谢你的密码


您没有在.Net2.0中编写,是吗?

您是否绝对确定它调用的是单击处理程序而不是TextChanged处理程序?现在尝试重现——有趣的一个……你绝对确定它调用的是Click处理程序而不是TextChanged处理程序吗?但现在正在尝试复制-有趣的一个。。。