Wednesday, September 3, 2008

Problem when creating a dynamic form with ActiveX control & Solution :)

The Requirement was to connect to the web and get the content of the web page and display it on

the form .Well, I did not want to create a form in AOT to display the HTML content .So , I thought of dynamically creating a form and adding the browser ActiveX Control using X++ code.

Here is the code which I pasted in the job.

static void ActiveX_DynamicForm(Args _args)

{

COMDispFunction webpreviewWrite;

COMVariant text = new COMVariant();

COM ctrl = new COM();

Form formBrowse;

FormActivexControl activex;

FormRun formRun;

Args args;

int handle;

WinInet wi = new WinInet();

str htmlContent;

;

handle = wi.internetOpenUrl('http://www.yahoo.com');

if (handle)

{

htmlContent = wi.internetReadFile(handle);

}



wi.internetCloseHandle(handle);

formBrowse = new Form('ActiveX AX Form', true);

formBrowse.design().width(500);

formBrowse.design().height(500);

formBrowse.design().addControl(FormControlType::ActiveX, 'Browser');

args = new Args(formBrowse.name());

args.name(formBrowse.name());

args.object(formBrowse);

formRun = classFactory.formRunClass(args);

formRun.init();

activex = formRun.design().controlName('Browser');

activex.className('{8856F961-340A-11D0-A96B-00C04FD705A2}');

activex.height(1,1);

activex.width(1,1);

formRun.run();

ctrl.attach(activex.interface());

webpreviewWrite = new COMDispFunction(ctrl, 'write', COMDispContext::Method);

text.bStr(htmlContent);

activex.open("");

webpreviewWrite.call(text);

formRun.detach();

}

I was successful in creating the form but strangely AX threw me an error when I ran the code. It was not supporting ‘write’ method for automatic interface of COM object of class “IWEBBrowser2’

I did not want to create a separate form adding an ActiveX control to achieve this.

I looked for alternatives and found one interesting class which served my purpose.

Class Name: KMKnowledgeFunctionShow

Method : static Object show(TextBuffer htmlText, Object _formRun = null)

The show method will open the standard “KMKnowledgeAnalogMeter” form which has an ActiveX Control added to it.

I got the content of the webpage and added it to the TextBuffer by using settext() method and passed the TextBuffer to the show method. I did not pass the second parameter formrun.

The idea behind this is to open the existing Standard form which has already Browser ActiveX in it.

Here is the code

static void ActiveX_Alternate(Args _args)

{

str htmlContent;

TextBuffer txtBuffer;

int handle;

WinInet wi = new WinInet();

;

handle = wi.internetOpenUrl('http://www.yahoo.com/');

if (handle)

{

htmlContent = wi.internetReadFile(handle);

}

txtBuffer = new TextBuffer();

txtBuffer.setText(htmlContent);

KMKnowledgeFunctionShow::show(txtBuffer);

}

Drawback: The form caption stills remains the “KMKnowledgeAnalogMeter “form caption

No comments: