HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
Go to Page: 12Next
Current Page: 1 of 2
MSHTML and Frames
Posted by: David (12.35.163.---)
Date: Tuesday, 24-Jun-2003, 23:08:32

Hey all,

I am having problems accessing the item method from the IHTMLFramesCollection2.
This is part of the code:

//doc2 = IHTMLDocument2
//at this point the HTML page has been loaded and contains two frames.
//frames.length = 2;

IHTMLFramesCollection2 frames = doc2.frames;
try
{
//i am not quite sure how to use frames.item
object obj = null;
frames.item(ref obj);
}
catch (Exception e) { MessageBox.Show(e.ToString() + " " + e.Message); }

This code throws the following exception:

System.InvalidCastException: No such interface supported
at mshtml.FramesCollectionClass.item(Object & pvarIndex)
...
Any ideas what am i doing wrong???

Thanks a lot in advance.

- Dave


Re: MSHTML and Frames
Posted by: David (12.35.163.---)
Date: Wednesday, 25-Jun-2003, 15:07:04

All i am trying to do is to access every frame on a page. Anybody knows how to do this in C#?
When I request say ... [www.unix4ever.net]
i am only able to see the contents of main.html which is the page that defines the frame structure. What i would like to do is to be able to parse every frame separately.

Any ideas?

Thanks in advance smiling smiley

- David

Re: MSHTML and Frames
Posted by: gio (146.133.13.---)
Date: Friday, 19-Nov-2004, 15:13:46


i've the same problem.
have you found the ideas?

Re: MSHTML and Frames
Posted by: Derek Schwartz (12.10.219.---)
Date: Wednesday, 15-Dec-2004, 07:46:59

I'm trying to figure out the same thing in VB.NET. I'm trying to create a tray application that'll login to our mainframe web interface, check for open and high-severity tickets, and flash the tray icon BEFORE I get paged about them. So far, I've been able to use InternetExplorerClass() to load the page, and then an instance of AxWebBrowser1.Document to fill in the username & password and then hit the submit button for me.

MyWinDoc.all.item("txtUserId").Value = Me.tbUserName.Text
MyWinDoc.all.item("txtPassword").Value = Me.tbPassword.Text
Me.AxWebBrowser1.Navigate2("javascript:JS_Product('ProblemManagement')")
MyWinDoc.forms.item(0).submit()

After that, it goes to a multi-frame page that I need to somehow control by hitting drop-downs and buttons on different frames. This is where I'm completely lost. I can't figure out how to specify a certain frame to work with. I found an example that someone else got to work, but it won't work for me...

Dim MyWindow As mshtml.IHTMLWindow2 = MyFrameCollection.item("Frame3")

... then you're just supposed to use the new object MyWindow to enumerate and manipulate the various controls in that frame. I think my problem is that the Java junkies who wrote the frame-based system I'm trying to manipulate didn't bother to assign frame tags, so there is no way to reference them by name. If that's my only problem, then this might work for you if your frames do have tags.

With this thought in mind, I was attempting to build a list of frame objects, using [mshtml.IHTMLDocument2.frames.length] to get the number of frames and work from there. I can get the number, but can't seem to figure out how to pull each object to add to an object collection. If I can get that down, I might be able to differentiate between the frames by just looking at the number of controls on each as a kind of signature.

Please let me know if you find a way to eaily manipulate frame contents (tagless)! The MSHTML documentation on MSDN isn't exactly ".NET Friendly"...

-Derek

Re: MSHTML and Frames
Posted by: Ender (---.uzgent.be)
Date: Friday, 28-Jan-2005, 13:14:19

Hello,

I've had the same problem in VB.NET, getting an invalid cast exception when trying to acces the frames collection

Dim IEWindows As New SHDocVw.ShellWindows
Dim IE As SHDocVw.WebBrowser

For Each IE In IEWindows
MessageBox.Show("Browser window " & IE.LocationURL)
If IE.LocationURL.IndexOf("start.aspx?NewWindow=1") >= 0 Then
If MessageBox.Show(IE.LocationURL, "Submit form?", MessageBoxButtons.OKCancel) = DialogResult.OK Then

MessageBox.Show("Type of document is " & IE.Document.GetType().ToString)
Dim oDoc As mshtml.HTMLDocument = CType(IE.Document, mshtml.HTMLDocument)

' ------ Error occurs here ---------
MessageBox.Show("There are " & oDoc.frames.length & " frames")

End If

End If
Next


The information provided by MS is ridiculously 'thin'

Re: MSHTML and Frames
Posted by: Philippe (---.adsl.proxad.net)
Date: Thursday, 24-Feb-2005, 15:37:32

Same problem with VB6, evidently.
I have had an idea :
Create a single
Public WithEvents DocHTML As HTMLDocument
Since it cannot be an array, I create a separate array :
Global Docs() as HTMLDocument
... to store successively the documents of the frames in the download_complete event of the WebBrowser.
Then, everytime a frame becomes "activated", for example the frame number i, maybe possible to write something like
Set DocHTML = Docs(i) and access the events of the DOCHTML object ???
But how is it possible to know when a frame becomes the active one ?

