VS.Net C# 調用 Active 組件

來源:互聯網
上載者:User
active

  在編碼中不可否認調用Active會給編程帶來很大便利,雖然微軟不太只持.net調用Active 組件。
       本執行個體是一個簡單Web 頁面調用Active組件的例子,實現把資料傳入並顯示在active組件裡的控制項中。
  執行個體代碼如下:
1、建立Active組件(這裡用.net c#建立)
        (本程式中:建立類庫添加組件類後托放:label 、 button  、groupBox各一個)
   需要注意的是在產生.dll之前一定要在類庫的AssemblyInfo.cs中加入引用using System.Security;和屬性[assembly : AllowPartiallyTrustedCallers()],這是為了給控制項賦予足夠的許可權使它能在頁面上顯示。

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms ;

namespace ActiveXDotNet
{
    /**//// <summary>
    /// AbeenControl 的摘要說明。
    /// </summary>
    public class AbeenControl : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.GroupBox groupBox1;
        /**//// <summary>
        /// 必需的設計器變數。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public string LabelText
        {
            get
            {
                return this.label1.Text.ToString();
            }
            set
            {
                this.label1.Text=value;
            }
        }

        public AbeenControl(System.ComponentModel.IContainer container)
        {
            /**////
            /// Windows.Forms 類撰寫設計器支援所必需的
            ///
            this.label1.Text="test";
            container.Add(this);
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
            //
        }

        public AbeenControl()
        {
            /**////
            /// Windows.Forms 類撰寫設計器支援所必需的
            ///
            InitializeComponent();
           

            //
            // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
            //
        }

        /**//// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }


        組件設計器產生的程式碼#region 組件設計器產生的程式碼
        /**//// <summary>
        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(16, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(296, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(120, 56);
            this.button1.Name = "button1";
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(320, 88);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "groupBox1";
            //
            // AbeenControl
            //
            this.Controls.Add(this.groupBox1);
            this.Name = "AbeenControl";
            this.Size = new System.Drawing.Size(328, 96);
            this.Load += new System.EventHandler(this.AbeenControl_Load);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void button1_Click(object sender, System.EventArgs e)
        {
            this.label1.Text="點擊完成!";
        }

        private void AbeenControl_Load(object sender, System.EventArgs e)
        {
       
        }
    }
}
2、web 頁面調用Active組件
  注意web頁面調用時需要把Active的.dll放到web的虛擬要目錄下。
<HTML>
    <HEAD>
        <title>TestActive</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="javascript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body >
        <font face="arial" size="1">
            <OBJECT id="MyControl1" name="MyControl1" classid="http://localhost/WebApplication1/ActiveXDotNet.dll"
                width="288" height="72" VIEWASTEXT>
            </OBJECT>
        </font>
        <form id="frm" name="frm">
            <input type="text" name="txt" value="請輸入資料:" ID="Text1">
            <input type="button" value="確定" ID="Button1" NAME="Button1">
            <script type="text/javascript">
            function doScript()
            {
                 MyControl1.LabelText=frm.txt.value;
            }
           
            </script>
        </form>
    </body>
</HTML>



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。