如何循环得到Ribbon控件里所有的ribbon tabs,ribbon bar and button item 控件
How to loop through all ribbon tabs, ribbon bar and items on WinForms Ribbon control
If you need to loop through all ribbon tabs, ribbon bars and items on each ribbon bar here is how to do it.
C#:
VB:
For Each item As BaseItem In ribbonControl1.Items
Dim ribbonTab As RibbonTabItem = TryCast(item, RibbonTabItem)
If ribbonTab IsNot Nothing Then
Dim panel As RibbonPanel = ribbonTab.Panel
For Each panelControl As Control In panel.Controls
Dim ribbonBar As RibbonBar = TryCast(panelControl, RibbonBar)
If ribbonBar IsNot Nothing Then
' At this point you can simply disable each RibbonBar and that will disable all items on it
'ribbonBar.Enabled = false;
' Here is how you loop through items on RibbonBar
For Each ribbonBarItem As BaseItem In ribbonBar.Items
ribbonBarItem.Enabled = False
Next
End If
Next
End If
Next
C#:
foreach (BaseItem item in ribbonControl1.Items)
{
RibbonTabItem ribbonTab = item as RibbonTabItem;
if (ribbonTab != null)
{
RibbonPanel panel = ribbonTab.Panel;
foreach (Control panelControl in panel.Controls)
{
RibbonBar ribbonBar = panelControl as RibbonBar;
if (ribbonBar != null)
{
// At this point you can simply disable each RibbonBar and that will disable all items on it
//ribbonBar.Enabled = false;
// Here is how you loop through items on RibbonBar
foreach (BaseItem ribbonBarItem in ribbonBar.Items)
{
ribbonBarItem.Enabled = false;
}
}
}
}
}
VB:
For Each item As BaseItem In ribbonControl1.Items
Dim ribbonTab As RibbonTabItem = TryCast(item, RibbonTabItem)
If ribbonTab IsNot Nothing Then
Dim panel As RibbonPanel = ribbonTab.Panel
For Each panelControl As Control In panel.Controls
Dim ribbonBar As RibbonBar = TryCast(panelControl, RibbonBar)
If ribbonBar IsNot Nothing Then
' At this point you can simply disable each RibbonBar and that will disable all items on it
'ribbonBar.Enabled = false;
' Here is how you loop through items on RibbonBar
For Each ribbonBarItem As BaseItem In ribbonBar.Items
ribbonBarItem.Enabled = False
Next
End If
Next
End If
Next