Quantcast
Viewing all articles
Browse latest Browse all 12583

C# Webbrowser, forward and back history

I created two contextmenustrips, one called backhistorystrip and forwardhistorystrip, the former stores a list of urls the user has navigated to and the latter stores urls that the user has navigated to, only the user has gone back already so it takes one and puts it in the other.

When the browser navigates, a new menustripitem is added to backhistorystrip, if the url is the same, it doesn't add it, so it isn't added twice when the user decides the refresh the page.

The following code is placed in the browser1_Navigated event If the user goes forward, the browser navigates again, so if the next item (which is always added to the top, 0) has the same url then a menustripitem is removed from fowardhistorystrip, and the code in the previous paragraph will move that url back into backhistorystrip.

ToolStripMenuItem newitem = new ToolStripMenuItem();
            if (backhistorystrip.Items.Count != 0)
            {
                if (browser1.Url.AbsoluteUri != backhistorystrip.Items[0].Text)
                {
                    backhistorystrip.Items.Insert(0, newitem);
                }
            }
            else
            {
                backhistorystrip.Items.Insert(0, newitem);
            }
            try
            {
                if (browser1.Url.AbsoluteUri == forwardhistorystrip.Items[0].Text)
                {
                    forwardhistorystrip.Items.RemoveAt(0);
                }
            }
            catch
            {
            }
            newitem.Text = browser1.Url.AbsoluteUri;
            foreach (ToolStripMenuItem backhistorypage in backhistorystrip.Items)
            {
                //backhistorypage.Click += new System.EventHandler(backhistorypage_Click)
//NOT SURE WHAT TO PUT IN backhistorypage_Click event;
            }

Going back is quite simple, if the user clicks back it simply inserts the top url fro,m backhistorystrip to the forwardhistorystrip and finally removes that item from backhistorystrip. Going back is even more simple, it simply removes the top url from forward historystrip, the code seen in the browser1_Navigated justs adds it back.

In the browser1_navigating event, I have this code to ensure that if the user were to go back and click on a different url, the forwardhistorystrip would be erased.However, this means fowardmenustrip can only display 1 url, the browser can still go forward multiple times though, I tried placing it in other events like "_documentcompleted" and "_Navigated" and tweaking the numbers a but there's always something wrong.

if (forwardhistorystrip.Items.Count != 0)
{
    if (browser1.Url.AbsoluteUri != forwardhistorystrip.Items[0].Text)
    {
        forwardhistorystrip.Items.Clear();

    }
}

But the problem is, a user cannot click on these urls, which are menustripitems, to navigate to a previously visited page and most of all, I want them to display the documenttitle as the text but navigate to a url when clicked upon.


Another problem: If you click the back and/or foward buttons too fast, they mess up their respective contextmenustrips, should I put recording code in _navigated event or _documentcompleted event?


Viewing all articles
Browse latest Browse all 12583

Trending Articles