Where should I attach solution or project events in my Visual Studio add-in? - visual-studio

Can anyone suggest the best place to add solution or project events, such as ProjectAdded, to a Visual Studio add-in?
If I do this when the add-in connects then there's no solution loaded, so how can I tell when a solution has been loaded?
For example, if I write an event to handle project items being added, where should I attach this? The event would be fired by the project, and that in turn by the solution, so I can't attach the events when the add-in connects because there is no solution when the add-in connects.
On the other hand, if I add them in the Exec() event then I need to do checks such as whether the event has been attached already, and I'm sure there must be a neater way sometime between the connection events and the Exec() event.

You probably figured this out long ago, but anyway: You can setup your events from within OnConnection like shown below, this is a snippet of an Addin's Connect class (assuming you're using c#):
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
namespace MyAddin1
{
/// <summary>The object for implementing an Add-in.</summary>
/// <seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2, IDTCommandTarget
{
private DTE2 _applicationObject;
private AddIn _addInInstance;
private SolutionEvents _solutionEvents;
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// check the value of connectMode here, depending on your scenario
if(connectMode == ...)
SetupEvents();
}
private void SetupEvents()
{
// this is important ...
_solutionEvents = _applicationObject.Events.SolutionEvents;
// wire up the events you need
_solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(_solutionEvents_Opened);
_solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(_solutionEvents_AfterClosing);
_solutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(_solutionEvents_ProjectAdded);
}
// add procedures to handle the events here, plus any other
// handling you need, ie. OnDisconnection and friends
}
The main point is, to wire up the solution and project events you need, it's not important if a solution or project is already loaded. They're not attached to any particular solution or project, they're provided by the Visual Studio object model and are embedded within the EnvDTE namespace.
It wouldn't make much sense to do anything else anyway, since you can configure an addin to load when VS starts, and in this case there will never ever be any solutions/projects loaded.
There's a few catches though:
It's important that you keep a reference to the SolutionEvents class as a member variable within your connect class, otherwise the events will never fire, (see also here).
You need to make sure you check the connectMode parameter passed into OnConnection. This gets called multiple times with different parameters, and if you do it the wrong way you may get the event wired up multiple times, which definetly will be a problem. Also, usually any Addin IDE, like Menus and stuff, is set up from within OnConnection, so you may end up with duplicate menu items if you don't do it right.
Here's a few pointers, some of the code provided is VB code, in case you're looking for that:
HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in
HOWTO: Getting Project and ProjectItem events from a Visual Studio .NET add-in.
HOWTO: Add an event handler from a Visual Studio add-in
Finally, here's a list of articles, about 70% of them cover basic and advanced topics regarding addins:
Resources about Visual Studio .NET extensibility
Find the section entitled MZ-Tools Articles Series (about add-ins) and have a look at what's covered there.

Related

How do I create a VS Extension without adding UI?

I'm writing a Visual Studio 2015 extension that adds messages to the Error List window. It needs no new UI. My code runs correctly if used in a tool window or menu item, but adding UI just for this feels like a sloppy workaround to obtain an IServiceProvider.
How can my code run and obtain an IServiceProvider without adding any UI elements?
Your Package class is an IServiceProvider; you can just call its GetService method.
If you're in a MEF class, you can instead simply import SVsServiceProvider.
For more information, see my blog.

Show my own Methods/Properties of a MS .NET class always at top in Intellisense in Visual Studio 2010

I have a UserControl and I put some public Methods/Properties in there.
I would like to have that when the user of my UserControl types something and Intellisense opens, that he immediately sees my custom methods created in the UserControl, is that possible somehow by marking the methods/props with kind of attributes?
No, this is not something that can be easily done. Visual Studio will sort the methods/properties alphabetically.
To do what you want, you will need to:
Mark your methods/properties with a custom attribute
Write this custom attribute
Use VS automation to reflect over every type to find this attribute
Reorder the intellisense listing (not sure if this is even possible with VS VBA)

How to extend the ADO.NET entity designer?

