Link to home
Start Free TrialLog in
Avatar of BurningWheels
BurningWheels

asked on

Refresh frame webbrowser

Hi Experts,

I would like to know how to refresh a childframe or iframe within a webpage. I don´t use the standard context menu. But the refresh option in the standard context menu is what i would like to be able to do. When i use webbrowser1.refresh it reloads the entire page not just the frame where the mouse is over.

Any suggestions?

Thanks.
Avatar of gladiatorno9
gladiatorno9

If a popup window has been opened from another page that utilizes frames this script will refresh the frame that the popup was opened from.

<!--From a regular popup window:-->

if (opener && !opener.closed) opener.location.reload(1);

<!--If the popup window itself is a frameset:-->

top.opener.location.reload(1);

<!--To close the popup and refresh the parent:-->

opener.location.reload(1);window.close;
Avatar of BurningWheels

ASKER

I am displaying a frameset or a webpage with an iframe. I want to refresh the frame which the mouse is over. I am using the webbrowser control in Visual Basic .NET. The solution you provided has to do with the webpage itself as far as i understood it.
You can find a frame webbrowser in 2.0
liek

WebBrowser1.Document.Window.Frames(2)

where 2 is the number of frame. you can iterate thourgh frames

for webbrowser in 1.1.

https://www.experts-exchange.com/questions/21166050/How-to-use-axWebBrowserto-get-the-HTML-source-of-the-page-in-a-frameset.html

After finding the frame, you can use the navigate method to navigate it to self
How can you which frame the mouse is over??
each frame is an Object of HtmlWindow,
in dotnet 2.0
attach GotFocus handler to it
Somthing like
Private Sub frame1_GotFocus(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles x.GotFocus

    End Sub


...in Dotnet 1.1
you can look for mshtml.HTMLWindow2Class and
and try events
Private Sub y_HTMLWindowEvents_Event_onfocus() Handles y.HTMLWindowEvents_Event_onfocus

End Sub

Private Sub y_HTMLWindowEvents2_Event_onfocus(ByVal pEvtObj As mshtml.IHTMLEventObj) Handles y.HTMLWindowEvents2_Event_onfocus

End Sub
If above things fails, which i think will not ..  :)
Then

handle the click event of all Frame Documents , Something like

Private Sub document_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles document.Click


and you will know which doucment was clicked
Hi
Why have you given C. Wasnt the answer helpfull ?
The answer was helpfull.

The problem with your code is that i don´t know how many frames and frames within those frames are being showed. I now have to iterate through all frames detect if the mouse is over by checking mouse position versus frame x and y and widht and height. Then check all frames within those frames etc... So it´s not a solution that works in every scenario.

What i really want to do is mimic the refresh function in the context menu of Internet Explorer.

Any other suggestions?
Actually in my opinion. 2nd last option which i gave will work for all scenarios.

cuz in main document complete.. you can just make a recursive function which can give you all the frames and you can add gotfocused event to all of them..
Then where ever is mouse clicked. you will get the corresponding Frame. and you will just do the navigate to self

Could you explain that option a little further? When I copy your code i get a message:
Error      1      Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

I´m quite inexperienced when it comes to vb.net... ;)

This is the code i use now.... I don´t trust it entirely... in some cases it works sometimes not..

