Addin ComboBox OnSelChange fires multiple times with messagebox

2816
5
01-20-2011 12:50 PM
TimWhiteaker
Occasional Contributor
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]
0 Kudos
5 Replies
JoePartlow
New Contributor III
I am experiencing the same symptom.  Did you ever find a resolution to this?
0 Kudos
JeffreyHamblin
New Contributor III
Interesting. It seems the OnSelChange event gets triggered before the combobox list closes up, so clicking on the MessageBox window causes focus to leave the combobox and it closes up without changing the selected index (stays at initial), and then re-enters the event opening a second MessageBox. You can see this easier by dragging the second one off to the side.

Also, while looking for workarounds, I noticed that the OnFocus event is never triggered.

Running the sample provided in the documentation exhibits the same problems:

  ESRI.ArcGIS.Desktop.AddIns ComboBox OnFocus_Method
0 Kudos
PhilBlondin
New Contributor III
Going off memory, on selection changed event will get fired several times.  For instance if a layer get selected and another one de-selected then it fires twice.  Something like that anyway. 

One way to get around this is with the feature layer selection event. 
IFeatureLayerSelectionEvents_Event.  This way it only fires on that layer.
0 Kudos
TimWhiteaker
Occasional Contributor
I am experiencing the same symptom.  Did you ever find a resolution to this?


My solution was to not show the message box.  It wasn't critical for my application.  So, didn't find a real solution.
0 Kudos
JonGarrido
New Contributor III
Hello,

This thread is quite old but I'm writting becouse I've found out the same ¿bug?. I'm working on 10.1 .NET sdk.

When you override the OnSelChange method it fires three times. I tried this, and again the message apears 3 times.

string myvalue;

protected override void OnSelChange(int cookie)
        {
            myvalue = this.Value.ToString();
           doSomethingMethod(myvalue);
        }

void doSomethingMethod(string txt)
{
MessageBox.Show(txt)
}


Finally it worked to me including a if clause like this. I don't know why, but in this way the method fires just once.

string myvalue;

protected override void OnSelChange(int cookie)
        {
            myvalue = this.Value.ToString();
           if (myvalue != "") doSomethingMethod(myvalue);
        }

void doSomethingMethod(string txt)
{
MessageBox.Show(txt)
}
0 Kudos