欢迎光临
我们一直在努力

关于.NET中WinForms里面的ListBox实现数据绑定的…-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

关于.net中winforms里面的listbox实现数据绑定的…

——————————————————————————–

在.net中,window forms下面的list box控件在开发时,如果采用其本身的数据绑定,绑定完以后就不能更改listbox的items了.而实际开发中却经常会碰到要改变的情况,在这里我提供了一重方法.采用开发继承listbox控件的自定义控件.然后在里面提供两个sortedlist类的属性,一个可以存放id,一个存放text,这样就解决了上面说的问题!!

控件的代码如下:

using system;

using system.collections;

using system.componentmodel;

using system.drawing;

using system.data;

using system.windows.forms;

namespace flowmanage

{

/// <summary>

/// syslistbox 的摘要说明。

/// </summary>

public class syslistbox : system.windows.forms.listbox

{

private sortedlist _sl=new sortedlist();

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;

public syslistbox()

{

// 该调用是 windows.forms 窗体设计器所必需的。

initializecomponent();

// todo: 在 initializecomponent 调用后添加任何初始化

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

public sortedlist datavalues

{

get

{

return _sl;

}

set

{

_sl=value;

}

}

public void additem(object key,object text)

{

if(this.datavalues==null)

{

this.datavalues=new sortedlist();

}

this.datavalues.add(key,text);

}

public void removeitem(int index)

{

this.datavalues.removeat(index);

}

public void removeitem()

{

this.datavalues.clear();

}

public void boundlist()

{

this.items.clear();

if(this.datavalues!=null)

{

this.beginupdate();

for(int i=0;i<this.datavalues.count;i++)

{

this.items.add(this.datavalues.getbyindex(i).tostring());

}

this.endupdate();

}

}

#region component designer generated code

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器

/// 修改此方法的内容。

/// </summary>

private void initializecomponent()

{

components = new system.componentmodel.container();

}

#endregion

}

}

而在调用这个控件时的代码如下:

string mkey=this.listcansel.datavalues.getkey(this.listcansel.selectedindex).tostring();

string mtext=this.listcansel.datavalues.getbyindex(this.listcansel.selectedindex).tostring();

this.listsel.additem(mkey,mtext);

this.listcansel.removeitem(this.listcansel.selectedindex);

this.listsel.items.add(mtext);

this.listcansel.items.removeat(this.listcansel.selectedindex);

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 关于.NET中WinForms里面的ListBox实现数据绑定的…-.NET教程,VB.Net语言
分享到: 更多 (0)

相关推荐

  • 暂无文章