Code Red > .NET
check if tab exists
sybold:
i'm trying to check if a custom tab is in the ribbon
if it is in the ribbon -> exit
if it is not in the ribbon -> add tab
i'm missing something or doing it completely wrong
--- Code: --- <CommandMethod("DOCRIB")> _
Public Sub DOCRIBCMD()
Dim tabName As String = "DocumentTab"
Dim ribCntrl As Autodesk.Windows.RibbonControl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl
For Each tab As Autodesk.Windows.RibbonTab In ribCntrl.Tabs
If tab.Title.Contains(tabName) = False Then
Try
Dim ribbon As New RIBBON()
ribbon.Register()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
_ed.WriteMessage(vbCrLf & ex.Message)
End Try
Else
_ed.WriteMessage("already there")
Exit Sub
End If
Next
End Sub
--- End code ---
BlackBox:
There may be a better way of doing this (particularly when testing for an existing tab), but this is the best I can do with the little I know of .NET development.
This code iterates the RibbonTabCollection to check... checks for the defined RibbonTab, and if it exists, sets it active. If the RibbonTab is not found, then the RibbonTab is created and set active.
Pseudo code:
** Edit - Revised code to use the FindTab Method (thanks, Jeff!)
--- Code - C#: --- using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Runtime;using Autodesk.Windows; using System; // This line is not mandatory, but improves loading performances[assembly: CommandClass(typeof(FOO.MyCommands))] namespace FOO{ public class MyCommands { private Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; // Jeff_M is awesome: // http://www.theswamp.org/index.php?topic=32869.msg383525#msg383525 [CommandMethod("DOCRIB")] public void DOCRIBCMD() { // Defin ribbon properties String myTabId = "DOCTAB"; String myTabTitle = "Document"; Boolean myTabIsContextual = true; try { RibbonControl oRibbon = ComponentManager.Ribbon; if (oRibbon != null) { RibbonTab oTab = oRibbon.FindTab(myTabId); if (oTab != null) { oTab.IsActive = true; } else { oTab = new RibbonTab(); oTab.Title = myTabTitle; oTab.Id = myTabId; oTab.IsContextualTab = myTabIsContextual; oTab.IsVisible = true; oRibbon.Tabs.Add(oTab); oTab.IsActive = true; // <-- Add your ribbon panel(s) here } } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\n** Error: " + ex.Message + " ** "); } } }}
Also, here are some references that you may find useful:
http://forums.autodesk.com/t5/NET/Create-custom-ribbon-tab-and-buttons-for-Autocad-mechanical-2011/td-p/2834343
http://adndevblog.typepad.com/autocad/2012/04/displaying-a-contextual-tab-upon-entity-selection-using-ribbon-runtime-api.html
http://spiderinnet1.typepad.com/blog/2012/06/autocad-cui-ribbon-net-add-ribbon-panel-to-existing-ribbon-tab.html
HTH
sybold:
great! thanks for the help, I now realize what i did wrong.
it now works, like i needed it.
--- Code: ---<CommandMethod("DOCRIB")> _
Public Sub DOCRIBCMD()
Dim ribCntrl As Autodesk.Windows.RibbonControl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl
Dim flag As Boolean = False
For Each tab As RibbonTab In ribbonCtrl.Tabs
If tab.Id.ToUpper() = "DOCTAB" Then
tab.IsActive = True
flag = True
_ed.WriteMessage("Tab already added")
End If
Next
If flag = False Then
Dim ribbon As New RIBBON()
ribbon.Register()
_ed.WriteMessage("Tab added")
End If
End Sub
--- End code ---
BlackBox:
--- Quote from: sybold on July 13, 2012, 06:58:16 am ---great! thanks for the help, I now realize what i did wrong.
--- End quote ---
Happy to help. :-)
FWIW - I am no expert at Exception handling myself, and I have not researched which Exceptions can be result from iterating the RibbonTabCollection, or even modifying a RibbonTab for that matter, but at minimum, you really should get in the habit of incorporating a Try, Catch [, Finally?] block, methinks. ;-)
BlackBox:
I've added C# code here, for those that may be interested.
Navigation
[0] Message Index
[#] Next page
Go to full version