none
Visio 2010 SelectionChanged Event Stops Working RRS feed

  • Question

  • Hi there,

    I've got a Windows Forms applications that's making use of a Visio ActiveX Control.

    I handle the SelectionChanged event of the control in order to assign some properties to a selected element, i.e. everytime a user selects a shape in a diagram he can see a list of corresponding properties in a PropertyGrid control. Also, those properties are not directly related to shapes located in a diagram, except for the Text property, instead I have a list of elements that are associated to those shapes and I modify properties of those elements.

    However, after a few modifications of some elements the SelectionChanged event stops working. Although it's still visible than different shapes get selected as they are highlighted in a diagram the event handler stops getting executed.

    Basically, I just want to get any hints on what may be causing that behaviour. Please, note I run my application in the debug mode and receive no exceptions.

    Wednesday, March 16, 2011 3:01 PM

All replies

  • I think I'd better provide some source code in order to better illustrate the problem.

    The event stops firing after I select an item from the ComboBox in the PropertyGrid.

    I know that actually I should preload values from a database instead of retrieving them each time a user selects a new element but unfortunately it didn't solve a problem when I tried doing that.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Visio = Microsoft.Office.Interop.Visio;
    
    namespace VisioSelectionBug
    {
      public partial class Form1 : Form
      {
        //list for keeping diagram elements with some annotations
        private List<Element> _elements = new List<Element>();
    
        public Form1()
        {
          InitializeComponent();
          axDrawingControl1.DocumentOpened += new AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEventHandler(axDrawingControl1_DocumentOpened);
          axDrawingControl1.Window.SelectionChanged += new Microsoft.Office.Interop.Visio.EWindow_SelectionChangedEventHandler(Window_SelectionChanged);
        }
    
        private void axDrawingControl1_DocumentOpened(object sender, AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEvent e)
        {
          //adding each shape from a just opened diagram to the collection
          foreach (Visio.Shape shape in axDrawingControl1.Window.Application.ActivePage.Shapes)
          {
            _elements.Add(new Element { Id = shape.ID });
          }
        }
    
        private void Window_SelectionChanged(Microsoft.Office.Interop.Visio.Window Window)
        {
          MessageBox.Show("SelectionChanged");
    
          propertyGrid1.SelectedObject = _elements.Find(x => x.Id == Window.Selection.PrimaryItem.ID);
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          axDrawingControl1.Src = "Diagram.vsd";
        }
      }
    
      public class Element
      {
        public int Id { get; set; }
    
        [TypeConverter(typeof(ProductCategoryConverter))]
        public ProductCategory ProductCategory { get; set; }
      }
      
    
      //converter for the PropertyGrid
      public class ProductCategoryConverter : ExpandableObjectConverter
      {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
          //true means show a combobox
          return true;
        }
    
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
          //true will limit to list. false will show the list, 
          //but allow free-form entry
          return true;
        }
    
    
    
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
          AdventureWorksLT2008Entities entities = new AdventureWorksLT2008Entities();
    
          List<ProductCategory> categories = (from c in entities.ProductCategories
                            select c).ToList();
    
          return new StandardValuesCollection(categories);
    
        }
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
          if (sourceType == typeof(string))
          {
            return true;
          }
    
          return base.CanConvertFrom(context, sourceType);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
          if (value is string)
          {
            AdventureWorksLT2008Entities entities = new AdventureWorksLT2008Entities();
    
            ProductCategory category = (from c in entities.ProductCategories
                          where c.Name == value
                          select c).Single();
    
            return category;
          }
    
          return base.ConvertFrom(context, culture, value);
    
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
          if (value is ProductCategory)
          {
            return ((ProductCategory)value).Name;
          }
    
          return base.ConvertTo(context, culture, value, destinationType);
        }
      }
    }
    
    

     

    Thursday, March 17, 2011 9:51 AM
  • Okay, I seem to have solved the problem by handling axDrawingControl1.SelectionChanged instead of handling axDrawingControl1.Window.SelectionChanged.

    Anyway, does anybody have an idea why it was happening?

    • Marked as answer by Mike Borozdin Thursday, March 17, 2011 10:05 AM
    • Unmarked as answer by Mike Borozdin Thursday, March 17, 2011 10:05 AM
    Thursday, March 17, 2011 10:05 AM
  • Hi Mike,

    Thanks for posting in the MSDN Forum.

    Would you please tell me where I can find axDrawingControl? I can add a axVisioView control in to my Winform application but I can not find axDrawingControl.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 3:46 AM
    Moderator
  • It's called Microsoft Office Visio 14.0* Drawing Control and it's located in the COM Components.

    I found some tutorials on using that control. There is even an MSDN Magazine article on the same control -http://msdn.microsoft.com/en-us/magazine/cc164043.aspx

    * if you have Visio 2010, otherwise you will have a different version number 

    Friday, March 18, 2011 8:19 AM