winform批量控件取值赋值(代码片段)

cuichaohui cuichaohui     2023-01-01     531

关键词:

以前写winform 最多写几个文本框,最近需要入录一个人员信息,那好几十个字段,一下子干蒙了,这要是一个个取值赋值都写到明天了,于是就自己写了个方法,也不是什么高大上的,就是很简单很普通很low的方法。

废话少说上代码,注意,这块我就用了个文本框,你也可以找到所有控件,尽量控件name与实体字段一样。

技术分享图片
  public Dictionary<string, object> GetRS_InfoVue()
        
            var dic = new Dictionary<string, object>();
            foreach (Control ctl in groupBox1.Controls)
            
                if (ctl is TextBox)
                
                    dic.Add(((TextBox)ctl).Name, ((TextBox)ctl).Text);
                
            
            return dic;
        
View Code

根据控件,实体赋值

技术分享图片
   /// <summary>
        /// 属性赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static T SetProperties<T>(T t, Dictionary<string, object> keyValues)
        
            PropertyInfo[] propertys = t.GetType().GetProperties();
            foreach (var item in propertys)
            
                if (keyValues.ContainsKey(item.Name))
                
                    //否是泛型
                    if (!item.PropertyType.IsGenericType)
                    
                        if (!string.IsNullOrEmpty(keyValues[item.Name].ToString()))
                        
                            item.SetValue(t, Convert.ChangeType(keyValues[item.Name], item.PropertyType), null);
                        
                    
                    else
                    
                        if (!string.IsNullOrEmpty(keyValues[item.Name].ToString()))
                        
                            //泛型Nullable<>
                            Type genericTypeDefinition = item.PropertyType.GetGenericTypeDefinition();
                            if (genericTypeDefinition == typeof(Nullable<>))
                            
                                item.SetValue(t, Convert.ChangeType(keyValues[item.Name], Nullable.GetUnderlyingType(item.PropertyType)), null);
                            
                        

                    
                
            
            if (keyValues.Count < 0)
            
                return default(T);
            
            return t;
        
View Code

根据实体,控件赋值

技术分享图片
   public static Dictionary<string, string> GetProperties<T>(T t)
        
            string tStr = string.Empty;
            if (t == null)
            
                return null;
            
            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

            if (properties.Length <= 0)
            
                return null;
            
            var dic = new Dictionary<string, string>();
            foreach (PropertyInfo item in properties)
            
                string name = item.Name;
                object value = item.GetValue(t, null);
                if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                
                    if (!dic.ContainsKey(name))
                    
                        if (value != null)
                        
                            dic.Add(name, value.ToString());
                        
                        else
                            dic.Add(name, "");
                    
                
            
            return dic;
        
View Code

 

winform框架中内容的学习(代码片段)

 一.Winform框架中的内容WinForm(一)WinForm入门与基本控件使用_阿阿阿安的博客-CSDN博客_winform winform控件及其各个属性_weixin_30907935的博客-CSDN博客【(1)WindowState属性:用来获取或设置窗体的窗口状态。取值有... 查看详情

winform里有很多textbox我如何可以批量的赋值以及保存

一个一个的赋值一个一个的保存很麻烦啊?而且DS一个一个对应的赋值繁琐。还是不行啊你们呢说的也没解决问题、这个问题我也想了很久还是一样的。没有简便方法,很多而且和数据库没有直接的规律。只是数据路里有对应的... 查看详情

线程中为控件赋值winform

