C # Creating and using ActiveX components

Source: Internet
Author: User

Development based on. The programmer on the Net platform is very difficult to link Visual C # and ActiveX components in nature, although it is sometimes necessary to develop applications in Visual C # for rapid development or because the. Net FrameWork SDK is incomplete, you need to use ActiveX. But even so, it is difficult to link the two. The reason for this is that executable programs that can be used directly by Visual C # and generated through Visual C # can only be managed files. The Active X component, however, is a non-managed file. 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 #.

A Visual C # and Active X components:

At this point, some friends might say that since the ability to be used directly by Visual C # can only be managed code files, what's going on in Visual C # that can call ActiveX directly from a reference? It is true that Visual C # provides operations that reference ActiveX components, which effectively take advantage of a lot of previous resources, making these resources not available with Microsoft. NET platform and because platform differences are "discarded", but this introduction of ActiveX components in Visual C # is not advocated by Microsoft, and does not conform to Microsoft's launch. NET's ultimate purpose. This is because Microsoft launched. NET is for the purpose of implementing cross-platform, in order to implement "write Once and run Anywhere", write over code that can be run on any platform. 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 implement Microsoft's "Write Once and Run Anywhere" final goal.

In addition, Visual C # provides an operation that references an ActiveX component, in fact, when the active X component is added to the Visual C # Toolbox, Visual Stuio. NET actually does a lot of work with ActiveX components, which are hidden by Visual C # and often not fully understood by users. The purpose of these operations is to convert unmanaged ActiveX components into managed components, collectively known as "Interop," and careful programmers may find that when the ActiveX component is dragged 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 the interop transformation of the Active X component. Instead of using ActiveX components in Visual C # at this time, the ActiveX components interoperate with the same class libraries that are available to the. NET platform, with the same functionality as the original ActiveX components.

Since ActiveX components cannot be used directly in Visual C #, the ActiveX components that appear to be used in Visual C # actually use a class library that has been converted after an interop operation. So is Visual C # capable of generating Active X components? This article explores how to build an ActiveX component 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 described in this article:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2003 Enterprise Architecture Edition,. NET Framework SDK 4322.

Three To create a Windows component by using Visual C #:

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

1. Start visual Studio. Net.

2. After 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 "Template" to "Class library".

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

6. Enter "C:\Class" in the text box in the location and click the OK button, then Visual C # creates a "activexdotnet" folder in the "C:\Class" directory that contains the Activexdotnet project file, as shown in 01:


Figure 01: Creating 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 useful in this program.

8. When you choose Project | Add component, the Add New Item dialog box pops up, where the template is set to component class and the name value is MyControl.cs, click the Open button. A file with the name "MyControl.cs" is added to the project file. Specific 02 shows:


Figure 02: The Add New Item dialog box in the project

9. Put visual Studio. NET switches to the MyControl.cs window and, from the Windows Forms Components tab in the Toolbox, drags the following components sequentially into the design form:

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

A TextBox component and a lable component.

10. Put visual Studio. NET switch to the "MyControl.cs" Code editing window and replace the InitializeComponent procedure in MyControl.cs with the following code, which initializes the components added above:

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 by the 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 to the active X component created by this "activexdotnet" project is basically complete, as shown in 03:


Figure 03: Design Interface for Active X components created by the "Activexdotnet" project

11. Add the following code in the "Summary description of MyControl" code area in MyControl.cs, which defines a common interface that tells Com/com+ that there is a common property available to read and write:

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

12. Add the following code to the "MyControl" Class code area in MyControl.cs, where the following code first defines a private string that is used to hold the numeric values passed from the Web test page to define a common property, in the next Web test page, The value will be passed through this property, which is read-write:

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

13. To save the above modification steps, we have created a class named MyControl with Visual C #, which is a component that resembles the Active X component in Visual C #.

14. Click the shortcut key "Ctrl+f5", the Visual C # will automatically complete the compilation, and in the "C:\Class\ActiveXDotNet\bin\Debug" directory to generate a name called "ActiveXDotNet.dll" file, which is the resulting component.

The following is the entire code for 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>
A summary description of the mycontrol.
</summary>
public class MyControl:System.Windows.Forms.UserControl, Axmycontrol
{
<summary>
The required designer variables.
</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;
Modifying the value of a component
Txtusertext.text = value;
}
}
Public MyControl ()
{
This call is required by the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call
}
<summary>
Clean up all the resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated by the #region Component Designer
<summary>
Designer supports required methods-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 Using the just-encapsulated Active X component in Visual C #:

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

1. Create a name called the test.htm file, mycontrol is put on this web page to be tested, the content of this file is as follows:










2. Copy the resulting "test.htm" and "ActiveXDotNet.dll" files 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 address bar of the browser, click the "Go" button, you will get the following running interface:


Figure 04: Testing the runtime interface of the active X component generated with Visual C #

The entire work of the active X component and testing of Visual C # is now complete.

Five Summarize:

Although the approach presented here does provide a convenient way to solve many of the thorny issues in Web pages, this article describes the usefulness of components produced in Visual C # that are really similar to Active X components, 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 same machine as the Web page, and when a client accesses a Web page, it does not actually download the components described in this article, nor does it need to set the computer's security level to be able to access Web pages that use this component. It can be seen that the component produced in this article is also a managed code file. It is just a clever way to define an interface to tell the Com/com+ object, this component has a common property to access, through the read and write operation of this property, the work similar to the Active X component.

C # Creating and using ActiveX components

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.