twhiteaker

Addin ComboBox OnSelChange fires multiple times with messagebox

Discussion created by twhiteaker on Jan 20, 2011
Latest reply on Feb 17, 2013 by jongarrido
When I show a message box in the OnSelChange event of a ComboBox in an add-in, the OnSelChange event fires two more times, and it changes the selected item to the first item in the ComboBox.  Note that when I remove the line with the message box, it doesn't revert to the first item in the list, and the event only seems to be firing once.

Environment: Visual Studio 10, Windows 7 64-bit, ArcGIS 10.0, .NET 3.5, c#.

I used the ArcGIS Add-Ins Wizard to create the class.  The only other item in my add-in is a toolbar on which the combo box is placed. 

How do I prevent the event from firing multiple times? 

Here's the entire code from my combo box class.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ComboBoxTest
{
    public class Combo1 : ESRI.ArcGIS.Desktop.AddIns.ComboBox
    {
        private bool m_updating;

        public Combo1()
        {
            m_updating = true;

            int firstCookie = Add("Items");
            Select(firstCookie);

            for (int i = 1; i < 11; i++)
            {
                Add(i.ToString());
            }

            m_updating = false;
        }

        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }

        protected override void OnSelChange(int cookie)
        {
            if (m_updating == false)
            {
                // Added reference to System.Windows.Forms
                System.Windows.Forms.MessageBox.Show(Value);
            }

            base.OnSelChange(cookie);
        }
    }

}



And here's Config.esriaddinx.

[HTML]<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>ComboBoxTest</Name>
  <AddInID>{fe058015-ea6a-47ae-be86-9a92d47bfcad}</AddInID>
  <Description>This add-in tests for unwanted combo box selection when message boxes are shown in the OnSelChanged event.</Description>
  <Version>1.0</Version>
  <Image>Images\ComboBoxTest.png</Image>
  <Author>Tim Whiteaker</Author>
  <Company>The University of Texas at Austin</Company>
  <Date>1/20/2011</Date>
  <Targets>
    <Target name="Desktop" version="10.0" />
  </Targets>
  <AddIn language="CLR" library="ComboBoxTest.dll" namespace="ComboBoxTest">
    <ArcMap>
      <Commands>
        <ComboBox id="The_University_of_ComboBoxTest_Combo1" class="Combo1" message="Add-in command generated by Visual Studio project wizard." caption="Choose Number: " tip="Change the combo box item to show a message box." category="Add-In Controls" image="Images\Combo1.png" editable="false" sizeString="WWWWWWWWWW" showCaption="true" />
      </Commands>
      <Toolbars>
        <Toolbar id="The_University_of_ComboBoxTest_My_Toolbar" caption="My Toolbar" showInitially="true">
          <Items>
            <ComboBox refID="The_University_of_ComboBoxTest_Combo1" />
          </Items>
        </Toolbar>
      </Toolbars>
    </ArcMap>
  </AddIn>
</ESRI.Configuration>

[/HTML]

Outcomes