2012-03-14 19 views
1

我使用下面的代碼來啓用/禁用我的usercontol中的按鈕控件,該工具完美工作。如何在UserControl的工具欄中查找toolstripButton

var btnAdd = this.userControlCommonTask1.Controls.Find("btnAdd", true); 
btnAdd[0].Enabled = true; 

但是,當我用按鈕(btnAdd,btnEdit,btndelete蝕刻..)toostrip(toolstrip1),並使用上述

enter image description here

我的代碼有:

Index was outside the bounds of the array. 

我試過這個,但它只適用於工具條。

 var btnAdd = this.userControlCommonTask1.Controls.Find("toolstrip1", true); 
     btnAdd[0].Enabled = true; 

感謝問候

+0

雖然我的評論有點偏離軌道,但我想知道,假設您在用戶控件中使用工具條,將條目添加到工具條之後,如果複製整個usercontrol並將其粘貼到其他位置,則是它保存工具條的項目?如果是,那麼你如何實現它?問候! – priyamtheone 2013-02-18 18:17:12

回答

3

我已經解決我的問題:

 var toolstrip1 = this.userControlCommonTask1.Controls.Find("toolstrip1", true); 
     var toolstrip1Items = toolstrip1[0] as ToolStrip; <-- set to toolstrip control 

     var btnRead = toolstrip1Items.Items.Find("btnRead", true); <--get BtnRead on toolstrip Item.Find 
     btnRead[0].Enabled = false; <--disable/Enable btn 

這可能是對其他開發人員的參考。

乾杯!

0

嘗試0而不是1的陣列是基於零

+0

我已經改變它,但不工作.. – BizApps 2012-03-14 02:57:48

+0

是btnAdd.Count == 0? – 2012-03-14 02:58:49

+0

是的,它的0 ... btnAdd是工具欄中的Toostrip按鈕項.. – BizApps 2012-03-14 03:04:05

0

工具條是另一個用戶控件。嘗試參考它,然後找到它的子控件

即。 ctlTooolStrip.Controls.Find( 「BtnAdd」,TRUE);

也可以嘗試toolStrip.Items

+0

我可以看到你已經解決了toolstrip.items的問題。很久以後我發佈了這個。 ! – 2012-03-14 03:34:20