列表控件数据操作辅助类

在使用 listbox,checklistbox 时,取出的值总想要自己定义的任意对象,同时可以方便的操作listbox,checklistbox,所以写了这个辅助类 HlListView

使用方法

 1.设置listbox,checklistb值

             ArrayList list  = new  ArrayList();
             testData tmp1 
=   new  testData( " 测试数据类1 " 10 );
             testData tmp2 
=   new  testData( " 测试数据类2 " 2 );
             testData tmp3 
=   new  testData( " 测试数据类3 " 5 );
             list.Add(tmp1);
             list.Add(tmp2);
             list.Add(tmp3);
             HlListView.setValue(
this .listBox2,list, " Name " , " Age " );
             HlListView.setValue(
this .checkedListBox2, list,  " Name " , " Age " );

2.获得listbox,checklistbox值

                // 获得listbox值
             Object value  =  HlListView.getSelectValue( this .listBox2);
            
if  ( value  !=   null ) {
                Console.WriteLine(
"name ="+  ((testData) value).Name );
            }

            
// 获得checklistbox值
            Object[] values  =  HlListView.getCheckedValues( this .checkedListBox2);
            
if  (values  !=   null {
                
for (int i = 0; i < values.Length; i++{
                    Console.WriteLine(
"checkselect="+i+",name =" + ((testData)values[i]).Name);
                }

            }

 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Windows.Forms;
using  System.Collections;

namespace  hllib.com.huanglin.ui
{
    
/// <summary>
    
/// 列表控件数据操作辅助类
    
/// </summary>

    public class HlListView
    
{
        
/// <summary>
        
/// 给列表增加数据
        
/// </summary>

        public static void setValue(ListBox listbox, ArrayList list, String viewfield, String keyfield)
        
{
            
int count = list.Count;
            
if (count > 0)
            
{
                ListData[] datalist 
= new ListData[count];
                
for (int i = 0; i < count; i++)
                
{
                    ListData data 
= new ListData();
                    data.Viewfield 
= viewfield;
                    data.Keyfield 
= keyfield;
                    data.Value 
= list[i];
                    datalist[i] 
= data;
                }

                listbox.DataSource 
= datalist;
            }

        }

        
        
/// <summary>
        
/// 给列表增加数据
        
/// </summary>

        public static void setValue(CheckedListBox listbox, ArrayList list, String viewfield,String keyfield)
        
{
            
int count = list.Count;
            
if (count > 0)
            
{
                ListData[] datalist 
= new ListData[count];
                
for (int i = 0; i < count; i++)
                
{
                    ListData data 
= new ListData();
                    data.Viewfield 
= viewfield;
                    data.Keyfield 
= keyfield;
                    data.Value 
= list[i];
                    datalist[i] 
= data;
                    
                }

                listbox.Items.AddRange(datalist);
            }

        }

 
        
/// <summary>
        
/// 获得选中的对象值
        
/// </summary>

        public static Object getSelectValue(ListBox listbox)
        
{
            Object rtn 
= null;
            
if (listbox.SelectedItem != null)
            
{
                rtn 
= ((ListData)listbox.SelectedItem).Value;
            }

             
return rtn; 
        }


        
/// <summary>
        
/// 取得选中的变量值
        
/// </summary>

        public static Object getSelectValue(CheckedListBox listbox)
        
{
            Object rtn 
= null;
            
if (listbox.SelectedItem != null)
            
{
                rtn 
= ((ListData)listbox.SelectedItem).Value;
            }

            
return rtn;
        }

       
        
/// <summary>
        
/// 取得选中的变量值数组
        
/// </summary>

        public static Object[] getSelectValues(ListBox listbox)
        
{
            Object[] rtn 
= null;
            
int count = listbox.SelectedItems.Count;
            
if (count > 0)
            
{
                rtn 
= new Object[count];
                
for (int i = 0; i < count; i++)
                
{
                    rtn[i] 
= ((ListData)listbox.SelectedItems[i]).Value;
                }

            }

            
return rtn;
        }

       
        
/// <summary>
        
/// 取得选中的变量值数组
        
/// </summary>

        public static Object[] getSelectValues(CheckedListBox listbox)
        
{
            Object[] rtn 
= null;
            
int count =listbox.SelectedItems.Count ;
            
if (count> 0)
            
{
                rtn 
= new Object[count];
                
for (int i = 0; i < count; i++)
                
{
                    rtn[i] 
= ((ListData)listbox.SelectedItems[i]).Value;
                }

            }

            
return rtn;
        }


        
/// <summary>
        
/// 取得复选框选中的变量值数组
        
/// </summary>

        public static Object[] getCheckedValues(CheckedListBox listbox)
        
{
            Object[] rtn 
= null;
            
int count = listbox.CheckedItems.Count;
            
if (count > 0)
            
{
                rtn 
= new Object[count];
                
for (int i = 0; i < count; i++)
                
{
                    rtn[i] 
= ((ListData)listbox.CheckedItems[i]).Value;
                }

            }

            
return rtn;
        }

       
        
/// <summary>
        
/// 按对象值设置复选框选中值
        
/// </summary>

        public static void setCheckedValues(CheckedListBox listbox, Object[] values) {
            
int count = listbox.Items.Count;
            
if (values.Length > 0)
            
{
                
for (int k = 0; k < count; k++)
                    listbox.SetItemChecked(k, 
false);
                
for (int i = 0; i < values.Length; i++)
                
{
                    
for (int j = 0; j < count; j++{
                        
if (((ListData)listbox.Items[j]).Equals(values[i]))
                            listbox.SetItemChecked(j, 
true);
                    }

                }

            }

        }


        
/// <summary>
        
/// 按关键字值设置复选框选中值
        
/// </summary>

        public static void setCheckedKeyValues(CheckedListBox listbox, Object[] keyvalues)
        
{
            
int count = listbox.Items.Count;
            
if (keyvalues.Length > 0)
            
{
                
for (int k = 0; k < count; k++)
                    listbox.SetItemChecked(k, 
false);
                
for (int i = 0; i < keyvalues.Length; i++)
                
{
                    
for (int j = 0; j < count; j++)
                    
{
                        
if (((ListData)listbox.Items[j]).getKeyValue().Equals(keyvalues[i]))
                            listbox.SetItemChecked(j, 
true);
                    }

                }

            }

        }

    }

}

 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  hllib.com.huanglin.utils;

namespace  hllib.com.huanglin.ui
{
    
/// <summary>
    
/// 列表数据模型
    
/// </summary>
    
/// <remarks>主要用于列表的数据的加工,便于列表显示数据,并能取得对应的对象</remarks>

    class ListData
    
{
        
/// <summary>
        
/// 显示值域名
        
/// </summary>

        private String viewfield;
        
/// <summary>
        
/// 关键字值域名
        
/// </summary>

        private String keyfield;

        
/// <summary>
        
/// 关键字值域名
        
/// </summary>

        public String Keyfield
        
{
            
get return keyfield; }
            
set { keyfield = value; }
        }


        
/// <summary>
        
/// 显示值域名
        
/// </summary>

        public String Viewfield
        
{
            
get return viewfield; }
            
set { viewfield = value; }
        }

        
/// <summary>
        
/// 值对象
        
/// </summary>

        private Object value;

        
/// <summary>
        
/// 值对象
        
/// </summary>

        public Object Value
        
{
            
get return this.value; }
            
set this.value = value; }
        }

        
/// <summary>
        
/// 取得关键字值
        
/// </summary>

        public Object getKeyValue() {
            Object rtn 
= null;
            
try
            
{
                
if (value != null)
                    rtn 
= ClassUtils.getFieldValue(value, keyfield);
            }

            
catch (Exception)
            
{

            }

            
return rtn;
        }

        
/// <summary>
        
/// 取得显示值
        
/// </summary>

        public Object getViewValue()
        
{
            Object rtn 
= null;
            
try
            
{
                
if (value != null)
                    rtn 
= ClassUtils.getFieldValue(value, viewfield);
            }

            
catch (Exception)
            
{

            }

            
return rtn;
        }

        
/// <summary>
        
/// 用于列表显示
        
/// </summary>

        public override string ToString()
        
{
            String rtn 
="";
            
try
            
{
                
if (value != null)
                    rtn 
= ClassUtils.getFieldValue(value, viewfield).ToString();
            }

            
catch (Exception)
            
{
            }

            
return rtn;
        }

    }

}

 

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Reflection;

