James Heffer
James Heffer

Reputation: 742

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();
userCont.Show();

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#.

EDIT:

Some explanation of my project:

I have an Excel 2013 and 2016 VSTO add-in project type. I have 3 classes, namely: UserControl1.vb - as my userControl Ribbon.vb - as my ribbon class ThisAddIn.vb - as the add in class

My userControl is rather simple, it has 3 buttons, a "Yes" button, a "No" button, and an "Add Tables" button. It has 1 listBox, and 12 checkboxes...

The Ribbon is also quite simple, I have 2 buttons in the Ribbon called "Show" and "Loop"...

The ThisAddIn class contains code that will make this add in work, such as methods and general functionality...

Okay so that's my classes...

The goal of my project is:

When the "Show" button is clicked, a CustomTaskPane will appear in excel on the right, inside this customTaskPane will be my userControl... (This works) Big thanks to DrDonut for his answer

When the "Add Tables" button is clicked (in the userControl), I want excel to iterate through all the sheets, and add every listObject's name (tables name) to the listBox. (The Loop and Add Tables button are the same)

In C# I know you go:

this.ListBox1.Items.Add("Item 1");

And that will add an item to the listbox, the same principle is applied in VB, but with my ListBox sitting in my userControl which inherently sits in the CustomTaskPane, no items can seem to be added... (Adding items to the list box does not work)

And with regards to the looping of tables, or retrieving their names, this doesn't work either...

(This is what I really want to get going nicely)

Hope this edit will shed some clarity on the situation...

Some gritty information:

OS: Windows 10 Pro (x64)

RAM: 32gb

CPU: i7-6700 @3.40ghz

Microsoft Visual Studio Community 2015

Microsoft Excel 2016 (x64)

Upvotes: 0

Views: 1826

Answers (1)

DrDonut
DrDonut

Reputation: 904

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.

Upvotes: 2

Related Questions