Author Topic: Need Ribbon ComboBox Example  (Read 6813 times)

0 Members and 1 Guest are viewing this topic.

elliottpd

  • Guest
Need Ribbon ComboBox Example
« on: March 03, 2012, 11:54:01 AM »
I have been looking for an example to help me add a tab to the AutoCAD ribbon, that includes a panel with a combobox.  What I have in mind is being able to make a couple of selections with comboboxes, then click a button to start a command with the combobox selections as arguments for the command.  Can you share an example?

Irvin

  • Guest
Re: Need Ribbon ComboBox Example
« Reply #1 on: March 05, 2012, 03:38:12 AM »
Hi there,

Hope this will get you going:

Code: [Select]
using System;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using Autodesk.Windows;

using AutoCAD = Autodesk.AutoCAD.ApplicationServices.Application;
using Ribbon = Autodesk.AutoCAD.Ribbon.RibbonServices;

using Kraan.Btm.Forms;

[assembly: ExtensionApplication(typeof(Kraan.Btm.Application))]
namespace Kraan.Btm
{
    public class RibbonComboSample : IExtensionApplication
    {
        private RibbonCombo _ribCom = new RibbonCombo();

        void IExtensionApplication.Initialize()
        {
            AutoCAD.SystemVariableChanged +=
                new SystemVariableChangedEventHandler(Application_SystemVariableChanged);

            //Check if Netloaded or loaded on startup
            //If dll is load on autocad startup Ribbon is not created yet. Setup event to watch if ribbon is created.
            if (Autodesk.Windows.ComponentManager.Ribbon == null)
            {
                Autodesk.Windows.ComponentManager.ItemInitialized +=
                    new EventHandler<RibbonItemEventArgs>(ComponentManager_ItemInitialized);
            }
            else
            {
                //dll is netloaded is avalible create the ribbon.
                CreateRibbon();
            }
        }

        void IExtensionApplication.Terminate()
        {
            AutoCAD.SystemVariableChanged -= new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(Application_SystemVariableChanged);
        }
             
        private void Application_SystemVariableChanged(object sender, SystemVariableChangedEventArgs e)
        {
            if (e.Name == "WSCURRENT")
            {
                CreateRibbon();
            }
        }

        private void CreateRibbon()
        {
            try
            {
                RibbonControl ribCntrl = Ribbon.RibbonPaletteSet.RibbonControl;

                if (ComponentManager.Ribbon == null) return;

                for (int i = 0; i < ribCntrl.Tabs.Count; i++)
                {
                    if (ribCntrl.Tabs[i].Id.Equals("MyID")) return;
                }

                //add the tab
                RibbonTab ribTab = new RibbonTab();
                ribTab.Title = "My New RibbonTab";
                ribTab.Id = "MyID";
                ribCntrl.Tabs.Add(ribTab);

                //create and add the panel
                CreateRibbonPanel(ribTab);
            }
            catch(SystemException ex)
            {
            }
        }

        public void CreateRibbonPanel(RibbonTab ribTab)
        {
            //create the panel source
            RibbonPanelSource _ribSourcePanel = new RibbonPanelSource();
            _ribSourcePanel.Title = "New Ribbon Panel";
            //now the panel
            RibbonPanel ribPanel = new RibbonPanel();
            ribPanel.Source = _ribSourcePanel;
            ribTab.Panels.Add(ribPanel);

            #region RibbonButtons

            RibbonButton ribBut01 = new RibbonButton();
            ribBut01.Text = "Combo Item 01";
            ribBut01.Id = "Item01";
            ribBut01.ShowText = true;
            //ribBut01.ShowImage = true;
            //_rbNewSymbol.LargeImage = Img.getBitmap(Properties.Resources.New_Symbol_32);
            //_rbNewSymbol.Orientation = System.Windows.Controls.Orientation.Vertical;

            RibbonButton ribBut02 = new RibbonButton();
            ribBut02.Text = "Combo Item 02";
            ribBut02.Id = "Item02";
            ribBut02.ShowText = true;
            //ribBut01.ShowImage = true;
            //_rbNewSymbol.LargeImage = Img.getBitmap(Properties.Resources.New_Symbol_32);
            //_rbNewSymbol.Orientation = System.Windows.Controls.Orientation.Vertical;

            # endregion

            //Set RibbonCombo Properties
            _ribCom.Text = "";
            _ribCom.ShowText = false;
            _ribCom.Width = 200;

            //Populate the RibbonCombo
            _ribCom.Items.Add(ribBut01);
            _ribCom.Items.Add(ribBut02);


            RibbonRowPanel RowPanel = new RibbonRowPanel();
            RowPanel.Items.Add(_ribCom);
        }

