net winfrom 中我要继承combobox 写个自定义控件,直接根据设置的自定义属性去数据库读取数据进行绑定,直接拖到From中就能用,打开From时直接显示绑定数据,现在我属性设好了, 应该在combobox 中的哪个事件中进行绑定操作,放在构造函数里根本不能获得设置的自定义属性值,就不能根据属性值去数据库获得绑定项!请高手帮帮忙,在web里的dropdownlist有个load事件, 但winfrom里的 combobox我就不知道什么事件了
调试欢乐多
public class MyComboBox : Combox
{
public MyComboBox() : base()
{ ... }
}
public class MyComboBox : ComboBox
{
public MyComboBox() : base()
{
BindData();
}
public object CustomProperty
{
get { ... }
set { ...; BindData(); }
} public void BindData()
{
...
}
}
{
get { ... }
set { ...; BindData(); }
}
都要BindData(); 控件加载好得绑定多少次啊!
当然不是
(1)只有会改变数据源的需要。比如字体改变完全没有必要刷新;
(2)
public void BindData()
{
...
}
的写法很灵活,比如你可以不用清空列表,而直接添加修改,或者定义一个叫 m_preData 的变量,保存当前的列表数据,然后比较,如果没有变化,就不更新。
客户端在 reqdat 事件里面完成赋值。public class MyComboBox : ComboBox
{
public delegate void RequestDataEventHandler(object sender, EventArgs e); public event RequestDataEventHandler reqdat; protected void RaiseRequestDataEvent(object sender, EventArgs e)
{
if (reqdat != null) reqdat(sender, e);
}
public MyComboBox() : base()
{
RaiseRequestDataEvent(this, new EventArgs());
BindData();
} public void BindData()
{
...
}
}
{
public kgdComboBox()
{
if (this.DesignMode == false)
{
string[] str = new string[10];
for (int i = 0; i < 10; i++)
{
str[i] = "kgd" + i.ToString();
}
this.Items.AddRange(str);
}
}
}
楼主可以仿照我给你的这个小程序来模仿,在控件的构造函数里面判断kgdComboBox是否在DesignMode,如果不是,就执行数据库绑定操作,你运行程序,便可以得到你想要的效果,不信自己试一下,如果成功别忘加分哟。
if (this.DesignMode == false)
{
。
}
中不能获得自定义属性值