簡體   English   中英

什么是 .net 中 VBControlExtender 的替代品

[英]what is replacement of VBControlExtender in .net

我正在將 VB6 項目轉換為 C#.net。
VB6 代碼是。

 Dim ctlControl As VB.VBControlExtender
 Dim objControl As DocSys.IControl



If blnRetVal Then

    ' get IControl interface
    On Error Resume Next
    Set objControl = ctlControl.object
    blnRetVal = objControl.Load(strName, ndControl, objField, objTab.Model)

其中用戶控件正在動態使用。
objControl 的類型是 IControl,它是一個接口。 IControl 在許多用戶控件中實現,如(按鈕、復選框、地址等)。

我正在將此代碼轉換為 C#.net。
代碼是

Control ctlControl = new Control();
DocSys.IControl objControl = default(DocSys.IControl);
 if (blnRetVal)
  {                    
    objControl = (IControl)ctlControl;  
     blnRetVal = objControl.Load(strName, ndControl, objField, objTab.Model);
  }

它顯示了一個異常ctlControl:

Cannot cast 'ctlControl' (which has an actual type of 'System.Windows.Forms.Control') to 'DocSys.IControl'  DocSys.IControl

大多數 WinForm 控件都繼承自System.Windows.Forms.Control ,如果您希望控件實現您的 IControl-Interface,那么您必須擴展基本控件。

像:

public class MyTextBox : System.Windows.Forms.TextBox, DocSys.IControl
{
    public string Test() // Function of IControl
    {
        throw new NotImplementedException();
    }
}

要將控件動態添加到您的表單,您可以使用以下示例代碼:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        MyTextBox textBox = new MyTextBox();
        textBox.Text = "Textbox content";
        textBox.Location = new Point(25, 25);
        this.Controls.Add(textBox);
    }
}

如何在運行時以編程方式向 Windows 窗體添加控件

丹尼斯 我已經做到了這一點

public partial class CanvasCtl: UserControl,IControl
  {
    public bool Load(string strName, System.Xml.XmlElement ndControl, IField objField, Model objModel)
    {
        m_strName = strName;
        m_Field = objField;

        this.Enabled = true;

        return Init(ref ndControl);

    }
   }

ChekBox 類和 CanvasCtl 類中的相同問題是我正在動態使用它們

Control ctlControl = new Control();
DocSys.IControl objControl = default(DocSys.IControl);

 ctlControl = (Control)objTab.CanvasCtl.GetCtl(strName);
 if (blnRetVal)
  {                    
  objControl = (IControl)ctlControl;  
   blnRetVal = objControl.Load(strName, ndControl, objField, objTab.Model);
  }

在線上

 objControl = (IControl)ctlControl;

它顯示一個異常

Cannot cast 'ctlControl' (which has an actual type of 'System.Windows.Forms.Control') to 'DocSys.IControl'  DocSys.IControl 

在 VB6 中,您需要VBControlExtender對象,以便使用 Add 方法將控件動態添加到 Controls 集合中。

它主要用於 ActiveX 控件。

例如

Dim WithEvents dynamicFlexGrid As VBControlExtender

您可以執行以下操作:

Set dynamicFlexGrid = Controls.Add("MSFlexGridLib.MSFlexGrid.1", ,"FlexGrid1")
   With dynamicFlexGrid.object  
   For r = 1 To .Rows - 1  
      For c = 1 To .Cols - 1  
         .TextMatrix(r, c) = "r"  & r & "c" & c  
      Next  
   Next 
dynamicFlexGrid.Height = 300
dynamicFlexGrid.Width = 300
dynamicFlexGrid.Visible = True

在 .NET 中,為了托管 ActiveX,您需要一個 AxHost 子類。 您可以創建一個簡單的助手,例如:

namespace UpgradeHelpers
{
    public class AxControl : AxHost
    {
        public AxControl(string strCLSID) : base(strCLSID)
        {
        }
    }


    public static class ControlExtenderHelper
    {
        public static AxHost AddExtended(this System.Windows.Forms.Control.ControlCollection controls, string progId, string controlName)
        {
            Type type = Type.GetTypeFromProgID(progId, true);
             var newControl = new AxControl(controlType.GUID.ToString());
            newControl.Name = controlName;
            controls.Add(newControl);
            return newControl;
        }
    }
}

然后你可以將你的變量聲明為

   AxHost dynamicVSFlexGrid;

並像這樣使用它:

            dynamicFlexGrid = Controls.AddExtended("MSFlexGridLib.MSFlexGrid.1", "FlexGrid1");

有關更多詳細信息,請參閱我的帖子: https : //www.mobilize.net/blog/dynamically-adding-activex-in-c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM