Visual C # Creating and using ActiveX components

Source: Internet
Author: User
Tags define interface modify numeric value reference return string visual studio
Active|activex|visual| created and developed based on. Net platform, it is difficult to link Visual C # and ActiveX components in nature, although in the case of developing applications using Visual C #, ActiveX is sometimes required for rapid development or because of the incomplete integrity of the. Net FrameWork SDK. But even so, it's hard to relate the two. The reason for this is that the executable program that can be used directly by Visual C # and generated by Visual C # can only be managed files. Active X components are unmanaged files. The difference between these documents determines the nature of the two "opposites". So this leads to the first question in this article, what is the relationship between ActiveX and Visual C #.
 
   one. Visual C # and Active X components

Some friends might say that since being able to be used directly by Visual C # can only be managed code files, what's the point in Visual C # that you can invoke ActiveX directly by reference? It is true that Visual C # provides operations that reference ActiveX components that effectively utilize many of the previous resources, making these resources not available with Microsoft. NET platform and because of the platform differences are "abandoned", but this introduction of ActiveX components in Visual C # is not actually advocated by Microsoft, and is not in line with Microsoft's launch. NET's ultimate goal. This is because of Microsoft's introduction. NET is to achieve cross-platform, in order to achieve "write Once and run Anywhere", write the code, can be run on any platform for the purpose. If the active X component is used in the program, it also determines that the program can only be used on the Windows platform and will not be able to achieve Microsoft's "Write Once and Run Anywhere" ultimate goal.

Furthermore, Visual C # provides an operation that references an ActiveX component, while the Active X component is added to Visual C # 's Toolbox when Visual Stuio. NET actually does a lot of work on ActiveX components, and these operations are hidden by Visual C #, which is often not fully understood by users. The role of these operations is to convert unmanaged ActiveX components into managed components, these actions are collectively referred to as "interop," and careful programmers may find that when you drag an ActiveX component into a program form, several "Dll" files are added to the "Bin" directory in the directory where the source program is located. These files are generated after an interop transformation of the Active X component. Instead of using ActiveX components in Visual C #, ActiveX components interoperate to get the same class libraries that are available to the. NET platform and that have the same functionality as the original ActiveX component.

Since ActiveX components cannot be used directly in Visual C #, the ActiveX component that appears to be used in Visual C # actually uses a class library that has been converted after interoperation. can Visual C # generate Active X components? This article explores the implementation methods for generating ActiveX components in Visual C #. The way to do this is to first create a Windows component from Visual C #, and then publish its interface in COM form.

   two. The program design and operating environment introduced in this paper

(1). Microsoft Windows 2000 Server Edition.
 
(2). Visual Studio. NET 2003 Enterprise Architecture,. NET Framework SDK 4322.

   three. To create a Windows component using Visual C #

The following steps are implemented to create a Windows component using Visual C #:

1. Start visual Studio. Net.

2. When you select the menu "file" | "new" | "Project", The New Project dialog box pops up.

3. Set the project type to Visual C # project.

4. Set the template to Class library.

5. Enter "Activexdotnet" in the Name text box.

6. Enter "C:\Class" in the text box in the location, and then click OK, and Visual C # creates the Activexdotnet folder in the C:\Class directory, where the Activexdotnet project file is stored. As shown in Figure 01:


Figure 01: Create the New Project dialog box for the class library
7. Select the Solution Explorer window and upload the Class1.cs file from it, because this file is no longer used in this program.

8. When you select Project | Add component, the Add New Item dialog box pops up, setting the template to component class, setting the name value to MyControl.cs, and clicking the Open button. A file with the name "MyControl.cs" is added to the project file. As shown in Figure 02:


Figure 02: The Add New Item dialog box 9 in the project. Take visual Studio. NET switch to the MyControl.cs window, and from the Windows Forms Components tab in the Toolbox, drag the following components into the design form in order:

A GroupBox component, and then drag it into the component,

A TextBox component and a lable component.

10. Take visual Studio. NET switches to the MyControl.cs Code editing window and replaces the InitializeComponent procedure in MyControl.cs with the following code, which initializes the added component:

private void InitializeComponent ()
{
This.groupbox1 = new System.Windows.Forms.GroupBox ();
This.txtusertext = new System.Windows.Forms.TextBox ();
This.label1 = new System.Windows.Forms.Label ();
This.groupBox1.SuspendLayout ();
This. SuspendLayout ();
THIS.GROUPBOX1.CONTROLS.ADD (This.txtusertext);
THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL1);
This.groupBox1.Location = new System.Drawing.Point (8, 8);
This.groupBox1.Name = "GroupBox1";
This.groupBox1.Size = new System.Drawing.Size (272, 56);
This.groupBox1.TabIndex = 0;
This.groupBox1.TabStop = false;
This.groupBox1.Text = "Visual Studio." NET created Active X component ";
this.txtUserText.Enabled = false;
This.txtUserText.Location = new System.Drawing.Point (84, 20);
This.txtUserText.Name = "Txtusertext";
This.txtUserText.Size = new System.Drawing.Size (180, 21);
This.txtUserText.TabIndex = 1;
This.txtUserText.Text = "";
This.label1.Location = new System.Drawing.Point (8, 24);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (66, 16);
This.label1.TabIndex = 0;
This.label1.Text = "Input information:";
This. Controls.Add (This.groupbox1);
This. Name = "MyControl";
This. Size = new System.Drawing.Size (288, 72);
This.groupBox1.ResumeLayout (FALSE);
This. ResumeLayout (FALSE);
}


