Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » VB.NET » Writing an ActiveX Control in VB.NET

Writing an ActiveX Control in VB.NET


Software developers have used ActiveX controls on their web pages to add advanced functionality to the web experience. With my migration from a Visual Basic 6 world to a Microsoft .NET C# world, I had some question as to how I can create an ActiveX control with .NET. After some research I found out that the solution is really quite simple. Create a Windows control project in Visual Studio .NET and expose an interface to the COM world.

Total page views :  103789
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

Software developers have used ActiveX controls on their web pages to add advanced functionality to the web experience. With my migration from a Visual Basic 6 world to a Microsoft .NET C# world, I had some question as to how I can create an ActiveX control with .NET. After some research I found out that the solution is really quite simple. Create a Windows control project in Visual Studio .NET and expose an interface to the COM world.

In this example, I will walk you through creating an ActiveX control that will show a simple user interface and accept input from a web page. This process will involve the following steps:

  1. Create an assembly (class library project) that contains an item of type User Control.

  2. Expose an interface for the control.

  3. Embed the user control into a web page.

  4. Transfer data from a web form to the control and display the data on the control.

Step 1: Create an assembly.

You can use the example provided for download, or simply create your own project from scratch. In this section I will outline everything you need to do in order to properly create your assembly.

Once the project is created, delete the Class1.cs file from your project as it will not be necessary. Next, add a User Control to the project by right clicking on the project in your solution explorer, choose Add, then User Control. Name your user control "myControl".

On the user control, add some UI elements, and a text box control named txtUserText. The txtUserText control will display the user data that is typed into the web form. This will demonstrate how to pass data to your User Control.

When you are done adding your user interface to the control we now have to add a key element to the control, an Interface. The interface will allow COM/COM+ objects to know what properties they can use. In this case, we are going to expose one public property named UserText. That property will allow us to set the value of the text box control.

Step 2: Expose the Interface for the control.

First, create a private String to hold the data passed from the web form to the control:

private Dim mStr_UserText as String

Place this String just inside the Class myControl.

Next, we will create a public property. The web page will use this property to pass text back to your control. This property will allow reading and writing of the value mStr_UserText.

Public Property UserText() As [String]
Get
Return mStr_UserText
End Get
Set(ByVal Value As [String])
mStr_UserText = value
'Update the text box control value also.
txtUserText.Text = value
End Set
End
Property

In this example, you will note the extra code in the set section of the public property. When a value is passed from the web form to the control we will set the private String value equal to the value passed to the property. In addition, we are simply going to modify the value of the Text Box control directly. Typically you would NOT do this. Instead, you would raise an event and then validate the data being passed by examining the private variable mStr_UserText. Then you would set the value of the Text Box Control. However, that would add significant code to this example and for simplicity sake I am omitting that security precaution.

Now that you have a public property that .NET assemblies can use, you need to make that property available to the COM world. We do this by creating an Interface and making the myControl class inherit the interface. This will allow COM objects to see what properties we have made available.

Your code will now look like this:

