Combobox on a Toolbar

3624
11
09-14-2010 07:27 AM
BBulla
by
Occasional Contributor
Hi,

I'd like to add a combox to a Toolbar I have created.  I understand how to add items to the toolbar, but I'm having difficulty understanding how to add a combobox (like the one you might see on the Edit Menu).  I'll need the combobox to have a list of items in it.

This is what I'm trying to do, but it's not working:

       
public DSM_Toolbar()
        {
            //
            // TODO: Define your toolbar here by adding items
            //

            AddItem("DSM_DocumentManager.DocumentManager");
            AddItem("DSM_Symbology.Symbology");
            AddItem("DSM_QCTool.QCTool");

            BeginGroup(); //Separator

            AddItem("DSM_CalcMetric.CalcMetric");
            AddItem("DSM_GIFAngle.GIF_Angle");
            AddItem("DSM_Attribute_Update.AttributeUpdater");
            
            //The ComboBox
            ComboBox combo = new ComboBox();
            combo.Items.Add("25");
            combo.Items.Add("50");

            AddItem(combo);  //this wont work though....how do I do this???
        }
0 Kudos
11 Replies
ThavitinaiduGulivindala
Occasional Contributor
0 Kudos
JohnHauck
Occasional Contributor II
The following sample demonstrates how you can go about this:

GraphicsLayerTool Sample
0 Kudos
JamesCrandall
MVP Frequent Contributor
I have had great success in building a UserControl for these kind of ToolBar components.
0 Kudos
BBulla
by
Occasional Contributor
Thanks for the responses.  I've implemented a solution based on jmhauck's posting.  I'm noticing one strange thing though.  Initially the toolbar opens up fine and everything is visible.  But once I start an edit session, the combobox dissapears.  It is still there, you just can't see it.  If you click on where the control should be, it then appears and you can select from the drop down list, and use it normally.

Why is it dissapearing when I start an edit session??

Here is my code for creating the combobox:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;

namespace DSM_ServiceConnection_Toolbar
{
    /// <summary>
    /// Summary description for DiametersComboBoxToolCtrl.
    /// </summary>
    [Guid("135e9481-2245-4411-866b-de687b51c512")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("DSM_ServiceConnection_Toolbar.DiametersComboBoxToolCtrl")]
    public sealed class DiametersComboBoxToolCtrl : BaseCommand, IToolControl
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);

        }

        #endregion
        #endregion
        
        private IHookHelper m_hookHelper = null;
        private DiametersComboBoxCtrl m_diametersComboBoxCtrl = null;

        //private IApplication m_application;

        public DiametersComboBoxToolCtrl()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "DSM Service Connection Tools"; //localizable text
            base.m_caption = "Diameters ComboBox";  //localizable text
            base.m_message = "";  //localizable text 
            base.m_toolTip = "";  //localizable text 
            base.m_name = "Diameters ComboBox";   //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overriden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            //make sure that the usercontrol has been initialized
            if (null == m_diametersComboBoxCtrl)
            {
                m_diametersComboBoxCtrl = new DiametersComboBoxCtrl();
                m_diametersComboBoxCtrl.CreateControl();
            }
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add DiametersComboBoxToolCtrl.OnClick implementation
        }

        #endregion

        #region IToolControl Members

        public bool OnDrop(esriCmdBarType barType)
        {
            return true;
        }

        public void OnFocus(ICompletionNotify complete)
        {

        }

        public int hWnd
        {
            get
            {
                //pass the handle of the usercontrol
                if (null == m_diametersComboBoxCtrl)
                {
                    m_diametersComboBoxCtrl = new DiametersComboBoxCtrl();
                    m_diametersComboBoxCtrl.CreateControl();
                }

                return m_diametersComboBoxCtrl.Handle.ToInt32();

            }
        }

        #endregion
    }
}
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks for the responses.  I've implemented a solution based on jmhauck's posting.  I'm noticing one strange thing though.  Initially the toolbar opens up fine and everything is visible.  But once I start an edit session, the combobox dissapears.  It is still there, you just can't see it.  If you click on where the control should be, it then appears and you can select from the drop down list, and use it normally.

Why is it dissapearing when I start an edit session??




You need to add your controls to a Panel and Refresh the panel in the _Paint event.  Just not real sure how you are going to accomplish this with the approach you have decided on.

...Like I said, I've had great success (ie, much easier to implement) with adding my ToolBar items (combobox, textbox, button controls) to a UserControl, and implement this UserControl in the ICommand object.


j
0 Kudos
BBulla
by
Occasional Contributor
Hi James,

Thanks for the Panel tip.  That worked perfectly and was very easy to implement.  The method I am doing this with is the same as yours....using a UserControl.

Thanks!!
0 Kudos
JamesCrandall
MVP Frequent Contributor
Hi James,

Thanks for the Panel tip.  That worked perfectly and was very easy to implement.  The method I am doing this with is the same as yours....using a UserControl.

Thanks!!


Glad it worked for you!
0 Kudos
BBulla
by
Occasional Contributor
One more quick question.  I have some Tools that when I click them, I need to change the SelectedItem in the ComboBox from the OnClick event of the tool.  How do I access the ComboBox from the Tool??  I've set the Modifiers of the ComboBox to public, but I can't seem to get to it.
0 Kudos
ThavitinaiduGulivindala
Occasional Contributor
Check with static keyword.
0 Kudos