首页
视频
资源
登录
小可爱
什么时候才能领悟,取之越多失之越多
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
ASP.NET
16篇
JS
1篇
.NET
23篇
英语
4篇
redis
4篇
随笔
30篇
EF
3篇
前端
53篇
.net core
25篇
python
8篇
版本控制
1篇
java
2篇
nginx
2篇
docker
6篇
后端
5篇
更多
mysql
8篇
数据库
15篇
微服务
4篇
移动开发
7篇
架构
6篇
linux
2篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术
原
动态设置winfrom控件属性的值
3929
人阅读
2020/7/25 16:00
总访问:
878857
评论:
0
收藏:
0
手机
分类:
后端
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
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}