Visual C # create and use ActiveX Components

Source: Internet
Author: User
Developed on the. NET platform Program Essentially, it is difficult to associate Visual C # With ActiveX components. Although Visual C # is used to develop applications. net Framework SDK is incomplete, and ActiveX is also required. However, even so, it is difficult to associate the two. The reason is that the files can be directly used by Visual C # And the executable programs generated through Visual C # can only be hosted files. The Active X component is not a hosted file. The difference in such files determines the nature of the two ". So this raises the first question in this article, what is the relationship between ActiveX and Visual C.

I. Visual C # and Active X components :

At this time, some may say that since Visual C # Can be used directly, it can only be hosted.CodeIn Visual C #, how does one call ActiveX directly through reference? Indeed, Visual C # provides operations that reference ActiveX components. This operation effectively utilizes a lot of previous resources, making these resources not available with Microsoft. NET platform, but this operation to introduce ActiveX Components in Visual C # Is not advocated by Microsoft, nor is it in line with Microsoft's launch. net. This is because Microsoft launched. Net to achieve cross-platform, to achieve "write once and run anywhere", and to write the code again, it can run on any platform. If the Active X component is used in the program, it is determined from the other hand that the program can only be used on the Windows platform, so it cannot achieve Microsoft's "write once and run anywhere" final goal.

In addition, the reference ActiveX component operation provided by Visual C #, in fact, when the Active X component is added to the "toolbox" of Visual C #, visual stuio. net actually performs a lot of operations on ActiveX components, and these operations are hidden by Visual C #, users are often not completely clear. These operations are used to convert unmanaged ActiveX components into managed components. These operations are collectively referred to as "interoperability". Careful programmers may find that, after the ActiveX component is dragged to the program form, several "DLL" files are added to the "bin" directory where the source program is located, these files are generated after interoperability conversion by the Active X component. In this case, Visual C # does not use ActiveX components, but uses ActiveX components for interoperability to obtain class libraries that are available for the. NET platform and have the same functions as the original ActiveX components.

Since ActiveX components cannot be directly used in Visual C #, ActiveX components that appear to be used in Visual C # actually use class libraries that are converted after interoperability. Can Visual C # generate an Active X component? This article describes how to generate ActiveX Components in Visual C. The method is to first create a Windows component through Visual C #, and then publish its interface in the form of COM.

2. The program design and running environment described in this article:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2003 enterprise structure edition,. NET Framework SDK 4322.

3. Use Visual C # To create Windows Components:

Follow these steps to create a Windows component using Visual C:

1. Start Visual Studio. NET.

2. Select File, new, and project. The new project dialog box is displayed.

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

4. Set [TEMPLATE] to [class library ].

5. Enter activexdotnet In the Name text box ].

6. in the "location" text box, enter "C: \ class" and click "OK". Then, Visual C # is displayed in "C: create the "activexdotnet" folder in the \ class "Directory, which stores the activexdotnet project file, as shown in 01:


Figure 01: Create a new project dialog box

7. Select Solution Explorer and upload the class1.cs file from it because the file is useless in this program.

8. select project | add component. The add new item dialog box is displayed. In this dialog box, set template to component class and name to mycontrol. after CS, click open. Add a file named "mycontrol. cs" to the project file. 02:


Figure 02: Add new item dialog box in the project

 

9. set Visual Studio. switch to mycontrol. CS (Design)] window, and drag the following components to the design form in sequence from the Windows Forms components tab in the toolbox:

A groupbox component and drag it into it,

A textbox component and a lable component.

10. Switch the current window of Visual Studio. NET to the "mycontrol. cs" code editing window and replace initializecomponent in mycontrol. CS with the following code. The following code initializes the components added above:

Private void initializecomponent ()
{
This. groupbox1 = new system. Windows. Forms. groupbox ();
This.txt usertext = new system. Windows. Forms. Textbox ();
This. label1 = new system. Windows. Forms. Label ();
This. groupbox1.suspendlayout ();
This. suspendlayout ();
This. groupbox1.controls. Add (this.txt usertext );
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 Studio. NET ";
This.txt usertext. Enabled = false;
This.txt usertext. Location = new system. Drawing. Point (84, 20 );
This.txt usertext. Name = "txtusertext ";
This.txt usertext. size = new system. Drawing. Size (180, 21 );
This.txt usertext. tabindex = 1;
This.txt usertext. 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 );
}