Does anyonw know how to extend, i.e., add funcionalities to the Entity Designer in Visual Studio?
For instance, I want to right click a property of an entity on the designer and have a new option on the context-menu that allows me to do any stuff I want.
The Entity Designer in VS 2008 SP1 doesn't have many extensibility hooks. What you could do is leverage the Visual Studio extensibility (VSIP, now known as VSX):
Add in your own context menu
Use IVsMonitorSelection to get the current selection, from which you can get ISelectionContainer.
If the user has selected the diagram surface, you can then cast ISelectionContainer as DiagramDocView. This is part of 'DSL', which is the framework that the Entity Designer uses for its designer surface.
From here you can do lots of things within the DiagramDocView. DiagramDocView.CurrentDiagram will give you the Diagram object. You can call Diagram.NestedChildShapes to get all shapes in the diagram. To make changes to the diagram, you will have to create a DSL transaction and perform your edits to the shapes in the transaction. This is simply another level above the Entity Designer and everything will be handled correctly:
using (Transaction tx = store.TransactionManager.BeginTransaction(txText))
{
// do something, such as creating an EntityTypeShape;
tx.Commit();
}
The Entity Designer in VS 2010 will have a lot more extensibility hooks to allow you to influence the model through the property window or through the wizard. New extensibility work in the new 'Model First' feature will basically allow you to generate anything from a model within Visual Studio in a composable way.

Visual Studio add-on gallery?

i'm hoping to find some add-ons for Visual Studio to address some specific usability issues. Is there a Visual Studio addons gallery that contains a huge dumping ground of addons that every person, company, yahoo and hick have created?
Kind of like Vista Sidebar Gadget gallery, but for addons.
Kind of like CodePlex, but for addons.
Is Visual Studio Gallery it?
Not that it's important to my question, but some of the IDE functionality i was hoping to see addressed through addons:
tabs are placed in chronological open order
renaming a control renames attached event handlers
deleting all code from an event handler deletes and unhooks the event handler
deleting an event handler unhooks it
rearrange code so private, protected and public methods and grouped. Properties and events are grouped. Private variables are grouped.
analyse for using lint
controls that have been cut/pasted have their event handlers functional
full support for B.R.I.E.F. bookmarks (found it)
/// automatically adds thrown exceptions
/// comments are rendered on the item their declared for
You may be looking for this
Have you checked the MSDN Code Gallery?

.NET VS2005 WinForms: How do i drop a user control onto a form?

i've written a UserControl descendant that is in an assembly dll.
How do i drop the control on a form?
namespace StackOverflowExample
{
public partial class MonthViewCalendar : UserControl
{
...
}
}
i've added a reference to the assembly under the References node in the Solution Explorer, but no new control has appeared in my Toolbox.
How do i make the control appear in the Toolbox so i can drop it on a form?
Update 1:
i tried building the assembly while the Visual Studio option:
Tools-->Options...-->Windows Forms Designer-->AutoToolboxPopulate = true
The control didn't appear when in the toolbox in a new solution.
Note: i somehow mistakenly wrote "...that is not in an assembly dll...". i don't know how i managed to write that, when it specifically is in an assembly dll. Controls have magically appeared when they're in the same project, but not now that it's a different project/solution.
Update 2: Answer
Right-click the Toolbox
Select Choose Items...
.NET Framework Components tab
Select Browse...
Browse to the assembly dll file that contains the control and select Open
Note: Controls in the assembly will silently be added to the list of .NET Framework Components.
Check each of the controls you wish to appear in the toolbox
Select OK
Normally, when you build your project, your user control will appear in your toolbox at the top. Normally, you will see a new pane with each of your Assemblies and the controls in there.
If that doesn't happen, you can also add your control by right clicking on the toolbox, selecting Choose Items, then under .NET Framework Components browsing for your assembly, adding it, then make sure your control is checked.
What I notice is that User Controls and Components are only automatically added to the Toolbox by vs2005 when your project (containing the controls/components) is located in the same folder as your solution. When this project is in a subfolder vs2005 won't add the controls and components in the Toolbox.
I stumbled upon some problems with this. In the end, just rebuild and re-reference will work. I had preferred to inherit from UserControl. It made my life simpler ;)
If for example you want to create a "rounded border" label, do something like this:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace YourUIControls
{
[DefaultProperty("TextString")]
[DefaultEvent("TextClick")]
public partial class RoundedLabel : UserControl
{
public RoundedLabel()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
//Draw your label hereā€¦
}
}
}
Compile and add a reference to the output. You'll be able to drag that to the Toolbox and later to the Designer.
You need to build the project containing the control you've created and make sure your options are set for the Toolbox to rebuild. If you haven't changed it from defaults, it should work; otherwise, go to Tools-->Options... and select the Windows Forms Designer group. Make sure the AutoToolboxPopulate option is set to true.
You don't need the ToolboxItemAttribute for it to work. If the providing assembly is in the same solution as the consuming assembly, it should appear in the toolbox. If the providing assembly is not part of the solution, you can manually add the assembly to the toolbox by selecting **Choose items...* from the toolbox context menu and adding your assembly directly. If you want the toolbox to automatically pick them up, you will need to use the ToolboxItemAttribute.
Add the ToolboxAttribute to your class.

Resources