James Heffer James Heffer - 2 years ago 77
Vb.net Question

How to instantiate a UserControl using VB?

So in C# to declare and instantiate a UserControl, you would go about it similar to this:

UserControl1 userCont = new UserControl1();

And this would show your user control named UserControl1.

This is the way you do it in C#, now how do you do this in VB?

Because I have coded an Excel Add In, and have put a userControl in the ribbon, but it does not want to show, and that project is in VB, not C#.

Answer Source

In vb.net it's quite similar, for a task pan (the panel at the right side of an office application) it is:

Dim userControl as UserControl
Dim userTaskPane as Microsoft.office.tools.CustomTaskPane

userControl = new UserControl
userTaskPane = Me.CustomTaskPanes.Add(userControl, "Title")
userTaskPane.visible = true

Now it should show the panel.

Edit: Source: https://msdn.microsoft.com/en-us/library/aa942846.aspx

Edit 2: You also need to set the right references. Assuming that you use visual studio, go to the solution explorer -> Your project -> References. In my project these contain the following:

  • Microsoft.Office.Interop.Excel
  • Microsoft.Office.Tools
  • Microsoft.Office.Tools.Common
  • Microsoft.Office.tools.Common.v4.0.Utilities
  • Microsoft.Office.Tools.Excel
  • Microsoft.Office.Tools.v4.0.Framework
  • Microsoft.VisualStudio.Tools.Applications.Runtime
  • Office

I don't know if you need all of them, but for sure you will need some of them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download