简体   繁体   中英

what is replacement of VBControlExtender in .net

I am converting VB6 project to C#.net.
VB6 Code is.

 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)

In it user Controls are using Dynamically.
Type of objControl is IControl which is an Interface. IControl is Implemented on in many user Controls like (Button,Chekbox,Address etc).

i am converting this code to C#.net.
code is

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

it shows an exception ctlControl:

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

Most WinForm controls inherit from System.Windows.Forms.Control , if you want that the control implements your IControl-Interface then you have to extend the basic-controls.

Like:

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

To dynamiclly add controls to your Form you can this example code:

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

Dennis I already done exactly this

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);

    }
   }

same in ChekBox class and CanvasCtl Class Problem is i am using them dynamically

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);
  }

on the line

 objControl = (IControl)ctlControl;

it shows an exception

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

In VB6, you need VBControlExtender object for dynamically adding a control to the Controls collection using the Add method.

It is mostly used for ActiveX controls.

For example

Dim WithEvents dynamicFlexGrid As VBControlExtender

And the you can do things like:

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

In .NET in order to host an ActiveX you need an AxHost subclass. You can create a simple helper like:

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;
        }
    }
}

And then you can declare your variable as

   AxHost dynamicVSFlexGrid;

and use it like this:

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

For more details see my post at: https://www.mobilize.net/blog/dynamically-adding-activex-in-c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM