控件的属性是个类,如何给添加自定义属性(C#)
控件的属性是个类,如何给添加自定义属性(C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;
namespace CSFramework.Tech2011
{
/// <summary>
/// 扩展DataRow.通过扩展DataRow,我们的新MyDataRow拥有属性:UserFullName 和方法GetUserFullName()
/// 注意:这里又用到Composite模式.这个模式太基础,应用无处不在.
/// </summary>
public class MyDataRow
{
private DataRow _source = null; //组合DataRow
public DataRow Row { get { return _source; } }
public MyDataRow(DataRow source)
{
_source = source;
}
//注意:扩展DataRow,新属性
public string UserFullName
{
get
{
return "ID:" + _source["user_id"].ToString() + " NAME:" + _source["user_name"].ToString();
}
}
//如果举例还不够,再加个方法
public string GetUserFullName()
{
return "ID:" + _source["user_id"].ToString() + " NAME:" + _source["user_name"].ToString();
}
}
/// <summary>
/// 组合模式演示,将DataTable.Rows由构造器传入。
/// 这个类组合DataTable.Rows ,用IList结构存储。
/// </summary>
public class MyDataRows
{
private IList _rows;
/// <summary>
/// 构建索引器
/// </summary>
public MyDataRow this[int index]
{
get { return (MyDataRow)_rows[index]; }
}
//将DataTable.Rows由构造器传入。
public MyDataRows(DataRowCollection rows)
{
_rows = new ArrayList();
//因传进参数rows为DataTable.Rows,而不是MyDataRow的Collection.需要转换
foreach (DataRow r in rows)
{
MyDataRow temp = new MyDataRow(r); //构建自己的类
_rows.Add(temp);
}
}
//设计一个自定义的方法.
public MyDataRow FindRowByFieldValue(string fieldName, string value)
{
foreach (MyDataRow row in _rows)
{
if (row.Row[fieldName].ToString().ToUpper() == value.ToUpper())
return row;
}
return null;
}
}
/// <summary>
/// 自定义DataTable .也是Compoiste
/// </summary>
public class MyDataTable
{
private MyDataRows _MyRows = null;
public MyDataRows MyRows { get { return _MyRows; } }
public MyDataTable(DataTable source)
{
_MyRows = new MyDataRows(source.Rows);
}
}
/// <summary>
/// 测试用的类
/// </summary>
public class vjsdn_Composite
{
private MyDataTable CreateTestTable()
{
DataTable dt = new DataTable();//创建一个自己的类
dt.Columns.Add("user_id", typeof(string));
dt.Columns.Add("user_name", typeof(string));
DataRow row;//增加两条测试记录
row = dt.NewRow();
row["user_id"] = "001";
row["user_name"] = "csdn";
dt.Rows.Add(row);
row = dt.NewRow();
row["user_id"] = "002";
row["user_name"] = "vjsdn";
dt.Rows.Add(row);
return new MyDataTable(dt);
}
public void Test()
{
MyDataTable dt = CreateTestTable();
//注意这里(MyRows属性及FindRowByFieldValue方法)。调用自定义的方法!
MyDataRow yes = dt.MyRows.FindRowByFieldValue("user_name", "csdn");
if (yes != null)
MessageBox.Show(yes.Row["user_name"].ToString());
else
MessageBox.Show("没找到!!!");
}
/// <summary>
/// 测试MyDataRow的方法和属性
/// </summary>
public void Test1()
{
MyDataTable dt = CreateTestTable();
MessageBox.Show(dt.MyRows[0].GetUserFullName()); //读自定义方法
MessageBox.Show(dt.MyRows[1].UserFullName); //读自定义属性
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;
namespace CSFramework.Tech2011
{
/// <summary>
/// 扩展DataRow.通过扩展DataRow,我们的新MyDataRow拥有属性:UserFullName 和方法GetUserFullName()
/// 注意:这里又用到Composite模式.这个模式太基础,应用无处不在.
/// </summary>
public class MyDataRow
{
private DataRow _source = null; //组合DataRow
public DataRow Row { get { return _source; } }
public MyDataRow(DataRow source)
{
_source = source;
}
//注意:扩展DataRow,新属性
public string UserFullName
{
get
{
return "ID:" + _source["user_id"].ToString() + " NAME:" + _source["user_name"].ToString();
}
}
//如果举例还不够,再加个方法
public string GetUserFullName()
{
return "ID:" + _source["user_id"].ToString() + " NAME:" + _source["user_name"].ToString();
}
}
/// <summary>
/// 组合模式演示,将DataTable.Rows由构造器传入。
/// 这个类组合DataTable.Rows ,用IList结构存储。
/// </summary>
public class MyDataRows
{
private IList _rows;
/// <summary>
/// 构建索引器
/// </summary>
public MyDataRow this[int index]
{
get { return (MyDataRow)_rows[index]; }
}
//将DataTable.Rows由构造器传入。
public MyDataRows(DataRowCollection rows)
{
_rows = new ArrayList();
//因传进参数rows为DataTable.Rows,而不是MyDataRow的Collection.需要转换
foreach (DataRow r in rows)
{
MyDataRow temp = new MyDataRow(r); //构建自己的类
_rows.Add(temp);
}
}
//设计一个自定义的方法.
public MyDataRow FindRowByFieldValue(string fieldName, string value)
{
foreach (MyDataRow row in _rows)
{
if (row.Row[fieldName].ToString().ToUpper() == value.ToUpper())
return row;
}
return null;
}
}
/// <summary>
/// 自定义DataTable .也是Compoiste
/// </summary>
public class MyDataTable
{
private MyDataRows _MyRows = null;
public MyDataRows MyRows { get { return _MyRows; } }
public MyDataTable(DataTable source)
{
_MyRows = new MyDataRows(source.Rows);
}
}
/// <summary>
/// 测试用的类
/// </summary>
public class vjsdn_Composite
{
private MyDataTable CreateTestTable()
{
DataTable dt = new DataTable();//创建一个自己的类
dt.Columns.Add("user_id", typeof(string));
dt.Columns.Add("user_name", typeof(string));
DataRow row;//增加两条测试记录
row = dt.NewRow();
row["user_id"] = "001";
row["user_name"] = "csdn";
dt.Rows.Add(row);
row = dt.NewRow();
row["user_id"] = "002";
row["user_name"] = "vjsdn";
dt.Rows.Add(row);
return new MyDataTable(dt);
}
public void Test()
{
MyDataTable dt = CreateTestTable();
//注意这里(MyRows属性及FindRowByFieldValue方法)。调用自定义的方法!
MyDataRow yes = dt.MyRows.FindRowByFieldValue("user_name", "csdn");
if (yes != null)
MessageBox.Show(yes.Row["user_name"].ToString());
else
MessageBox.Show("没找到!!!");
}
/// <summary>
/// 测试MyDataRow的方法和属性
/// </summary>
public void Test1()
{
MyDataTable dt = CreateTestTable();
MessageBox.Show(dt.MyRows[0].GetUserFullName()); //读自定义方法
MessageBox.Show(dt.MyRows[1].UserFullName); //读自定义属性
}
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网