Dim tel As Integer, tel2 As Integer, tel3 As Integer, MouseX As Integer, MouseY As Integer
        Dim FrameX1 As Integer, FrameX2 As Integer, FrameY1 As Integer, FrameY2 As Integer

        MouseX = MousePosition.X.ToString
        MouseY = MousePosition.Y.ToString

        For tel = 0 To WebBrowser1.Document.Window.Frames.Count - 1
            FrameX1 = WebBrowser1.Document.Window.Frames(tel).Position.X
            FrameX2 = WebBrowser1.Document.Window.Frames(tel).Size.Width + FrameX1
            FrameY1 = WebBrowser1.Document.Window.Frames(tel).Position.Y
            FrameY2 = WebBrowser1.Document.Window.Frames(tel).Size.Height + FrameY1

            If MouseX > FrameX1 And MouseX < FrameX2 Then
                If MouseY > FrameY1 And MouseY < FrameY2 Then

                    If WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames.Count = 0 Then
                        Dim readme As Object = Nothing
                        WebBrowser1.Document.Window.Frames(tel).Document.ExecCommand("refresh", True, readme)
                        Exit Sub
                    Else

                        For tel2 = 0 To WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames.Count - 1

                            FrameX1 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Position.X
                            FrameX2 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Size.Width + FrameX1
                            FrameY1 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Position.Y
                            FrameY2 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Size.Height + FrameY1

                            If MouseX > FrameX1 And MouseX < FrameX2 Then
                                If MouseY > FrameY1 And MouseY < FrameY2 Then

                                    If WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames.Count = 0 Then
                                        Dim readme As Object = Nothing
                                        WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.ExecCommand("refresh", True, readme)
                                        Exit Sub
                                    Else



                                        For tel3 = 0 To WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames.Count - 1

                                            FrameX1 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Position.X
                                            FrameX2 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Size.Width + FrameX1
                                            FrameY1 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Position.Y
                                            FrameY2 = WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Size.Height + FrameY1

                                            If MouseX > FrameX1 And MouseX < FrameX2 Then
                                                If MouseY > FrameY1 And MouseY < FrameY2 Then

                                                    If WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Document.Window.Frames.Count = 0 Then
                                                        Dim readme As Object = Nothing
                                                        WebBrowser1.Document.Window.Frames(tel).Document.Window.Frames(tel2).Document.Window.Frames(tel3).Document.ExecCommand("refresh", True, readme)

                                                        Exit Sub
                                                    Else

                                                    End If

                                                End If
                                            End If

                                        Next tel3

                                    End If

                                End If
                            End If

                        Next tel2

                    End If

                End If
            End If

        Next tel
Ok :)

let me give you the VB code

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandlerToFrames(WebBrowser1.Document.Window.Frames)
    End Sub
    Private Sub AddHandlerToFrames(ByVal h As HtmlWindowCollection)
        For Each window As HtmlWindow In h
            RemoveHandler window.GotFocus, AddressOf s1_GotFocus
            AddHandler window.GotFocus, AddressOf s1_GotFocus

            If window.Frames IsNot Nothing AndAlso window.Frames.Count > 0 Then
                AddHandlerToFrames(window.Frames)
            End If
        Next
    End Sub

    Private Sub s1_GotFocus(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        CType(sender, HtmlWindow).Document.ExecCommand("refresh", False, Nothing)
    End Sub
Thanks for the code. It doesn´t seem to work as i expected. Now, everytime a frame receives focus it refreshes. Is there a way to make it refresh when a user clicks a menu item??

Something like:

ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
       CType(sender, HtmlWindow).Document.ExecCommand("refresh", False, Nothing)
End Sub

If it is going to work. I will give you more credit. if i can figure out how to reconsider my score for your answers.
Oh sorry Send GotFocused Event.
It should be click event. Something like

 Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandlerToFrames(WebBrowser1.Document.Window.Frames)
    End Sub
    Private Sub AddHandlerToFrames(ByVal h As HtmlWindowCollection)
        For Each window As HtmlWindow In h

            RemoveHandler window.Document.Click, AddressOf s_Click
            AddHandler window.Document.Click, AddressOf s_Click

            If window.Frames IsNot Nothing AndAlso window.Frames.Count > 0 Then
                AddHandlerToFrames(window.Frames)
            End If
        Next
    End Sub
    Private Sub s_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
        CType(sender, HtmlDocument).ExecCommand("refresh", False, Nothing)
    End Sub



To reconsider the score.

Post a question in Community
https://www.experts-exchange.com/Community_Support/
to reopen Question
https://www.experts-exchange.com/questions/21956182/Refresh-frame-webbrowser.html
for changing of grades
Now everytime i click on a page it refreshes. How do i trigger this only when i click an item in a custom menu?
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I posted a message in Community to change the grades

You´re receiving 200 points and a grade A