我正在将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在许多用户控件中实现,如(按钮,Chekbox,地址等)。
我正在将此代码转换为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
答案 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
答案 1 :(得分:0)
丹尼斯我已经完成了这个
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
答案 2 :(得分:0)
在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