Now the Active X component interface created by the activexdotnet project is basically complete, as shown in 03:


Figure 03: design interface of the Active X component created by activexdotnet

11. in mycontrol. in CS, [Summary of mycontrol] Add the following code in the code area. The following Code defines a public interface that tells COM/COM +, there is a common attribute for read/write operations:

Public interface axmycontrol
{
String usertext {set; get ;}
}

12. in mycontrol. add the following code to the [mycontrol] class code area of CS. The following Code defines a private string first, this string is used to save the value passed from the web test page and define a public attribute. In the following web test page, the value is passed through this attribute, Which is readable and writable:

Private string mstr_usertext;
Public String usertext
{
Get {return mstr_usertext ;}
Set
{
Mstr_usertext = value;
// Modify the component Value
Txtusertext. Text = value;
}
}

13. save the above modification steps. Now we have created a class named mycontrol using Visual C #, which is a component encapsulated by Visual C # and similar to the Active X component.

14. click Ctrl + F5, Visual C # automatically completes compilation, and generates a name named "activexdotnet" in the "C: \ class \ activexdotnet \ bin \ debug" directory. DLL file, which is the generated component.

The following is all the code for mycontrol. CS generated after 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 of mycontrol.
/// </Summary>
Public class mycontrol: system. Windows. Forms. usercontrol, axmycontrol
{
/// <Summary>
/// 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;
// Modify the component Value
Txtusertext. Text = value;
}
}
Public mycontrol ()
{
// This call is required by the windows. Forms Form Designer.
Initializecomponent ();

// Todo: add any initialization after initializecomponent calls
}
/// <Summary>
/// Clear all resources 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>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. groupbox1 = new system. Windows. Forms. groupbox ();
This.txt usertext = new system. Windows. Forms. Textbox ();
This. label1 = new system. Windows. Forms. Label ();
This. groupbox1.suspendlayout ();
This. suspendlayout ();
This. groupbox1.controls. Add (this.txt usertext );
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 # Created Active X component ";
This.txt usertext. Enabled = false;
This.txt usertext. Location = new system. Drawing. Point (84, 20 );
This.txt usertext. Name = "txtusertext ";
This.txt usertext. size = new system. Drawing. Size (180, 21 );
This.txt usertext. tabindex = 1;
This.txt usertext. 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
}
}

Iv. VisualC #Use the encapsulated Active X component:

Follow these steps to test the above components on the web page:

1. Create a file named test.htm. mycontrol is tested on this web page. The content of this file is 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 = "Enter data:"> <input type = button value = "OK" onclick = "doscript ();">
</Form>
<HR>
</Body>

<Script language = "JavaScript">
Function doscript ()
{
Mycontrol1.usertext = frm.txt. value;
}
</SCRIPT>
</Html>

2. Copy all the generated "test.htm" and "activexdotnet. dll" files to the virtual directory of the machine. Generally, the virtual directory is "C: \ Inetpub \ wwwroot ".

3. Open the browser, enter "http: // localhost/test.htm" in the address bar of the browser, and click "go to". The following running interface is displayed:


Figure 04: test the running interface of the Active X component generated by Visual C #

So far, all the work of the Active X component generated by Visual C # and the test of this component has been completed.

V. Summary:

Although the methods described in this article can easily solve many difficult problems on the web page, this article introduces that the components generated by Visual C # are very similar to Active X components in practicality, however, in essence, the components produced in this article are not actually Active X components. To use the components created in this article, you must install them on the machine where the web page is located.. The component generated in this article is actually a hosted code file. It tells COM/COM + objects cleverly by defining interfaces. This component has a public attribute that can be accessed through read/write operations on this attribute, similar to the Active X component.

Related Article

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.