Tutorials to .com

Tutorials to .com » Dotnet » Control-and-components » Visual C # to create and use ActiveX components

Visual C # to create and use ActiveX components

Print View , by: iSee ,Total views: 19 ,Word Count: 2685 ,Date: Thu, 21 May 2009 Time: 9:07 PM

development based on. Net platform, it is very difficult from the programmer is essentially the Visual c # and ActiveX components linked together, although in the use of Visual C++ # development and application of procedures, sometimes in order to or as a result of rapid development. Net framework SDK incomplete, but also require ActiveX. But even so, it is difficult to link the two. One reason for this is that Visual C++ # can be used directly through the files and Visual C # to generate only the executable program files may be hosted. Active X components and non-hosted files none. This paper determines the differences between the nature of "opposition." This then leads to the first question in this paper, ActiveX and Visual C # what kind of relationship in the end.

1. Visual C # and Active X components:

At this point there may be some friends say that since it can be directly used Visual C # managed code can only be a document, that in Visual C # can be provided directly by reference call how ActiveX is going on? Indeed Visual C # provides a reference of the operation of ActiveX components, the operation of efficient use of a lot of previous resources, so that these resources and not with Microsoft. Net platform and the differences were due to platform, "abandoned", but this in Visual C # in the operation of the introduction of ActiveX component is not being advocated by Microsoft and does not comply with Microsoft's. Net's ultimate goal. This is because the reason for the introduction of Microsoft. Net is to achieve cross-platform, in order to achieve the "Write Once and Run Anywhere", to rewrite the code, you can run on any platform purposes. If the procedure is used in Active X components, which will be decided on the other hand, this procedure can only be used on Windows platform, it is impossible to achieve Microsoft's "Write Once and Run Anywhere" the ultimate objective.

Furthermore Visual C # invoke ActiveX components provided by the operator, in fact, Active X components were added to Visual C #'s "toolbox" when, Visual Stuio. Net a matter of fact, a lot of ActiveX components to operate, and these operators also have been Visual C # hidden, the user is often not entirely clear. The role of these operations is to unmanaged hosting ActiveX components into the components, these operations collectively, the "interoperability", programmers carefully may be found that when the procedure to form into ActiveX components, the source procedures where the directory "Bin" directory will add a number of "Dll" documents, which is the Active X components for interoperability generated after the conversion. At this point in the Visual C # does not use ActiveX components, but by the ActiveX components to be available for interoperability. Net platform to use, features, and the original ActiveX component class library of the same.

Since Visual C # can not direct the use of ActiveX components, which appears in the Visual C # in a matter of fact the use of the ActiveX component is used in the interoperability of the library after the conversion. Well, Visual C # is able to generate Active X components? In this paper, to explore the generated Visual C # Implementation of ActiveX components. First of all, the method of production is through the Visual C # to create a Windows component, and then to its COM interface can form.

. In this paper, the procedures described in the design and runtime environment:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. Net 2003 version of the Enterprise structure,. Net Framework SDK 4322.

. The use of Visual C # to create Windows components:

The following is the use of Visual C # to create a Windows component to achieve these steps:

1. Start Visual Studio. Net.

2. Select Menu】 【paper | 【New】 | 【Project】, the pop-up】 【New project dialog box.

3. Types of items will 【】 【set】 Visual C # project.

4. Will template】 【Class】 【set.

5. In the Name text box】 【ActiveXDotNet.

6. 【Location】 in the text box 【C: \ Class】, and then click OK button, Visual C # in the "C: \ Class" to create directory "ActiveXDotNet" folder, which is stored in the project ActiveXDotNet document, the specific figure 01 below:


Figure 01: Creating a new class library project】 【dialog box

7. Choice of solutions 【】 Explorer window and from Class1.cs upload files because the file has been in the process does not use the.

8. Select 【Item】 | 【】 to add components, the pop-up】 【Add New Item dialog box, set in this dialog box】 【template for the "component type", set the value of Name "MyControl.cs ", click 【Open】 button. Project documents in the name of a new "MyControl.cs" document. Specific in Figure 02 below:


Figure 02: in the project】 【Add New Item dialog box
9. The Visual Studio. Net current window to switch to 【MyControl.cs (design)】 window, and Toolbox】 【The】 【Windows Forms tab of components designed to form into the following groups in order items:

A GroupBox component and then dragged into this component,

