0

I honestly have no clue what is causing this.

I want to have a message appear that tells me which layers are selected or not.

Here is my code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.ArcMap;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using System.Windows.Forms;

namespace SelectedLayersTest2
{
    public class Button1 : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public Button1()
        {
        }

        protected override void OnClick()
        {

            IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
            IMap map = mxdoc.FocusMap;

            ILayer layer;
            string message = "";

            for (int i = 0; i < map.LayerCount; i++)
            {
                layer = map.Layer[i];
                if (mxdoc.SelectedLayer == layer)
                {
                    message += layer.Name + "\n";
                }
                else
                {
                    message += layer.Name + " not selected\n";
                }
            }

            Message msgForm = new Message();
            msgForm.lbl.Text = message;
            msgForm.ShowDialog();

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

}

This seems like such a harmless piece of code. What is causing this?

2
  • 3
    Have you tried debugging it? Have you tried adding output / logging statements to find out where it gets up to?
    – BradHards
    Commented Jan 25, 2015 at 8:10
  • I had try catch statements set up, but got rid of them because they didn't seem to be doing anything. The crash seems to occur at the same time no matter what. I have had this problem with several simple addins I have been trying. I suspect my novice C# /.NET/ArcObjects skills have something to do with it.
    – TheLastGIS
    Commented Jan 25, 2015 at 8:45

2 Answers 2

2

The following code fixed it for me. Change this:

Message msgForm = new Message();
msgForm.lbl.Text = message;
msgForm.ShowDialog();

To this:

MessageBox.Show(message);
2
  • I had the same result. But why wont the original code work?
    – TheLastGIS
    Commented Feb 2, 2015 at 8:38
  • Well, your original code doesn't even compile for me. Visual Studio tells me that ".lbl" is not a method for "Message". I don't usually code in C#, so I don't know if this should work and I'm just missing a reference, or if it's just an incorrect way to do it. If it normally does work, you could probably ask why it doesn't in this case over on Stack Overflow. My knowledge about this is pretty much exhausted at this point. Commented Feb 2, 2015 at 14:09
1

I don't program in C# so may be what I am going to suggest is actually valid syntax for c#? I think the problem is this line:

if (mxdoc.SelectedLayer == layer)

If VB .net one would not compare two layer objects this way. If this was me using VB I would have done it this way:

if mxdoc.SelectLayer.Name = layer.Name then
    msgbox "same!"
end if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.