阿里妈妈,帮你实现网络赚钱梦,流量变成现金!
上个月做了个可编辑的Listbox,春节放假前又改进了一点,加了一个自定义的事件。此事件是当编辑完成时触发,因为当编辑项时没法在外部获得值,加此事件将值传递出来,这样就可在外部做一些操作(修改数据库)。
添加的事件是OnEditComplete,具体代码如下:
复制内容到剪贴板
程序代码
程序代码 public sealed class EditCompleteEventArgs : EventArgs
{
private int index;
private string newText;
public EditCompleteEventArgs(int index, string newText)
{
this.index = index;
this.newText = newText;
}
public int Index
{
get
{
return index;
}
}
public string NewText
{
get
{
return newText;
}
}
}
protected virtual void OnEditComplete(EditCompleteEventArgs e)
{
if (EditComplete != null)
{
EditComplete(this, e);
}
}
{
private int index;
private string newText;
public EditCompleteEventArgs(int index, string newText)
{
this.index = index;
this.newText = newText;
}
public int Index
{
get
{
return index;
}
}
public string NewText
{
get
{
return newText;
}
}
}
protected virtual void OnEditComplete(EditCompleteEventArgs e)
{
if (EditComplete != null)
{
EditComplete(this, e);
}
}
这里给这个事件设置了两个参数,分别是项的索引值和编辑后的文本,可以根据需要添加不同的参数。这是当编辑完成时触发的事件,所以把它放在textbox的keypress事件里响应,如下:
复制内容到剪贴板
程序代码
程序代码 if (e.KeyChar == 13)
{
//省略N代码。。。
OnEditComplete(new EditCompleteEventArgs(selectedIndex, newText));
}
{
//省略N代码。。。
OnEditComplete(new EditCompleteEventArgs(selectedIndex, newText));
}
这个地方可以多加一些判断,可以只让它在项的值被修改后才触发事件。
考虑到有可能编辑项后数据的长度变化了,所以这时要重新设置水平滚动条的长度。
所增加的大概就是这些,以前的可参考:http://lwolf.cn/blog/article/code/csharp-editable-listbox.htm
PS:我的C#交流群 30954402,欢迎加入~~
[本日志由 老狼 于 2010-02-17 04:13 PM 编辑]
文章来自: 本站原创
文章标签: c# winform 控件
网摘收录:
相关日志:
发表评论
上一篇:
下一篇: 