namespace  hllib.com.huanglin.utils
{
    
/// <summary>
    
/// 类操作工具
    
/// </summary>

    public class ClassUtils
    
{
        
        
/// <summary>
        
/// 判断当前对象是否是类
        
/// </summary>

        public static Boolean isClass(String classname, Object obj)
        
{
            Boolean rtn 
= false;
            
if (obj == nullreturn rtn;
            
if (classname == nullreturn rtn;
            Type type 
= obj.GetType().GetInterface(classname);
            
if (obj.GetType().FullName.Equals(classname))
                rtn 
= true;
            
return rtn;
        }


        
/// <summary>
        
/// 判断当前对象是否是类
        
/// </summary>

        public static Boolean isClass(Type type, Object obj) {
            Boolean rtn 
= false;
            
if (type == nullreturn rtn;
            
if (obj == nullreturn rtn;
            
if (type.Equals(obj.GetType()))
                rtn 
= true;
            
return rtn;
        }


        
/// <summary>
        
/// 判断当前对象是否有接口
        
/// </summary>

        public static Boolean isInterface(String interfacename, Object obj)
        
{
            Boolean rtn 
= false;
            
if (obj == nullreturn rtn;
            
if (interfacename == nullreturn rtn;
            Type type 
= obj.GetType().GetInterface(interfacename);
            
if (type != null)
                rtn 
= true;
            
return rtn;
        }


        
/// <summary>
        
/// 获得对象指定属性的值
        
/// </summary>

        public static Object getFieldValue(Object obj, String fieldname)
        
{
            Object rtn 
= null;
            
try
            
{
                rtn 
= obj.GetType().InvokeMember(fieldname, BindingFlags.GetProperty, null, obj, null);
            }

            
catch (Exception)
            
{
            }

            
return rtn;
            
        }


        
/// <summary>
        
/// 获得对象属性类型名
        
/// </summary>

        public static String getPropertyTypeName(Object obj,String fieldname) {
            
return obj.GetType().GetProperty(fieldname).PropertyType.FullName;
        }


        
/// <summary>
        
/// 获得对象属性类型
        
/// </summary>

        public static Type getPropertyType(Object obj, String fieldname)
        
{
            
return obj.GetType().GetProperty(fieldname).PropertyType;
        }


        
/// <summary>
        
/// 设置对象指定属性的值
        
/// </summary>

        public static void setFieldValue(Object obj,String fieldname,Object value) {
            
try
            
{

                obj.GetType().InvokeMember(fieldname, BindingFlags.SetProperty, 
null, obj, new Object[] { value });
            }

            
catch (Exception)
            
{
            }

        }


        
/// <summary>
        
/// 调用没有返回值得方法
        
/// </summary>

        public static void callNoReturnProcedure(Object obj ,String procedure,Object[] vars) {
            
try
            
{
                obj.GetType().InvokeMember(procedure, BindingFlags.InvokeMethod 
| BindingFlags.Public | BindingFlags.Instance, null, obj, null);
            }

            
catch (Exception)
            
{
            }

        }


        
/// <summary>
        
/// 获得String类型
        
/// </summary>

        public static Type getTypeString()
        
{
            
return Type.GetType("System.String");
        }

        
/// <summary>
        
/// 获得Int16类型
        
/// </summary>

        public static Type getTypeInt16()
        
{
            
return Type.GetType("System.Int16");
        }

        
/// <summary>
        
/// 获得Int32类型
        
/// </summary>

        public static Type getTypeInt32()
        
{
            
return Type.GetType("System.Int32");
        }

        
/// <summary>
        
/// 获得Int64类型
        
/// </summary>

        public static Type getTypeInt64()
        
{
            
return Type.GetType("System.Int64");
        }

        
/// <summary>
        
/// 获得Double类型
        
/// </summary>

        public static Type getTypeDouble()
        
{
            
return Type.GetType("System.Double");
        }


        
/// <summary>
        
/// 获得DateTime类型
        
/// </summary>

        public static Type getTypeDateTime() {
            
return Type.GetType("System.DateTime");
        }

        
/// <summary>
        
/// 获得Boolean类型
        
/// </summary>

        public static Type getTypeBoolean() {
            
return Type.GetType("System.Boolean");
        }


        
/// <summary>
        
/// 获得Decimal类型
        
/// </summary>

        public static Type getTypeDecimal(){
            
return Type.GetType("System.Decimal");
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值