this.Invoke(newMethodInvoker(()=>{ //TODO}));this.Invoke(newAction(()=>{//TODO}));   查看详情

c#-winform-公共控件的基本属性及练习

视图→工具箱基本操作:控件的取值、赋值、改值、事件1、Button——按钮   AutoSize-指示该控件是否自动调整自身的大小以适应其内容的大小。    默认False,此时文字内容超过其宽度时自动换行;如果为true,控件会... 查看详情

c#winform设置控件居中(代码片段)

简单阐述1在C#的WinForm里面,原生控件是没有居中属性的,故通过重写OnResize(EventArgse)方法,通过计算,重新定位控件位置。以Tab控件为例(1)重写居中的代码如下:protectedoverridevoidOnResize(System.EventArgse)base.OnResize(e);intx=(int)(0.5*(this.W... 查看详情

批量操作取值调接口(代码片段)

批量操作取值调接口selectionChange(val)vararr=[];//数组置空if(val.length)/////////////切记,判断取值数组的的长度for(vari=0;i<val.length;i++)arr.push(val[i].id)//追加你需要的idthis.selectedId=arrthis.selectShow=trueelsethis.s 查看详情

c#winform开发(代码片段)

文章目录C#WinForm开发1.创建C#WinForm项目a.进入项目界面b.项目结构c.自定义一个Form2.给控件添加事件3.显示时间小项目4.控件5.几种布局a.FlowLayoutPanel流式布局b.TableLayoutPanel表格布局c.可以自定义控件6.文本框7.CheckBox复选框8.其他的一... 查看详情

winform用户控件事件的写法(代码片段)

publicpartialclassUcTest:UserControlpublicUcTest()InitializeComponent();//定义事件publiceventEventHandlerUserControlBtnClicked;privatevoidbtn_Click(objectsender,EventArgse)UserControlBtnClicked?.Invok 查看详情

winform自定义自动完成控件(代码片段)

...,很多优秀的前端框架都会带有自动完成控件,同样的,winform也有,在我们的TextBox和ComboBox中,只需要设置AutoCompleteSource属性为CustomSource,然后将值加入到AutoCompleteCustomSource中就可以了  比如:  string[]dataSource=newstring[]"appl... 查看详情

winform自定义控件中其他遮挡控件点击事件(代码片段)

自定义控件在其他窗口调用时,里面的lable阻挡了控件的点击事件解决方法自定义控件中lable的 点击事件privatevoidLable1_Click(objectsender,EventArgse)base.OnClick(e);//触发控件点击事件  查看详情

winform控件坐标定位(代码片段)

控件的X,Y轴坐标都是相对外面一层容器的位置。如果控件在Panel1,Panel1又在Panel2,则控件坐标是根据Panel1计算。1、通过属性Location,可以在属性框设置。2、通过属性Left、Top,不在属性框里。usingSystem;usingSystem.Windows.Forms;namespace... 查看详情

图案滑屏解锁控件----------winform控件开发系列(代码片段)

///<summary>///图案滑屏解锁控件///</summary>[ToolboxItem(true)][DefaultEvent("UnLock")][DefaultProperty("Value")][Description("图案滑屏解锁控件")]publicpartialclassPatternLockExt:Control#region事件publicde 查看详情

layui表单赋值/取值与任意位置按钮提交表单(代码片段)

表单赋值/取值与任意位置按钮提交表单前言表单赋值/取值任意位置按钮执行提交方法:测试效果预览前言1.通常表单赋值,通过Jquery赋值,每给标签单独赋值,显得非常笨着,希望直接可以给form表单赋值。key-value方式。2.在v2.7.... 查看详情

wpf使用winform自定义控件(代码片段)

...引用WindowsFormsIntegration.dllSystem.Windows.Forms.dll2、在要使用WinForm控件的WPF窗体的XAML文件中添加如下内容:xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration... 查看详情

实现winform跨线程安全访问ui控件(代码片段)

在多线程操作WinForm窗体上的控件时,出现“线程间操作无效:从不是创建控件XXXX的线程访问它”,那是因为默认情况下,在Windows应用程序中,.NETFramework不允许在一个线程中直接操作另一个线程中的控件(因为访问Windows窗体控件... 查看详情

winform跨线程更新ui控件(代码片段)

首选拖几个控件,如图:  直接上代码:1publicpartialclassForm3:Form23publicForm3()45InitializeComponent();6789//方法一开始10privateasyncvoidbutton1_Click(objectsender,EventArgse)1112vart=Task.Run(()=>1314Th 查看详情

winform控件之combobox,datagridview(代码片段)

1.代码结构截图2.核心代码usingSystem;usingSystem.Data;usingSystem.Drawing;usingSystem.Windows.Forms;namespaceWinFormComboBoxDemos///<summary>///WinForm程序:ComboBox和DataGridView使用方法///LDH@2018-3-6///</su 查看详情

13.winform练习--listbox控件使用(代码片段)

namespace_13.ListBox控件的使用publicpartialclassForm1:FormpublicForm1()InitializeComponent();//新建个集合对象用于存储图片的全路径List<string>list=newList<string>();privatevoidForm1_Load(objectsender,EventAr 查看详情