2013-10-28 41 views
1

我使用C#語言創建了一個activeX控件。現在我需要將UI(一些文本框和標籤)添加到我的activeX控件中。我怎樣才能做到這一點?我嘗試了下一個代碼,但我沒有成功(我從msdn.microsoft.com得到了這段代碼)。如何編寫基於winform的activeX控件以在網頁上使用?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Runtime.CompilerServices; 
using System.Data; 
using System.Linq; 

namespace AxControls 
{ 
    [ComVisible(true)] 
    [ClassInterface(ClassInterfaceType.None)] 
    [Guid("42BBA00A-515E-45b5-9EAF-3827F7AEB4FA")] 
    [ProgId("AxControls.HelloWorld")] 
    [ComDefaultInterface(typeof(IClip))] 
    public class HelloWorld : UserControl 
    { 
     // Create the controls. 
     private System.Windows.Forms.ErrorProvider errorProvider1; 
     private System.Windows.Forms.TextBox textName; 
     private System.Windows.Forms.TextBox textAddress; 
     private System.Windows.Forms.TextBox textCity; 
     private System.Windows.Forms.TextBox textStateProvince; 
     private System.Windows.Forms.TextBox textPostal; 
     private System.Windows.Forms.TextBox textCountryRegion; 
     private System.Windows.Forms.TextBox textEmail; 
     private System.Windows.Forms.Label labelName; 
     private System.Windows.Forms.Label labelAddress; 
     private System.Windows.Forms.Label labelCityStateProvincePostal; 
     private System.Windows.Forms.Label labelCountryRegion; 
     private System.Windows.Forms.Label labelEmail; 
     private System.ComponentModel.IContainer components; 

     // Define the constructor. 
     public HelloWorld() 
     { 
     InitializeComponent(); 
     } 

     // Initialize the control elements. 
     public void InitializeComponent() 
     { 
     // Initialize the controls. 
     components = new System.ComponentModel.Container(); 
     errorProvider1 = new System.Windows.Forms.ErrorProvider(); 
     textName = new System.Windows.Forms.TextBox(); 
     textAddress = new System.Windows.Forms.TextBox(); 
     textCity = new System.Windows.Forms.TextBox(); 
     textStateProvince = new System.Windows.Forms.TextBox(); 
     textPostal = new System.Windows.Forms.TextBox(); 
     textCountryRegion = new System.Windows.Forms.TextBox(); 
     textEmail = new System.Windows.Forms.TextBox(); 
     labelName = new System.Windows.Forms.Label(); 
     labelAddress = new System.Windows.Forms.Label(); 
     labelCityStateProvincePostal = new System.Windows.Forms.Label(); 
     labelCountryRegion = new System.Windows.Forms.Label(); 
     labelEmail = new System.Windows.Forms.Label(); 

     // Set the tab order, text alignment, size, and location of the controls. 
     textName.Location = new System.Drawing.Point(120, 8); 
     textName.Size = new System.Drawing.Size(232, 20); 
     textName.TabIndex = 0; 

     textAddress.Location = new System.Drawing.Point(120, 32); 
     textAddress.Size = new System.Drawing.Size(232, 20); 
     textAddress.TabIndex = 1; 

     textCity.Location = new System.Drawing.Point(120, 56); 
     textCity.Size = new System.Drawing.Size(96, 20); 
     textCity.TabIndex = 2; 

     textStateProvince.Location = new System.Drawing.Point(216, 56); 
     textStateProvince.Size = new System.Drawing.Size(56, 20); 
     textStateProvince.TabIndex = 3; 

     textPostal.Location = new System.Drawing.Point(272, 56); 
     textPostal.Size = new System.Drawing.Size(80, 20); 
     textPostal.TabIndex = 4; 

     textCountryRegion.Location = new System.Drawing.Point(120, 80); 
     textCountryRegion.Size = new System.Drawing.Size(232, 20); 
     textCountryRegion.TabIndex = 5; 

     textEmail.Location = new System.Drawing.Point(120, 104); 
     textEmail.Size = new System.Drawing.Size(232, 20); 
     textEmail.TabIndex = 6; 

     labelName.Location = new System.Drawing.Point(8, 8); 
     labelName.Size = new System.Drawing.Size(112, 23); 
     labelName.Text = "Name:"; 
     labelName.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 

     labelAddress.Location = new System.Drawing.Point(8, 32); 
     labelAddress.Size = new System.Drawing.Size(112, 23); 
     labelAddress.Text = "Address:"; 
     labelAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 

     labelCityStateProvincePostal.Location = new System.Drawing.Point(8, 56); 
     labelCityStateProvincePostal.Size = new System.Drawing.Size(112, 23); 
     labelCityStateProvincePostal.Text = "City, St/Prov. Postal:"; 
     labelCityStateProvincePostal.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 

     labelCountryRegion.Location = new System.Drawing.Point(8, 80); 
     labelCountryRegion.Size = new System.Drawing.Size(112, 23); 
     labelCountryRegion.Text = "Country/Region:"; 
     labelCountryRegion.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 

     labelEmail.Location = new System.Drawing.Point(8, 104); 
     labelEmail.Size = new System.Drawing.Size(112, 23); 
     labelEmail.Text = "email:"; 
     labelEmail.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 

     // Add the Validating and Validated handlers for textEmail. 
     textEmail.Validating += new System.ComponentModel.CancelEventHandler(textEmail_Validating); 
     textEmail.Validated += new System.EventHandler(textEmail_Validated); 

     // Add the controls to the user control. 
     Controls.AddRange(new System.Windows.Forms.Control[] 
     { 
      labelName, 
      labelAddress, 
      labelCityStateProvincePostal, 
      labelCountryRegion, 
      labelEmail, 
      textName, 
      textAddress, 
      textCity, 
      textStateProvince, 
      textPostal, 
      textCountryRegion, 
      textEmail 
     }); 

     // Size the user control. 
     Size = new System.Drawing.Size(375, 150); 
     } 