Hope it will help everybody !

Re: MSHTML and Frames
Posted by: Debasish Bose (---.200.22.162.bng.vsnl.net.in)
Date: Friday, 01-Apr-2005, 10:20:16

vey easy !
passing values through ref objects is not a new scenario in C#.NET Inetrop

//doc2 = IHTMLDocument2
//at this point the HTML page has been loaded and contains two frames.
//frames.length = 2;

IHTMLFramesCollection2 frames = doc2.frames;
try
{
object o = null;
for(int i=0; i < frames.length; i++)
{
o = i;
IHTMLFrameElement frame = (IHTMLFrameElement)
docPlayer.frames.item(ref o);

//Do your stuff with frame
}
}
catch{}

Re: MSHTML and Frames
Posted by: Rodney (---.slkc.eli.net)
Date: Friday, 30-Sep-2005, 01:28:12

Did you ever figure out how to parse the contents of each frame? I have been able to find the individual frames, but am still not able to parse the contents - now knowing which object on the frame has the data.

Any suggestions would be greatly appreciated.

Re: MSHTML and Frames
Posted by: David (---.223.240.220.dsl.comindico.com.au)
Date: Tuesday, 18-Oct-2005, 13:46:43

I have this code:

webAccess.Navigate(URL);
// Wait until the navigation is complete
IHTMLDocument2 mDoc = (IHTMLDocument2)(webAccess.Document);
IHTMLFramesCollection2 mFrames = (IHTMLFramesCollection2)(mDoc.frames);

The last line of code fails with the same error. Could someone tell me what I'm doing wrong?

Debasish Bose wrote:

> vey easy !
> passing values through ref objects is not a new scenario in
> C#.NET Inetrop
>
> //doc2 = IHTMLDocument2
> //at this point the HTML page has been loaded and contains two
> frames.
> //frames.length = 2;
>
> IHTMLFramesCollection2 frames = doc2.frames;
> try
> {
> object o = null;
> for(int i=0; i < frames.length; i++)
> {
> o = i;
> IHTMLFrameElement frame = (IHTMLFrameElement)
>
> docPlayer.frames.item(ref o);
>
> //Do your stuff with frame
> }
> }
> catch{}

Re: MSHTML and Frames
Posted by: Patrick Redestam (---.bynxx3.adsl-dhcp.tele.dk)
Date: Wednesday, 19-Oct-2005, 20:15:22

Hi,

After spending the whole day on it I have found a solution.

This is a known issue that Microsoft have chosen not to fix...

[lab.msdn.microsoft.com]

The problem is that we have to read the InternetExplorer.Document at the right time and from the right thread. We have to wait until the main frameset is loaded, but we have to read before the first frame is completed...

In the example below I know that there are three frames and I want to be sure that they are all loaded before I access them (This is another problem you have to avoid...)

/Patrick


using System;

namespace ConsoleApplication7
{
/// <summary>
/// Summary description for Class1.
/// </summary>
using System;
using mshtml;
using SHDocVw;
using System.Collections;
using System.Threading;

namespace ConsoleApplication1
{
class Class1
{
static InternetExplorer ie = null;

[STAThread]
static void Main(string[] args)
{
Class1 class1 = new Class1();
class1.Test();
Console.Read();
}

AutoResetEvent firstDocumentCompleted;
AutoResetEvent lastDocumentCompleted;
int completedDocuments;
int expectedDocuments;

public void Test()
{
Console.WriteLine("Starting:" + Thread.CurrentThread.ApartmentState.ToString());

ie = new SHDocVw.InternetExplorer();
ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(DocumentComplete);
ie.Visible = true;
firstDocumentCompleted = new AutoResetEvent(false);
lastDocumentCompleted = new AutoResetEvent(false);


completedDocuments = 0;
expectedDocuments = 4;
object o = null;
ie.Navigate("http://www.htmq.com/html/sample/frame.htm", ref o, ref o, ref o, ref o);
firstDocumentCompleted.WaitOne(30000, false);

IHTMLDocument2 document = (IHTMLDocument2)ie.Document;
lastDocumentCompleted.WaitOne(30000, false);

IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)document.frames;
for (int index = 0; index < frames.length; index++)
{
object i = index;
frames.item(ref i);

IHTMLWindow2 window = (IHTMLWindow2)frames.item(ref i);
IHTMLDocument2 frameDocument = (IHTMLDocument2)window.document;

Console.WriteLine(frameDocument.body.innerHTML);
}

Console.WriteLine();
}

public void DocumentComplete(object pDisp, ref object URL)
{
Console.WriteLine("DocumentComplete:" + Thread.CurrentThread.ApartmentState.ToString());

if (completedDocuments == 0)
{
firstDocumentCompleted.Set();
}

Interlocked.Increment(ref completedDocuments);

if (completedDocuments == expectedDocuments)
{
lastDocumentCompleted.Set();
}

}
}
}
}

Re: MSHTML and Frames
Posted by: Patrick Redestam (---.bynxx3.adsl-dhcp.tele.dk)
Date: Wednesday, 19-Oct-2005, 22:07:53

