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

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.

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.

How to reference a custom class library in wp7

I have created a windows phone class library, I need to add this library as a reference to my application. How do I do this?
Same as a normal project/class library - right click on the project in solution explorer, select add reference. If the project is also in the current solution, choose it from the projects tab.

ASP.NET MVC Add View Dialog closing

Recently a large project I work on started having a problem with the Add View dialog. When clicking the Add Strongly-typed View checkbox, the spinner comes up the first time for about a second or two, and then the entire dialog just closes and disappears. If I open the dialog and click the button again, it just closes again quickly.
The project is using ASP.NET MVC 2, I have installed VS2010 SP1 and this problem occurs with or without the MVC3 tools update installed. It only happens with this project, and I have replicated the problem on 2 different development machines. If I create a new MVC2 or MVC3 project, this does not happen at all, nor do any other small to medium sized projects I have.
I can of create a regular view and change it to strongly typed by myself, so there is a workaround, but this is still pretty annoying. Any ideas what could be causing this or how to fix it?
Do you use version control? Make sure all the assemblies that your project depends on (i.e. referenced by the project itself or by its referenced assemblies) are in sync. I've just run into this (both "Add View" and "Add Controller" problems) after updating a bunch of projects from SVN and rebuilding some. The issue was fixed after I rebuilt several libraries that my MVC project depends on.
I had the same problem, and I could not open the Add->Controller dialog either as described here: Add Controller after recent tools update fails with dependency error. In my case, I had added models to my domain, but I had forgotten to add contexts for these new models; things like
public DbSet<Region> Regions { get; set; }
I had no error at compile time, and I was not using these models yes in the solution, so there was no error at run time either. I guess the problem can be anywhere though.

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

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.

Krypton Form not showing up in Visual Studio Professional 2008

I have just installed the Krypton Toolkit 3.0.6 from component Factory.
I find that in the create new Project Dialog Box , Krypton Form does not show up as an option. I am sure it used to show up ( and I have actually used it in an earlier version of krypton toolkit).But after the new install it does not.
For the sake of completeness and accuracy , I am posting the actual code for inheriting from a krypton form.
public partial class Form1 : ComponentFactory.Krypton.Toolkit.KryptonForm
The "New Krypton Form" used to show up not inside the "new project" dialog, but inside the "new item" dialog. (e.g. right-click on project, Add New Item)
But I don't see it there either. Phil may have removed this from the installer.
In any case, just add a regular Form, then make it derive from KryptonForm rather than Form, and voila, you have yourself a KryptonForm.

Resources