Namespace ActiveXDotNet
{
Public Interface AxMyControl
Property UserText() As String
End
Property
End Interface 'AxMyControl

Public Class myControl
Inherits System.Windows.Forms.UserControl, AxMyControl
Private mStr_UserText As [String]

Public Property UserText() As String
Get
Return mStr_UserText
End Get
Set(ByVal Value As String)
mStr_UserText = value
'Update the text box control value also.
txtUserText.Text = value
End Set
End Property
End Class 'myControl
...

Notice that we now have an interface defined, the interface tells COM/COM+ that there is a public property available for use that is of type String and is readable (get) and writeable (set). All we do now is have the Class myControl inherit the interface and viola! We have a .NET assembly that acts like an ActiveX Control.

Step 3: Embed the user control in a web page.

The last thing we do now is use the control in an example web page.

<html>
<
body color=white>
<
hr>
<font face=arial size=1>
<
OBJECT id="myControl1" name="myControl1" classid="ActiveXDotNet.dll#ActiveXDotNet.myControl" width=288 height=72>
</OBJECT>
</
font>
<form name="frm" id="frm">
<
input type="text" name="txt" value="enter text here"><input type=button value="Click me" onClick="doScript();">
</
form>
<
hr>
</
body>
<script language="javascript">
function
doScript()
{
myControl1.UserText = frm.txt.value;
}
</script>
</
html>

You will notice in the HTML code above, that you call your .NET assembly very similar to an ActiveX control; however there is no GUID, and no .OCX file. Your CLASSID is now the path to your DLL and the Namespace.Classname identifier. Refer to the code above to understand the syntax of the CLASSID object tag property. Place the HTML file and your DLL in the same directory on your web server and navigate to the HTML document. (Do not load the HTML document by double clicking on it, navigate to it in your browser by using the Fully Qualified URL.) *NOTE: You might need to add your web server to your Trusted Sites list in your Internet Explorer browser.

Step 4: Transfer data from the web form to the user control.

When you load the HTML page, your control should load into the page and you will see a web form with a text box and a button. In this example, if you type some text into the text box and click the button, it will use JavaScript to send the text from the web page form, to the User Control that you just built. Your User Control will then display the text in the Text Box control that I on the form.

Where do I go from here?

There are many issues that you should investigate in order to properly create User Controls that work on a web page. .NET Security plays a big part in what you can actually do within the confines of your code. You should also investigate code signing your control.


Login to add your contents and source code to this article
 About the author
 
David Sandor
David Sandor is an Enterprise Software Architect currently working at a Fortune 100 company. He has been certified by serveral sources in the following areas: MCSE, MCSD, Linux, C#, JAVA, Visual Basic 6. David has 10 years of professional development experience.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
ASP.Net 4 Hosting is here
Become a Sponsor
 Comments
Example by Greg On March 27, 2007

I've made my UC (vb.net) and it's embeded in the webpage.
But I'm having issues with a DLL that that UC uses. 

For some reason, it's not recognising the references I have. (it is on design time, but not run time)
any suggestions?

Reply | Email | Delete | Modify | 
Re: Example by S On May 14, 2007
Can you please provide me the vb.net code of this sample for download. Only c# version is available for download, but if I convert that to vb.net code, i get few build errors (even i tried using c# to vb.net converter tools). Thanks.
Reply | Email | Delete | Modify | 
Re: Re: Example by sri On June 7, 2007

Hi

Please could you attach the code for "Writing an ActiveX Control in VB.NET"?

Iam unable to get the output

what should be typed in usercontrol and what should be typed in a class. Please tell me

 

thanks

Reply | Email | Delete | Modify | 
Re: Re: Re: Example by Deena On June 19, 2007

Hi

Where do you download the code for this example?

Thanks

Reply | Email | Delete | Modify | 
Re: Example by Marcus On August 10, 2007

Hi,

I'm having a problem with this. Followed the example, enabled COM Interop etc in the application build, created the interface etc.

But, when I add the object tag into a webpage, the ActiveX control just shows up as an edit box. The control has "full trust" privelages on my development machine, so it's not the CAS security I'm sure that's causing this.

Any ideas of how to get round this?

Many thanks.

Reply | Email | Delete | Modify | 
Debug & Testing by Dennis On July 3, 2007
I noticed that if I alter my ActiveX Control it didnt show properly in IE. I had to run "gacutil.exe /cdl" to clear the donwload cache and then it works. But when I run my ActiveX Control in IE I can't debug it. I'm using webservice inside my ActiveX and it does not run correctly inside the TestContainer in Visual Studio. Any tips about this?
Reply | Email | Delete | Modify | 
Can this be modifed for use as an embedable OLE object? by Scott On September 5, 2007
Good Day, I am looking for a way to build an embedable OLE object (with properties events etc) which can be used in Microsoft Office documents (such as Excel). Could you please point me in the right direction. Thank you,
Reply | Email | Delete | Modify | 
Can I do this on IIS without .Net Framework? by Andy On October 16, 2007

Dear Sir,
After following your guides, I made a simple "ActiveX control" with VB.Net.
And it ran successfully when I put it on IIS 5.0 with .Net Framework.

Then I did two experiments, each of the ActiveX control was built by .Net 1.1 and .Net 2.0.
I put them on IIS with .Net Framework 1.1 respectively, and they could go well.
(Internet Explorer 7.0, .Net Framework 1.1 and 2.0 were all installed on remote client)
That means IIS with .Net Framework doesn't care that if the ActiveX control runs in the same .Net Framework version.
In this architecture, IIS plays a role that only delivers the DLL to the client.
The key are Internet Explorer and the .Net Framework on the client, which provide an environment for the ActiveX DLL after downloading.

However IIS is not very critical in this architecture, it still couldn't work independently.
In the beginning, I consider that IIS could work without .Net Framework.
But, if I put the same ActiveX DLL on another IIS which is without .Net Framework, it would fail completely.
Even the client with .Net Framework could download the DLL from the IIS directly; the ActiveX control couldn't be launched.

I just want to ask...
How can I deploy ".Net ActiveX control" on IIS without .Net Framework?
Could it be possible?
Thanks for your patient!

Reply | Email | Delete | Modify | 
Looking for download by Dave On December 17, 2007
Dear Mr. Sandor, Can you tell if there is a downloadable example to go along with your article? Thanks much.
Reply | Email | Delete | Modify | 
ActiveX by uday On February 16, 2008
Dear Mr. Sandor, In VB.net, its showing the error on following line. Inherits System.Windows.Forms.UserControl, AxMyControl Can you tell if there is a downloadable example to go along with your article? Thanks
Reply | Email | Delete | Modify | 
download example by inzare On July 14, 2009
I can't find where to download it...
Reply | Email | Delete | Modify | 
Useless by Craig On April 28, 2010
You obviously just copied this example from your C# example and have no knowledge of VB.NET!!!! This example is completely useless!!! Especially to a novice VB.NET developer! Maybe you should keep your example code to yourself!!! Idiot!
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.