我有2个标签,在用户控件的属性: 这里是物业:用户控件自定义事件,并获取属性
private int _SelectIndex;
[Browsable(true)]
public int SelectIndex { get; set; }
和2标签:
Label lbl1, lbl2;
void iControl()
{
lbl1 = new Label();
lbl2 = new Label();
lbl1.Name = "lbl1";
lbl2.Name = "lbl2";
lbl1.Click += lbl_Click;
lbl2.Click += lbl_Click;
this.Controls.Add(lbl1);
this.Controls.Add(lbl2);
}
点击:
void lbl_Click(object sender, EventArgs e)
{
Label selectedlbl = sender as Label;
if(selectedlbl.Name == "lbl1")
this.Select = 1;
else
this.Select = 2;
}
上课活动:
在我的控制public class SelectEventArgs : EventArgs
{
private int index;
public SelectEventArgs(int index)
{
this.index = index;
}
public int ItemIndex
{
get
{
return index;
}
}
}
自定义事件:
public event EventHandler SelectEvent;
protected virtual void OnSelectEvent()
{
if (SelectEvent!= null)
SelectEvent(this, new SelectEventArgs(this._SelectIndex));
}
我需要在MainForm的一个事件来获取和设置属性值如下:
int index = 0;
public Form1()
{
InitializeComponent();
this.icontrol = new iControl();
this.SelectEvent += Select();
}
void Select(object sender, SelectItem e)
{
//use this to set value of Select
this.icontrol.SelectIndex = index;
//and this to get value of Select
index = this.icontrol.SelectIndex;
}
选择是空的。 如何让它工作?
什么部分是在这里失败,通过它的外观这将工作。 – jbmintjb
我不知道我可以添加一个事件为[email protected] – Jandy
啊列表框,没有提及那一个!我的答案如下。 – jbmintjb