How to: Change the Display Name of a Host Item

When an add-in developer changes the name of an item in the host application UI, your code can handle an event that is raised when the item is renamed and change the display name of the host item in the project. This helps the add-in developer keep track of how an item that appears in the UI of the host application maps to a host item that appears in the project.

To change the display name of a host item, get the host adapter of the Visual Studio Tools for Applications IDE. You can then use the host adapter to access a collection of project host items. For more information, see Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project.

The code used in these procedures is taken from the VstaDesignTimeIntegration.cs file of the ShapeAppDynamicProgrammingModelCSharp sample. For more information, see How to: Build and Run the ShapeAppDynamicProgrammingModelCSharp Sample.

To change the display name of a host item

  1. Get the host adapter by using the Extender property of the Project instance.

    IVstaHostAdapter hostAdapter = 
        (IVstaHostAdapter)project.get_Extender("VSTAHostAdapter2007");
    
  2. Get an object that implements the IVstaHostItem interface by using the ProgrammingModelHostItem property of the ProjectHostItem.

    To change the display name of the host item, set the DisplayName property of the IVstaHostItem.

    The following method handles an event that is raised when the add-in developer changes the name of a drawing host item in the ShapeApp sample application. This event handler sets the display name of the host item to the updated name of the drawing.

    void drawing_NameChanged(object sender, NameChangedEventArgs e)
     {
        Drawing drawing = (Drawing)sender;
        string cookie = drawing.Cookie;
        if (string.IsNullOrEmpty(cookie))
        {
            throw new InvalidOperationException("Cannot find matching drawing cookie");
        }
        IVstaProjectHostItem projectHostItem = this.hostAdapter.ProjectHostItems[cookie];
        if (projectHostItem == null)
        {
            throw new InvalidOperationException("Cannot find matching drawing");
        }
        projectHostItem.ProgrammingModelHostItem.DisplayName = drawing.Name;
    }
    

See Also

Tasks

How to: Add and Remove Host Objects in an Add-in Project

Walkthrough: Adding Host Items and Host Objects to an Add-in Project

Concepts

Dynamically Creating and Modifying Host Items and Host Objects in an Add-in Project

Creating In-Process Hosts

Defining Entry Points and Other Proxy Changes