Friday, September 16, 2011

ArcGIS Desktop API: Finding a Style in the ESRI Style Gallery

Here's a handy piece of code for finding a style in the ESRI Style Gallery. You simply pass in the style and the category and it returns the appropriate object (if it exists):
 public static object FindEsriWidget(string style, string category)  
 {  
   try  
   {  
     // style comparision is case insensitive, without leading/trailing spaces  
     string normalizedStyle = style == null ? "" : style.Trim().ToUpper();  
     // these are our interfaces and coclasses  
     IStyleGallery gallery = new StyleGalleryClass();  
     IStyleGalleryItem item = new StyleGalleryItemClass();  
     IStyleGalleryStorage storage = (IStyleGalleryStorage)gallery;  
     IEnumStyleGalleryItem list = null;  
     // get the gallery storage item  
     string path = storage.DefaultStylePath + "ESRI.Style";  
     // what we're interested in  
     list = gallery.get_Items(category == null ? "" : category, path, null);  
     // reset our cursor  
     list.Reset();  
     // enumerate the list as required  
     for (item = list.Next(); item != null; item = list.Next())  
     {  
       // if we find a match, use it  
       if (item.Name.Trim().ToUpper().Equals(normalizedStyle))  
       {  
         return item.Item;  
       }  
     }  
   }  
   catch (Exception ex)  
   {  
      // do something here  
   }  
   // not found  
   return null;  
 }  

No comments: