Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
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.

Page Views : 126601
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Mindcracker MVP Summit 2012
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate 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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
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 | 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 | 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 | Modify 
Re: Re: Re: Example by Deena On June 19, 2007

Hi

Where do you download the code for this example?

Thanks

Reply | Email | 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 | 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 | 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 | 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 | 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 | 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 | Modify 
download example by inzare On July 14, 2009
I can't find where to download it...
Reply | Email | Modify 
Error with references by Saurabh On June 2, 2011
This article was a lifesaver and it was able to implement it successfully. Now that I have a reference to Bytescout.scan.dll in my project from http://bytescout.com/products/developer/scansdk/bytescoutscansdk.html. After compiling I copied ActivexDotNet.dll and Bytescout.scan.dll to server Now when I request the same page the active object is not visible in the web-page. please help.
Reply | Email | Modify 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.