[英]Class Referencing with multiple instances accessing Custom Properties
我有一個條件,我有一個用戶單擊一個按鈕,它將啟動一個自定義控件。 基本控件是一個TabPage。
CustomControl1 cust = new CustomControl1();
我的CustomControl中有一個屬性,就像這樣:
private string ButtonName = string.Empty;
public string buttonclicked
{
get { return ButtonName; }
}
我只是沒有適當地考慮這個問題,或者我錯過了一些東西。
如果我嘗試引用該控件,則將獲得最后的控件值,而不是正在使用的控件。 我嘗試了各種各樣的方法,只是無法獲得所選的選項卡來返回值。
foreach(Tabpage page in this.TabControl1.TabPages)
{
if(page.name == this.TabControl1.SelectedTab.Name)
{
MessageBox.Show(cust.buttonclicked);
}
}
我總是得到最后添加的控件,而不是選擇的控件。 贊賞的建議,謝謝。
在循環中,您指的是cust
但這不是循環變量。 據推測,您覆蓋cust
當您創建一個新的自定義控制。
如果控件實際上是選項卡,則需要引用該選項卡。類似:
var custPage = (CustomControl1)TabControl1.SelectedTab;
MessageBox.Show(custPage.buttonclicked);
由於TabControl1.SelectedTab
的類型為TabPage
,但是您知道它實際上是TabPage
名為CustomControl1
的子類,因此您希望將其CustomControl1
為CustomControl1
(這就是(CustomControl1)
完成的工作)。
然后,您可以訪問在CustomControl1
定義的成員-例如buttonclicked
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.