概述C#調用Active組件
在編碼中不可否認調用Active會給編程帶來很大便利,雖然微軟不太只持C#調用Active組件。本實例是一個簡單Web 頁面調用Active組件的例子,實現(xiàn)把數(shù)據(jù)傳入并顯示在active組件里的控件中。
實例代碼如下:
C#調用Active組件
本程序中:建立類庫添加組件類后托放:label 、 button 、groupBox各一個,需要注意的是在生成.dll之前一定要在類庫的AssemblyInfo.cs中加入引用using System.Security;和屬性[assembly : AllowPartiallyTrustedCallers()],這是為了給控件賦予足夠的權限使它能在頁面上顯示。
- usingSystem;
- usingSystem.ComponentModel;
- usingSystem.Collections;
- usingSystem.Diagnostics;
- usingSystem.Windows.Forms;
- namespaceActiveXDotNet
- {
- /**////<summary>
- ///AbeenControl的摘要說明。
- ///</summary>
- publicclassAbeenControl:System.Windows.Forms.UserControl
- {
- privateSystem.Windows.Forms.Labellabel1;
- privateSystem.Windows.Forms.Buttonbutton1;
- privateSystem.Windows.Forms.GroupBoxgroupBox1;
- /**////<summary>
- ///必需的設計器變量。
- ///</summary>
- privateSystem.ComponentModel.Containercomponents=null;
- publicstringLabelText
- {
- get
- {
- returnthis.label1.Text.ToString();
- }
- set
- {
- this.label1.Text=value;
- }
- }
- publicAbeenControl(System.ComponentModel.IContainercontainer)
- {
- /**////
- ///Windows.Forms類撰寫設計器支持所必需的
- ///
- this.label1.Text="test";
- container.Add(this);
- InitializeComponent();
- //
- //TODO:在InitializeComponent調用后添加任何構造函數(shù)代碼
- //
- }
- publicAbeenControl()
- {
- /**////
- ///Windows.Forms類撰寫設計器支持所必需的
- ///
- InitializeComponent();
- //
- //TODO:在InitializeComponent調用后添加任何構造函數(shù)代碼
- //
- }
- /**////<summary>
- ///清理所有正在使用的資源。
- ///</summary>
- protectedoverridevoidDispose(booldisposing)
- {
- if(disposing)
- {
- if(components!=null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
以上介紹C#調用Active組件。
【編輯推薦】