在我们开发自定义
控件的过程中,我们常常会给
控件添加集合属性。比如定制Grid
控件就会有Column集合。当集合属性发生变化时,比如添加新元素,删除新元素,我们要通知
控件去重绘以反映新的变化。我们可以创建一个集合类,在类里添加一个新的事件,并在集合发生变化时触发事件。.Net框架已经为我们提供了现成的委托声明,下面我们来看看怎么使用。
我创建一个名字叫HeaderCollection的类,派生于System.System.Collections.ObjectModel.Collection<ColumnHeader>。利用.Net已经声明的CollectionChangeEventHandler委托来声明一个事件,如下:
public delegate void CollectionChangeEventHandler (
Object sender,
CollectionChangeEventArgs e
)

CollecionChangeEventArgs的构造函数需要两个参数,一个枚举类型CollectionChangeAction,用它来指出发生了什么变化,包含添加,删除,整个集合发生变化三种动作,第二个参数传递发生变化的元素。
接下来我们重载Collection<T>的几个方法:InsertItem,ClearItems,RemoveItem在这些方法里触发事件,下边是这个类的完整描述:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CPI.DataProcess.Forms


{
[Serializable]
public class HeaderCollection:Collection<ColumnHeader>


{

public event CollectionChangeEventHandler ItemChanged;

public HeaderCollection()


{
}

protected override void InsertItem(int index, ColumnHeader item)


{
base.InsertItem(index, item);
ItemChanged(this,new CollectionChangeEventArgs(CollectionChangeAction.Add,item));
}

protected override void ClearItems()


{
base.ClearItems();
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}

protected override void RemoveItem(int index)


{
base.RemoveItem(index);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Remove, this[index]));
}

protected override void SetItem(int index, ColumnHeader item)


{
base.SetItem(index, item);
ItemChanged(this, new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
}

private void OnItemPropertyChanged(Object sender)


{
ItemChanged(sender,null);
}
}
}

这样我们的Collection就暴露出一个改变事件,在使用它的地方给它添加委托就可以了,例如:
public void OnCollectionPropertyChanged(Object sender,CollectionChangeEventArgs e)


{
Invalidate();
}
private HeaderCollection _Headers =new HeaderCollection();
Headers.ItemChanged+=new CollectionChangeEventHandler(OnCollectionPropertyChanged);

当集合属性发生变化时就会触发事件通知自定义
控件重绘。
posted @
2006-12-04 23:12 纶巾客 阅读(2961) |
评论 (3) |
编辑
我本人不是专业的控件开发人员,只是在平常的工作中,需要自己开发一些控件。在自己开发WinForm控件的时候,没有太多可以借鉴的资料,只能盯着MSDN使劲看,还好总算有些收获。现在我会把这些经验陆陆续续的总结出来,写成一系列方章,希望对看到的朋友有所帮助。今天我来开个头。
其实开发WinForm控件并不是很复杂,.NET为我们提供了丰富的底层支持。如果你有MFC或者API图形界面的开发经验,那么学会WinForm控件可能只需要很短的时间就够了。
自己开发的WinForm控件通常有三种类型:复合控件(Composite Controls),扩展控件(Extended Controls),自定义控件(Custom Controls)。
复合控件:将现有的各种控件组合起来,形成一个新的控件,将集中控件的功能集中起来。
扩展控件:在现有控件的控件的基础上派生出一个新的控件,为原有控件增加新的功能或者修改原有控件的控能。
自定义控件:直接从System.Windows.Forms.Control类派生出来。Control类提供控件所需要的所有基本功能,包括键盘和鼠标的事件处理。自定义控件是最灵活最强大的方法,但是对开发者的要求也比较高,你必须为Control类的OnPaint事件写代码,你也可以重写Control类的WndProc方法,处理更底层的Windows消息,所以你应该了解GDI+和Windows API。
本系列文章主要介绍自定义控件的开发方法。
控件(可视化的)的基本特征:
1. 可视化。
2. 可以与用户进行交互,比如通过键盘和鼠标。
3. 暴露出一组属性和方法供开发人员使用。
4. 暴露出一组事件供开发人员使用。
5. 控件属性的可持久化。
6. 可发布和可重用。
这些特征是我自己总结出来,不一定准确,或者还有遗漏,但是基本上概括了控件的主要方面。
接下来我们做一个简单的控件来增强一下感性认识。首先启动VS2005创建一个ClassLibrary工程,命名为CustomControlSample,VS会自动为我们创建一个solution与这个工程同名,然后删掉自动生成的Class1.cs文件,最后在Solution explorer里右键点击CustomControlSample工程选择Add->Classes…添加一个新类,将文件的名称命名为FirstControl。下边是代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace CustomControlSample


{
public class FirstControl : Control

{

public FirstControl()

{

}

// ContentAlignment is an enumeration defined in the System.Drawing
// namespace that specifies the alignment of content on a drawing
// surface.
private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;

[
Category("Alignment"),
Description("Specifies the alignment of text.")
]
public ContentAlignment TextAlignment

{

get

{
return alignmentValue;
}
set

{
alignmentValue = value;

// The Invalidate method invokes the OnPaint method described
// in step 3.
Invalidate();
}
}


protected override void OnPaint(PaintEventArgs e)

{
base.OnPaint(e);
StringFormat style = new StringFormat();
style.Alignment = StringAlignment.Near;
switch (alignmentValue)

{
case ContentAlignment.MiddleLeft:
style.Alignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleRight:
style.Alignment = StringAlignment.Far;
break;
case ContentAlignment.MiddleCenter:
style.Alignment = StringAlignment.Center;
break;
}

// Call the DrawString method of the System.Drawing class to write
// text. Text and ClientRectangle are properties inherited from
// Control.
e.Graphics.DrawString(
Text,
Font,
new SolidBrush(ForeColor),
ClientRectangle, style);

}
}
}

晚了,今天写到这里,下一篇文章介绍怎样使用我们写好的控件。
posted @
2006-12-04 23:03 纶巾客 阅读(11279) |
评论 (31) |
编辑
在做控件的时候经常会遇到一个很烦人的问题,就是在测试工程中加入自己的控件,然后编辑属性的总是提示"Unable to cast object of type Xto type X"。看到这个问题,真是叫人哭笑不得啊。两个类名完全一样,何谓“无法转换” 。
既然出了问题,就得解决,一步一步跟吧。设好环境,调试控件的设计时行为,运行的结果竟然是完全没有问题。没有问题却成了最大的问题,无从下手了,上网查查吧,还好,发现很多人也都碰到这个问题。在微软的论坛里有一个解决的方法:
This particular problem apparently arises because of several factors:
The IDE caches versions of assemblies in the following folder:
"x\Documents and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies"
And at least in my case doesn't detect the particular assembly containing the type has a new version and it doesn't update the cache.
If you attach to a running instance of VS2005 and check what Modules are loaded (you may need to add the Modules window command from the Debug category in Tools > Customize...) you'll probably see two versions of your assembly are loaded, or even the same version of the assembly loaded twice: in both cases you'll see the same error message.
So first step, delete all folders in ProjectAssemblies. Next, make sure the IDE can only find ONE copy of your assembly, which is easier if you only have one copy of the DLL in your disk, and only one reference to it (or exactly the same reference from all projects) in your solution.
自己动手实践一下,删掉了cache的汇集,重新运行VS2005,问题解决了。
posted @
2006-12-04 10:01 纶巾客 阅读(3561) |
评论 (0) |
编辑