根据对象属性给控件赋值

当界面控件比较多的时候,给控件赋值就是一件比较困难的事情了,但是我们可以使用下面这样的办法给控件赋值

  /// <summary>
    /// 绑定帮助类
    /// </summary>
    public class QueryBondHelper
    {
        /// <summary>
        /// 绑定属性到控件
        /// </summary>
        /// <param name="control">control</param>
        /// <param name="mStockClientProductinfoQueryBycodeOut">mStockClientProductinfoQueryBycodeOut</param>
        public static void BindPropertyToControl(Control control, StockClientProductinfoQueryBycodeResp resp)
        {
            //Dictionary<string, string> mWY = new Dictionary<string, string>();
            //mWY.Add("publishAmount", "发行总额(万元)");
            //mWY.Add("assetSize", "资产规模(万元)");
            foreach (Control ctrl in control.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textbox = ctrl as TextBox;
                    ctrl.Enabled = true;
                    textbox.ReadOnly = true;
                    PropertyInfo pi = resp.GetType().GetProperty(textbox.Name.Substring(3));
                    if (pi != null)
                    {
                        string txtValue = pi.GetValue(resp, null) + "";
                        if (pi.PropertyType == typeof(long) || pi.PropertyType == typeof(decimal) || pi.PropertyType == typeof(double))
                        {
                            double i = 0.0;
                            if (double.TryParse(txtValue, out i))
                            {
                                if (textbox.Name.EndsWith("_WY"))
                                {
                                    textbox.Text = i.ToString("N6");
                                }
                                else
                                {
                                    //textbox.Text = i.ToString("N2");
                                    textbox.Text = i.ToString();
                                }
                            }
                            else
                            {
                                textbox.Text = txtValue;
                            }
                        }
                        else
                        {
                            textbox.Text = txtValue;
                        }
                    }
                }
                else if (ctrl is DicComboBox)
                {
                    ctrl.Enabled = false;
                    DicComboBox dicctrl = ctrl as DicComboBox;
                    PropertyInfo pi = resp.GetType().GetProperty(dicctrl.Name.Substring(6));
                    if (pi != null)
                    {
                        string data= pi.GetValue(resp, null) + "";
                        if(data.EndsWith(",") || data.EndsWith(";"))
                        {
                            data = data.Substring(0, data.Length - 1);
                        }
                        dicctrl.Data = data;
                    }
                }
                else if (ctrl is DicCheckBoxComboBox)
                {
                    ctrl.Enabled = false;
                    DicCheckBoxComboBox dicctrl = ctrl as DicCheckBoxComboBox;
                    PropertyInfo pi = resp.GetType().GetProperty(dicctrl.Name.Substring(6));
                    if (pi != null)
                    {
                        string data = pi.GetValue(resp, null) + "";
                        if (data.EndsWith(",") || data.EndsWith(";"))
                        {
                            data = data.Substring(0, data.Length - 1);
                        }
                        dicctrl.Data = data;
                    }
                }
                else if (ctrl is NullableDateTimePicker)
                {
                    ctrl.Enabled = false;
                    NullableDateTimePicker dtpctrl = ctrl as NullableDateTimePicker;
                    PropertyInfo pi = resp.GetType().GetProperty(dtpctrl.Name.Substring(3));
                    if (pi != null)
                    {
                        string txtValue = pi.GetValue(resp, null) + "";
                        if (txtValue != null)
                        {
                            long i;
                            if (Int64.TryParse(txtValue, out i))
                            {
                                dtpctrl.Date = i;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 通过名字找控件。
        /// </summary>
        /// <param name="container">container</param>
        /// <param name="name">name</param>
        /// <returns>returns</returns>
        public Control findControlByName(Control container, string name)
        {
            if (name == null || name.Length == 0)
            {
                return null;
            }

            int count = container.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                Control curControl = container.Controls[i];
                if (name.Equals(curControl.Name))
                {
                    return curControl;
                }
                else
                {
                    Control foundControl = this.findControlByName(curControl, name);
                    if (foundControl != null)
                    {
                        return foundControl;
                    }
                }
            }
            return null;
        }
    }

根据对象属性给控件赋值

时间: 2024-08-29 15:07:50

根据对象属性给控件赋值的相关文章

页面单击按钮弹出modaldialog然后调用ajax处理程序获取数据,给父级页面控件赋值

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RefTopicList.aspx.cs" Inherits="Project.RefTopicList" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.

数据绑定(五)使用集合对象作为列表控件的ItemsSource

原文:数据绑定(五)使用集合对象作为列表控件的ItemsSource ItemsSource属性可以接收一个IEnumerable接口派生类的实例作为自己的值,ItemsSource里存放的是一条一条的数据,列表式控件的条目容器会为这些数据传上外衣,只要为ItemsControl对象设置了ItemsSource属性值,ItemsControl对象就会自动迭代其中的数据元素,为每一个数据元素准备一个条目容器,并使用Binding在条目容器与数据元素之间建立起关联,例子: 界面代码: <StackP

MFC 模仿编译器属性 设置控件

1.VS 编译器 属性设置控件的实现. 完整工程下载地址: 

重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

[源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性 作者:webabcd介绍重新想象 Windows 8.1 Store Apps 之控件增强 文本类控件的增强 为一些控件增加了 Header 属性和 HeaderTemplate 属性 为一些控件增加了 PlaceholderText 属性 示例1.演示

用js给html控件赋值

用js给html控件赋值 <script> window.onload=function setValue()//在页面加载时赋值 { document.getElementById("你要赋值的控件ID").value=你需要赋的值; //$("#aa").html('给html赋值'); //document.getElementById('aa').innerText='给html赋值'; //document.getElementById('aa

WinForm-利用Anchor和Dock属性缩放控件

转自:http://www.cnblogs.com/tianzhiliang/articles/2144692.html 有一点让许多刚接触WinForms编程的开发者感到很棘手,就是在用户调整各种控件的大小时,怎样使它们的窗体保持同步,并与父窗体相称.这可以成为一种让人非常沮丧的情况,尤其是对于有着Web编程背景,转为WinForms的开发者来说.为了缓解这个问题,.NET框架允许你对子控件设置属性,命令在调整父窗体大小时,它们应该如何运作.用来命令控件在调整大小时动作的两个属性就是“Dock

Android开发技巧之使用weight属性实现控件的按比例分配空间

从今天开始,把看书时候的知识点整理成博客, 这个比较简单,估计有经验的都用过,weight属性 在做Android布局的时候,经常遇到需要几个控件按比例分配空间的情况 比如下图效果 在底部设置两个button,占据底部宽度一部分的同时,保持1:3的比例, 当然了,这么难看的布局用处不大,仅是用来说明weight的用法 布局代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" x

【转】黑马程序员_WPF基础属性、控件和语法

1.WPF事件中的sender --WPF中所有事件响应方法第一个参数都是sender,代表触发事件的控件对象,是object类型,需要类型转换使用. --应用:多个按钮的Click共享一个事件处理方法 2.集合List<T> --有时候不能预先确定长度或者需要动态修改内容和长度,这时候可以使用List<T>泛型集合. --List<T>,可以看做是动态的数组.Add.Clear.Count.Remove.RemoveAt.ToArray(转换为数组) --List&l

Delphi对象变成Windows控件的前世今生(关键是句柄和回调函数)

----------------------------------------------------------------------第一步,准备工作:预定义一个全局Win控件变量,以及一个精简化的Win控件类var CreationControl: TWinControl = nil; // 定义全局变量,用来表示每次刚创建的Win控件 TWinControl = class(TControl) private FDefWndProc: Pointer; // 记录原有的窗口过程,但只有