     private void MyValidatingCode() 
     { 
     // Confirm there is text in the control. 
     if (textEmail.Text.Length == 0) 
     { 
      throw new Exception("Email address is a required field."); 
     } 
     // Confirm that there is a "." and an "@" in the e-mail address. 
     else if(textEmail.Text.IndexOf(".") == -1 || textEmail.Text.IndexOf("@") == -1) 
     { 
      throw new Exception("Email address must be valid e-mail address format." + 
      "\nFor example: '[email protected]'"); 
     } 
     } 


     // Validate the data input by the user into textEmail. 
     private void textEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e) 
     { 
     try 
     { 
      MyValidatingCode(); 
     } 

     catch(Exception ex) 
     { 
      // Cancel the event and select the text to be corrected by the user. 
      e.Cancel = true; 
      textEmail.Select(0, textEmail.Text.Length); 

      // Set the ErrorProvider error with the text to display. 
      this.errorProvider1.SetError(textEmail,ex.Message); 
      } 
     } 


     private void textEmail_Validated(Object sender, System.EventArgs e) 
     { 
     //If all conditions have been met, clear the error provider of errors. 
     errorProvider1.SetError(textEmail, ""); 
     } 

    } 
} 

,這是我如何使用HTML頁面中的ActiveX控件

<html> 
<head> 
</head> 
    <body> 
     <object name='Hello' style='display: none' id='Hello' classid='CLSID:42BBA00A-515E-45b5-9EAF-3827F7AEB4FA' 
      codebase='AxControls.cab#version=1,0,0,0' width="100%" height="100%"> 
     </object> 
    </body> 
</html> 

我確實知道的ActiveX是登錄並執行好,因爲當我添加類似

MessageBox.Show("HELLO"); 

我的ActiveX控件的代碼我可以看到這個消息,同時調用我的HTML頁面。無論如何,我根本看不到UI。

任何建議將是偉大的。

回答

1

<object>標籤中使用style='display: none'是一個非常糟糕的主意:)

相關問題