A small change to make the code thread safe...


Replace this code:

Interlocked.Increment(ref completedDocuments);

if (completedDocuments == expectedDocuments)

with:

int i = Interlocked.Increment(ref completedDocuments);

if (i == expectedDocuments)

Re: MSHTML and Frames
Posted by: Patrick Redestam (---.bynxx3.adsl-dhcp.tele.dk)
Date: Wednesday, 19-Oct-2005, 22:20:41

Sorry... I was to quick...

The DocumentComplete method should look like this:

public void DocumentComplete(object pDisp, ref object URL)
{
Console.WriteLine("DocumentComplete:" + Thread.CurrentThread.ApartmentState.ToString());

int i = Interlocked.Increment(ref completedDocuments);

if (i == 1)
{
firstDocumentCompleted.Set();
}

if (i == expectedDocuments)
{
lastDocumentCompleted.Set();
}

}

Re: MSHTML and Frames
Posted by: Tim (---.gotadsl.co.uk)
Date: Wednesday, 19-Oct-2005, 22:47:23

This looks really useful, thanks.

Do you have time to make a wiki entry on the subject?

Tim

Re: MSHTML and Frames
Posted by: Patrick Redestam (---.bynxx3.adsl-dhcp.tele.dk)
Date: Thursday, 20-Oct-2005, 08:09:50

Hi again,

I should have waited to open my mouth before I had investigated it completely...

The example I sent do work fine, but my arguments are wrong...

You can read it whenever you feel like as long as you do it from the main thread and use [STAThread].

This guy seems to know more about STAThread

[blogs.msdn.com]

/Patrick

Off topic (Was: Re: MSHTML and Frames)
Posted by: Henrik Bengtsson (---.wehi.edu.au)
Date: Thursday, 24-Nov-2005, 00:56:09

Patrick, you're one of those guys not leaving tracks on the web winking smiley I've been trying to find your email address without success (the old ones I've got do not work). Send me an email to my braju.com account. Cheers Henrik B.

Re: MSHTML and Frames
Posted by: summer (---.sw.biz.rr.com)
Date: Wednesday, 18-Jan-2006, 20:07:56

Ok, I understand that it works properly when calling it from the main thread. However, I need to call code to get the frames in response to an event:

IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)webpage.frames;

Any suggestions on how to get from the event thread back to the main thread in order to call this code. I tried delcaring a delegate and on my event handler invoking the delegate. however i still get the Cast Not Valid Exception.

Thanks
Summer
=

Re: MSHTML and Frames
Posted by: netZ (---.hsd1.nj.comcast.net)
Date: Thursday, 02-Feb-2006, 03:11:29

ran into the same issue... 2 ways:

1) create a form with a timer with a method that checks a boolean flag

2) ondocumentcomplete check the url for the page you wish to inspect, set the boolean flag to true

3) the form's timer will trigger and the event handler should be able to iterate through the frames

the other way requires use of a delegate.

Re: MSHTML and Frames
Posted by: smr (62.139.87.---)
Date: Sunday, 16-Apr-2006, 01:50:15

i am trying to access elements in the pages...
i had the same problem of frames,as when i accesd last document just retrive the code of one frame...
anyway,i tried ur code...it is very usefull but have some problems in it
when i try it in that page(http://msdn.microsoft.com/library/en-us/default.asp?frame=true)

it make strange things...

at first i tried the code in the event downloade complete it bring me 6 document but it in fact it return the document of only three frames and duplicate them ...and missing some document of another frames..

i tried the code on document complet it bring me that time 27 document and again just duplicate 3 document
and other document for other frames are missing...

can anyone tell me why

Re: MSHTML and Frames
Posted by: MH (---.cpe.net.cable.rogers.com)
Date: Thursday, 11-May-2006, 17:20:16

Don't use FramesCollection, it could contain IFrames at times and is very unreliable.

m_WBWebBrowser2 = IWebbrowser2 interface

IHTMLDocument2 pDoc2 = (IHTMLDocument2)m_WBWebBrowser2.Document;
//Account for frames
IHTMLElement pElem = pDoc2.activeElement;
try
{
IWebBrowser2 pFrame = (IWebBrowser2)pElem;
while (pFrame != null)
{
pDoc2 = (IHTMLDocument2)pFrame.Document;
pElem = pDoc2.activeElement;
pFrame = (IWebBrowser2)pElem;
}
}
catch (Exception) //InvalidCastException
{
}
pElem = pDoc2.body;
IHTMLBodyElement pBodyelem = (IHTMLBodyElement)pElem;

Hope this helps

Re: MSHTML and Frames
Posted by: omkar (---.ac.in)
Date: Monday, 11-Sep-2006, 10:44:52

hi..
i am trying to hide one frame to another frame.
i am creating two frame in the form.
in 1st frame i button is there when i clicking the button the 2nd frame will disable.i am using c#.net.
how to do?ple give me suggestion.
or code for that problem
thanks

Go to Page: 12Next
Current Page: 1 of 2


Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
HtmhE
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.