The interface for the Active X component created by this "activexdotnet" project is basically complete, as shown in Figure 03:


Figure 03: Design interface for the Active X component created by the "Activexdotnet" project
11. Add the following code in the "MyControl Summary description" code area of MyControl.cs, which defines a common interface that tells Com/com+ that there is a common property that can read and write:

public interface Axmycontrol
{
String Usertext {set; get;}
}


12. Add the following code in the "MyControl" Class code area of MyControl.cs, which is the first to define a private string that is used to hold the value passed from the Web test page to define a common property, and in the following Web test page, This property is used to pass the numeric value, which is read-write:

Private String Mstr_usertext;
Public String Usertext
{
get {return mstr_usertext;}
Set
{
Mstr_usertext = value;
Modify the value of a component
Txtusertext.text = value;
}
}


13. Save the above steps so that we have created a class called MyControl using Visual C #, which is a component that resembles the Active X component encapsulated in Visual C #.

14. By clicking the shortcut "Ctrl+f5", Visual C # automatically completes the compilation and generates a file called "ActiveXDotNet.dll" in the "C:\Class\ActiveXDotNet\bin\Debug" directory, which is the resulting component.

The following is the full code of the MyControl.cs generated by the above steps:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;
Namespace Activexdotnet
{
public interface Axmycontrol
{
String Usertext {set; get;}
}
<summary>
Summary description of the mycontrol.
</summary>
public class MyControl:System.Windows.Forms.UserControl, Axmycontrol
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Private System.Windows.Forms.GroupBox GroupBox1;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.TextBox Txtusertext;
Private String Mstr_usertext;
Public String Usertext
{
get {return mstr_usertext;}
Set
{
Mstr_usertext = value;
Modify the value of a component
Txtusertext.text = value;
}
}
Public MyControl ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
This.groupbox1 = new System.Windows.Forms.GroupBox ();
This.txtusertext = new System.Windows.Forms.TextBox ();
This.label1 = new System.Windows.Forms.Label ();
This.groupBox1.SuspendLayout ();
This. SuspendLayout ();
THIS.GROUPBOX1.CONTROLS.ADD (This.txtusertext);
THIS.GROUPBOX1.CONTROLS.ADD (THIS.LABEL1);
This.groupBox1.Location = new System.Drawing.Point (8, 8);
This.groupBox1.Name = "GroupBox1";
This.groupBox1.Size = new System.Drawing.Size (272, 56);
This.groupBox1.TabIndex = 0;
This.groupBox1.TabStop = false;
This.groupBox1.Text = "Active X component created by Visual C #";
this.txtUserText.Enabled = false;
This.txtUserText.Location = new System.Drawing.Point (84, 20);
This.txtUserText.Name = "Txtusertext";
This.txtUserText.Size = new System.Drawing.Size (180, 21);
This.txtUserText.TabIndex = 1;
This.txtUserText.Text = "";
This.label1.Location = new System.Drawing.Point (8, 24);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (66, 16);
This.label1.TabIndex = 0;
This.label1.Text = "Input information:";
This. Controls.Add (This.groupbox1);
This. Name = "MyControl";
This. Size = new System.Drawing.Size (288, 72);
This.groupBox1.ResumeLayout (FALSE);
This. ResumeLayout (FALSE);
}
#endregion
}
}

   Four. Use the Active X component just encapsulated in Visual C #:

The following steps are to test the creation of the component above through a Web page:

1. To create a name called Test.htm file, MyControl is placed in this web page to test, the contents of this file are as follows:
















2. The resulting "test.htm" and "ActiveXDotNet.dll" files are all copied to the virtual directory of the machine, in general, the virtual directory is "C:\Inetpub\wwwroot".

3. Open the browser, enter "Http://localhost/test.htm" in the browser's address bar, click the "Go" button, you will get the following running interface:


Figure 04: Testing the running interface of the active X component generated with Visual C #
The active X component produced by Visual C # and all the work to test this component are complete.

   Five. Summary:

Although the approach presented in this article can easily solve many of the thorny problems in Web pages, this article describes the practical, very similar active X components of components produced with Visual C #, but essentially the components produced in this article are not really active X components. To use the components created in this article, you must install the. NET Framework on the machine where the Web page is located, and when the client accesses the Web page, it does not actually download the components described in this article, and you can access Web pages that use this component without setting the security level of the computer. It is obvious that the component produced in this article is also a managed code file. It's just a clever way of defining an interface to tell the Com/com+ object that this component has an accessible public property that completes the work of a similar Active X component by reading and writing to this property.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.