排名
7
文章
192
粉丝
15
评论
16
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

code:
public static void SetBehavior<TControl, TValue>(Control control, string propertyName, TValue value)
{
if (control is TControl)
{
Type t = typeof(TControl);
PropertyInfo propertInfo = t.GetProperty(propertyName);
propertInfo.SetValue(control, value);
}
}
public void update(string name, object propertyName, Object value)
{
foreach (Control c in Controls.OfType<Control>().ToList().Where(a => a.Name == name))
{
// MessageBox.Show("修改的控件名:"+c.Name+"");
//SetProperty<Control, object>(c, propertyName, value);
SetBehavior<Control, object>(c, propertyName.ToString(), value);
}
}
调用:
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
//
// MessageBox.Show("修改的属性名:" + e.ChangedItem.PropertyDescriptor.Name+ "");
// MessageBox.Show(models.Name+"");
// MessageBox.Show("更改后的数据:"+models.Text);
// MessageBox.Show("更改前的值:"+e.OldValue + "");
// MessageBox.Show("更改后的值:" + e.ChangedItem.Value + "");
//拿到更改后的数据
ControlModel models = this.propertyGrid1.SelectedObject as ControlModel;
/// <param name="propertyName">属性名</param>
string propertyName = e.ChangedItem.PropertyDescriptor.Name;
/// <param name="value">属性值</param>
object value = e.ChangedItem.Value;
//update(models.Name, func, value);
update(models.Name, propertyName, value);
}
public void property(Control ctr)
{
propertyGrid1.SelectedObject = null;
ControlModel controlModel = new ControlModel();
controlModel.Text = ctr.Text;
controlModel.Name = ctr.Name;
controlModel.Size = ctr.Size;
controlModel.Color = ctr.ForeColor;
controlModel.Font = ctr.Font;
controlModel.Image = ctr.BackgroundImage;
controlModel.Location = ctr.Location;
propertyGrid1.SelectedObject = controlModel;
propertyGrid1.Visible = true;
}
private void label1_Click(object sender, EventArgs e)
{
property(sender as Control);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
property(sender as Control);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
property(sender as Control);
}
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价