A TextBox component and a component Lable.

10. To Visual Studio. Net current window MyControl.cs】 【switch to the code editor window and replace with the following code in the InitializeComponent process MyControl.cs, the following code is initialized by adding the above components:

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 to create 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 = "Enter the message:";
this.Controls.Add (this.groupBox1);
this.Name = "MyControl";
this.Size = new System.Drawing.Size (288, 72);
this.groupBox1.ResumeLayout (false);
this.ResumeLayout (false);
)

This project created】 【ActiveXDotNet the Active X interface components on the basic completion of the specific figure 03 below:


Figure 03: 【】 ActiveXDotNet the Active X project to create the design of interface components

11. MyControl.cs in the Explanatory Memorandum MyControl 【】 to add the following code area code, the following code is the definition of a common interface, this interface is to tell COM / COM +, here are the attributes of a public can read and write operations:

public interface AxMyControl
(
String UserText (set; get;)
)

12. MyControl.cs the class】 【MyControl add the following code area code, the following code is the definition of a private first string, using string to save this test from the Web page transmission to a common definition of numerical attributes, in Web page the next test will pass through this attribute value, this attribute is read-write:

private String mStr_UserText;
public String UserText
(
get (return mStr_UserText;)
set
(
mStr_UserText = value;
/ / Modify the value components txtUserText.Text = value;
)
)

13. Save the changes above steps, now we will use Visual C # has created a name for MyControl the class, which is packaged with Visual C # and similar components of Active X components.

14. Click the shortcut keys Ctrl + F5】 【, then Visual C # compiler will auto-complete, and "C: \ Class \ ActiveXDotNet \ bin \ Debug" directory name to generate a "ActiveXDotNet.dll" document, which is generated by component .

The following is a result of these steps all MyControl.cs generated code:

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>
/ / / MyControl summary.
/ / / </ Summary>
public class MyControl: System.Windows.Forms.UserControl, AxMyControl
(
/ / / <summary>
/ / / 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 components txtUserText.Text = value;
)
)
public MyControl ()
(
/ / The call is the Windows.Forms Form Designer necessary.
InitializeComponent ();

/ / TODO: Add after the InitializeComponent call any initialization)
/ / / <summary>
/ / / Clean up all the resources being used.
/ / / </ Summary>
protected override void Dispose (bool disposing)
(
if (disposing)
(
if (components! = null)
(
components.Dispose ();
)
)
base.Dispose (disposing);
)
# region Component Designer generated code / / / <summary>
/ / / Designer to support the required method - Do not use a 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 = "Visual C # to create 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 = "Enter the message:";
this.Controls.Add (this.groupBox1);
this.Name = "MyControl";
this.Size = new System.Drawing.Size (288, 72);
this.groupBox1.ResumeLayout (false);
this.ResumeLayout (false);
)
# endregion
)
)

. Visual C # just use the Active X component package:

The following step of the way through the Web page created to test the above components:

1. Test.htm to create a file name, MyControl on this Web page is to be tested, this document reads as follows:

<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 = ":"> <input type = button value = "" onClick = "doScript (); ">
</ Form>
<hr>
</ Body>

<script Language = "javascript">
function doScript ()
(
MyControl1.UserText = frm.txt.value;
)
</ Script>
</ Html>

2. To have the "test.htm" and "ActiveXDotNet.dll" copy all files to the virtual machine directory, virtual directory is generally "C: \ Inetpub \ wwwroot".

3. Open your browser in your browser's address bar enter "http://localhost/test.htm", click "go" button, it will be running the following interface:


Figure 04: Testing with Visual C # generated by running Active X components interface

This Visual C # generated by Active X components and test the components of all of the work is complete.

. Summary:

Although the method described in this article is indeed a convenient solution to Web pages, many difficult issues, this paper introduces the use of Visual C # generated in the practical component is very similar to the Active X components, but essentially, this article generated components is not the true sense of the Active X component. To use components created in this article, Web page must be installed on the host machine. Net Framework, visit the Web page the client will not download this paper, the real components, which do not need to set the computer's security level will be able to use this visit to the Web page components. In this paper, the components can be seen arising from the essence of the code is a document hosting. It is only the definition of interface with clever ways to tell the COM / COM + objects, the components have a common attribute for the visit, through the read and write operation on this attribute, similar to Active X components to complete the work.


.Net Control and components Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.