1

I am having trouble type casting and using the right interfaces. I have a function that loops through all text elements with a specific property set. Once a match is found, I want to be able to select this text element or make it active or highlighted or whatever on the map. Code below.

protected override void OnClick(Item item)
{
IMxDocument pDoc = ValidateInterface.GetMxDocument();
IActiveView pLayout = (IActiveView)pDoc.PageLayout;
IGraphicsContainer pGraphicsCont = (IGraphicsContainer)pDoc.PageLayout;
pGraphicsCont.Reset();

            IElementProperties _ElemProps = null;
            while ((_ElemProps = (IElementProperties)pGraphicsCont.Next()) != null)
            {
                if (_ElemProps.CustomProperty is IPropertySet2)
                {
                    ITextElement _textElement = (ITextElement)_ElemProps;
                    IPropertySet2 _propertySet = (IPropertySet2)_ElemProps.CustomProperty;
                    MessageBox.Show("Before I compare item to string");
                    if (item.Caption == Convert.ToString(_propertySet.GetProperty(NAME_STRING)))
                    {
                        //Problems start here
                        MessageBox.Show("Inside the IF statement");
                        IGraphicsContainerSelect _SelectMyElement = null;   // = (IGraphicsContainerSelect)pGraphicsCont;
                        ITextElement _newTextElement = (ITextElement)pGraphicsCont;
                        IElement TestElement = _newTextElement as IElement;

                        _SelectMyElement.SelectElement(TestElement);
                    }
                }
            }
}

Nothing is being selected though on my map. I'm looping through each graphic element (IGraphicContainer) on the map. Once I find a match, I want to select that Graphic Element. I am trying to utilize IGraphicContainerSelect to do this. It takes an IElement variable type as a parameter, thus why I'm trying to cast it. But again, noting is being selected. This all happens when I click a button.

1

First of all you can't just assign null to _SelectMyElement and then use it. It will cause an exception later. Second pGraphicsCont does not implement ITextElement.

You should uncomment your assignment of _SelectMyElement and select _textElement. You also might need to refresh the display.

    if (item.Caption == Convert.ToString(_propertySet.GetProperty(NAME_STRING)))
                        {
                            //Problems start here
                            MessageBox.Show("Inside the IF statement");
                            IGraphicsContainerSelect _SelectMyElement =(IGraphicsContainerSelect)pGraphicsCont;

                            _SelectMyElement.SelectElement((IElement)_textElement);
                            pLayout.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphicSelection, null, null);
                        }

After the SelectElement you can add a check on how many objects are selected for debugging purposes. If you have items selected but it does not show in the map, try to refresh another draw phase or the entire active view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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