        [CommandMethod("GetCurrentRibbonComboItem")]
        public void GetCurrentRibbonComboItem()
        {
            if (_ribCom.Items.Count != 0)
            {
                RibbonButton but = (RibbonButton)_ribCom.Current;
                switch (but.Id)
                {
                    case "Item01":
                        AutoCAD.ShowAlertDialog("Ribbon Item 01 is selected");
                        break;
                    case "Item02":
                        AutoCAD.ShowAlertDialog("Ribbon Item 01 is selected");
                        break;
                    default:
                        AutoCAD.ShowAlertDialog("Error Getting Current RibbonCombo Item");
                        break;
                }
            }
        }

        void  ComponentManager_ItemInitialized(object sender, RibbonItemEventArgs e)
        {
            if (Autodesk.Windows.ComponentManager.Ribbon != null)
            {
                //ok, create Ribbon
                CreateRibbon();
                //and remove the event handler
                Autodesk.Windows.ComponentManager.ItemInitialized -= new EventHandler<RibbonItemEventArgs>(ComponentManager_ItemInitialized);
            }
        }
    }
}

It's a shame when a button is added to a combobox it can't use the commandhandler of the button. It's has some events when. CurrentItemChanged. This way you can set variables in your application.

Again hope it helps. Code is not tested it's copy pasted from my application.

Kind regards,

Irvin

elliottpd

  • Guest
Re: Need Ribbon ComboBox Example
« Reply #2 on: March 05, 2012, 08:32:25 AM »
Thank you Irvin!  This is just what I was looking for.
You are a scholar and a gentleman, and I appreciate your sharing.

vegbruiser

  • Guest
Re: Need Ribbon ComboBox Example
« Reply #3 on: March 13, 2012, 11:47:30 AM »
Hi Irvin,

I've been trying to use your example with AutoCAD 2011 and I can't seem to get past this section:

Code: [Select]
//Check if Netloaded or loaded on startup
            //If dll is load on autocad startup Ribbon is not created yet. Setup event to watch if ribbon is created.
            if (Autodesk.Windows.ComponentManager.Ribbon == null)
            {
                Autodesk.Windows.ComponentManager.ItemInitialized +=
                    new EventHandler<RibbonItemEventArgs>(ComponentManager_ItemInitialized);
            }
            else
            {
                //dll is netloaded is avalible create the ribbon.
                CreateRibbon();
            }

I should add that I've converted the above to VB.NET (for a non-C# savvy colleague to use):

Code: [Select]
If Autodesk.Windows.ComponentManager.Ribbon Is Nothing Then
                Autodesk.Windows.ComponentManager.ItemInitialized += New EventHandler(Of RibbonItemEventArgs)(AddressOf ComponentManager_ItemInitialized)
            Else
                createRibbon()
            End If

and I get the following error message:

Code: [Select]
Error 1 'Public Shared Event ItemInitialized(sender As Object, e As Autodesk.Windows.RibbonItemEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

I'm pretty sure something is getting lost in the translation (using one of the many online converters)

Any thoughts?

vegbruiser

  • Guest
Re: Need Ribbon ComboBox Example
« Reply #4 on: March 13, 2012, 12:19:36 PM »
Actually, putting a "Do While Loop" in the createRibbon() Method seems to work correctly:
Do
'wait for the ribbon to fully load
Loop While Autodesk.Windows.ComponentManager.Ribbon Is Nothing


Putting "_RIBBON" in my .scr file before netloading my .dll works but is a horrible kludge..  :x