Jump to content

How do I implement history for a web browser in c#?

Kyph4n

I'm not familiar enough with c# to have a good idea of how to ask this, but how do I take the text from the address bar and input the strings into a list that can be accessed somehow?

 

I would imagine doing something involving taking the string with the event "Text update", but I don't know how to do it and can't find anything online! (probably because I am unsure exactly what to ask)

 

Thanks

 

Link to comment
Share on other sites

Link to post
Share on other sites

There are different Web viewers in C#, you'll have to specify which one you're using. I am however pretty sure that your URL bar is just an input field though.

 

You'll be wanting to look for something like webview.navigatedTo, you know as a general direction I'm sure there's something like that.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, ManIkWeet said:

There are different Web viewers in C#, you'll have to specify which one you're using. I am however pretty sure that your URL bar is just an input field though.

 

You'll be wanting to look for something like webview.navigatedTo, you know as a general direction I'm sure there's something like that.

I'm using visual studio and making the web browser as a windows form application. I'm not sure what a web view is tbh :D

private void Navigate(String address)
       

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        XmlDocument settings = new XmlDocument();

        public Form1()
        {
            InitializeComponent();
        }

        WebBrowser CharlieBrowser = new WebBrowser();
        int i = 0;

        private WebBrowser getCurrentBrowser()
        {
            return (WebBrowser)tabControl1.SelectedTab.Controls[0];
        }

        private void addNewTab()
        {
            CharlieBrowser = new WebBrowser();
            CharlieBrowser.ScriptErrorsSuppressed = true;
            CharlieBrowser.Dock = DockStyle.Fill;
            CharlieBrowser.Visible = true;
            CharlieBrowser.DocumentCompleted += CharlieBrowser_DocumentCompleted;
            CharlieBrowser.Navigate("http://www.google.co.uk");
            tabControl1.TabPages.Add("New Tab");
            tabControl1.SelectTab(i);
            tabControl1.SelectedTab.Controls.Add(CharlieBrowser);
            i += 1;
        }

        private void closeTab()
        {
            if (tabControl1.TabCount - 1 > 0)
            {
                tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                i -= 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            addNewTab();
        }

        void CharlieBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            tabControl1.SelectedTab.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentTitle;
            comboBox1.Text = CharlieBrowser.Url.AbsoluteUri;
        }

        private void Navigate(String address)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") &&
                !address.StartsWith("https://"))
            {
                address = "http://" + address;
            }
            try
            {
                CharlieBrowser.Navigate(new Uri(address));
            }
            catch (System.UriFormatException)
            {
                return;
            }
        }

        

        //BROWSER CONTROLS
        #region BROWSER CONTROLS
        private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addNewTab();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().GoBack();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().GoForward();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().GoForward();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Stop();
        }

        private void backToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().GoBack();
        }

        private void forwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().GoForward();
        }

        private void homePageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Navigate("http://www.google.co.uk");
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Stop();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Refresh();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Document.ExecCommand("Cut", false, null);
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Document.ExecCommand("Copy", false, null);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Document.ExecCommand("Paste", false, null);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Document.ExecCommand("SelectAll", true, null);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            getCurrentBrowser().Navigate(comboBox1.Text);
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                getCurrentBrowser().Navigate(comboBox1.Text);
            }
        }

        private void duplicateTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (getCurrentBrowser().Url != null)
            {
                Uri dup_url = getCurrentBrowser().Url;
                addNewTab();
                getCurrentBrowser().Url = dup_url;
            }
            else addNewTab();
        }

        private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (new WebBrowser()).Show();
        }

        private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            closeTab();
        }

        #endregion
    }
}
 

This is the current code I have, comboBox1 is the address bar, I need to somehow take the text from that and add it to a list somewhere within the browser but I have no idea how to do this. :P

Link to comment
Share on other sites

Link to post
Share on other sites

Just do  CharlieBrowser. and see what suggestions IntelliSense gives, find if you can get the current URL, once you got it, and only if you got it, only then you navigate away. Ofcourse keeping track of previous URLs is easy at that point...

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Kyph4n said:

I'm using visual studio and making the web browser as a windows form application. I'm not sure what a web view is tbh :D

private void Navigate(String address)

 

-snip-

 

This is the current code I have, comboBox1 is the address bar, I need to somehow take the text from that and add it to a list somewhere within the browser but I have no idea how to do this. :P

Please for the sake of the kittens use code tags...

10 hours ago, prolemur said:

The history could be a text file that you append to each time you switch urls I guess

That would work but I can see scalability being a concern (not in this context so much). You could take it a bit further and use a database such a SQLite for example and store history as well as settings. You could improve your architecture by defining a interface for data access (this would be your model) such that you hide your implementation detail away behind it. This way you could start with a text or XML file and later move to a database. The idea being that the behaviour (saving data somewhere) is consistent but the implementation my change - gives you a great place to add some unit tests.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×