我有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