繁体   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