【问题标题】:what is replacement of VBControlExtender in .net什么是 .net 中 VBControlExtender 的替代品
【发布时间】:2012-11-26 11:42:04
【问题描述】:

我正在将 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

【问题讨论】:

  • 在 C# 4.0 中,您使用 this, 关键字来访问 Extender 公开的当前表单的控件。
  • 什么是VBControlExtender?不,文档并没有帮助我理解它。 :)

标签: .net vb.net c#-4.0


【解决方案1】:

大多数 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);
    }
}

How to programmatically add controls to Windows forms at run time

【讨论】:

  • 我已经完成了这个公共部分类按钮:UserControl,IControl { }
【解决方案2】:

丹尼斯我已经这样做了

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 

【讨论】:

    【解决方案3】:

    在 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

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2011-08-31
      • 2014-04-12
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 2019-02-22
      相关资源
      最近更新 更多