2008-11-21 3 views
0

Так что я не совсем уверен, почему это происходит, но я просматриваю некоторые DataRows, где у меня есть имя, свойство и значение элемента управления, которое я хотите установить. Все работает отлично, за исключением случаев, когда я устанавливаю свойство TEXT кнопки. По какой-то причине, событие щелчка называется ...System.Reflection.PropertyInfo.SetValue() вызывает обработчик события по умолчанию кнопки

Вот некоторые из кода, который я получил:

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> 
+0

Вы абсолютно уверены, что это вызов обработчика Click, а не обработчик TextChanged? Попытка воспроизвести сейчас, хотя - интересная ... – 2008-11-21 18:43:49

ответ

3

Я не могу воспроизвести это с простой короткой, но полной программе:

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); 
    } 
} 

Можете ли вы придумать с такой же полной программе, делает продемонстрировать это?

0

К сожалению, я тоже не смог воспроизвести его. Я не уверен, что вызвало это, но все, что я сделал, чтобы исправить это, удалил кнопку и вернул ее туда.

не уверен, что это было, но спасибо за код.

Вы не писали это в .Net2.0, не так ли?

Смежные вопросы