Access ActiveX Object Control in Visual Studio 2008

I encountered a problem in Microsoft MSDN Visual Studio Debugger forum, the original thread is:
http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/64a02264-2beb-4794-913b-a96ec0ea0c58

I have to say, it’s an amazing thing to reproduce what has happened to the customer. However, there are a lot learned by the process
during the reproduce.

ISSUE REPRODUCE
=====================

Here is the process that I reproduce the problem, we could learn how to access ActiveX Object controls (an MSFlexGrid control in VB6 in this case.) from within.

  1. Find the original trouble maker – MSFlexGrid control
Open Microsoft Visual Basic 6.0, and create a new standard project, right click the toolbox and choose Components... to open the Components dialog box, here,
we find the “Microsoft FlexGrid Control 6.0 (SP6), and its location is D:"Windows"System32"MSFlxGrd.ocx (just the path on my box, not needed the same.)
If you did not install Visual Basic 6.0, the file MSFlxGrd.ocx could be download anywhere in the Internet.

    2. Since the FlexGrid control is a COM component, however, Windows Forms Applications only host Windows Forms controls – that is, classes that are derived
from Control. We need to convert type defined in a COM type library for an ActiveX control into a Windows Forms control.
We need a tool called Aximp.exe[http://msdn.microsoft.com/en-us/library/8ccdh774(VS.80).aspx] to complete this job.

    3. Convertion.
Open Visual Studio 2008 Command Prompt and input aximp [path]"MSFlxGrd.ocx. After this step, we could see two files were generated under the folder
C:"Program Files"Microsoft Visual Studio 9.0"VC, one is named MSFlexGridLib.dll and another is named AxMSFlexGridLib.dll

    4. Using the ActiveX control
Now, Open Visual Studio 2008 and create a new Windows Forms Application, Open the Toolbox and right click the blank area and choose “Choose Items...”
(this will open the “Choose Toolbox Items” dialog box). Click COM Components tab then, and find “Microsoft FlexGrid control, version 6.0” checkbox. Check it without
any doubt and then click OK button. A moment later, an icon something looks like: will appear on the Toolbox,
drag and drop it to the new form we just created.

    5. Problem reproduce
paste the code snippet in our project, set a breakpoint in a right place, then debug..... I’ve to say, the first turn of the loop has been executed successfully,
and when executing the second turn of the for loop, the exception was thrown:

ROOT CAUSE:
===============

A simple word to describe the cause is that the index of the Col is out of range of the Cols property of the ActiveX control.

Initially, we could set properties of the ActiveX control through the ActiveX –Properties, like this:

we could see from the above screen shot that the Cols is 5, if we write managed code like:for( int i=0; i<10; i++)
{
       this.axMSFlexGrd.Col = i;
       this.axMSFlexGrid.Text = “Node”+i.ToString();
}
I can assure that you’ll receive the COMException notification!
Since CLR could not find the location out of the range (in this case, the default Row is set to 0 and for each row the Cols value is 5.)

Solution:
==========

Got the Cols and Rows property of the control first and then set value to the corresponding cell. For example:
int cols = this.axMSFlexGrid1.Cols;

int rows = this.axMSFlexGrid1.Rows;

for (int j = 0; j < rows; j++)

{

    this.axMSFlexGrid1.Row = j;

    for (int i = 0; i < cols; i++)

    {

        this.axMSFlexGrid1.Col = i;

        this.axMSFlexGrid1.Text = "Node " + (i*j).ToString();

    }

}

APPENDX:
============
We could manually set the Cols and Rows of the control and then assign each cell a value. Please try it yourself!

posted @ 2009-07-02 16:38  Roahn Luo  阅读(1150)  评论(0编辑  收藏  举报