i want to know how to do when the form is closed, the current contents of the listbox will be stored in the bookmarks textfile (called bookmarks.txt). If a file error occurs, display a messagebox specifying the error.

public Form1()
        {
            InitializeComponent();
        }

        private void btn_go_Click(object sender, EventArgs e)
        {
            wBrowser.Navigate(tbx_website.Text);
        }

        private void btn_bookmark_Click(object sender, EventArgs e)
        {
            //Add the url to the listbox
            LBlist.Items.Add(tbx_website.Text);

            if (LBlist.Items.Count == 0)
            {
                btn_delete.Enabled = false;
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            // The Delete button was clicked
            int i_delete = LBlist.SelectedIndex;
            
            try
            {
                // Remove the item in the List.
                LBlist.Items.RemoveAt(i_delete);

            }
            catch
            {
            }
        }

        private void listbox_DoubleClick(object sender, EventArgs e)
        {
            tbx_website.Text = LBlist.SelectedItem.ToString();     
        }

        private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            tbx_website.Text = wBrowser.Url.ToString();
        }

        private void LBlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_delete.Enabled = (LBlist.Items.Count > 0);
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btn_delete.Enabled = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            StreamWriter sw_file;
            string s_bmfile = LBlist.Items.ToString();
            sw_file = new StreamWriter("bookmarks.txt");
            sw_file.Write(s_bmfile);
            sw_file.Close();
        }

Hello, i dont understand exatly that you ask but take a look here

private void listbox_DoubleClick(object sender, EventArgs e)
        {
            tbx_website.Text = listbox.SelectedItem.ToString();
        }

this up means on double click on all listbox, not in items, you can have no select any item and click whatever in listbox and event will be run, you will get problem ofc, anyway

so that you want is to create your own web browser?

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace Mozile_Browser
{
    // Program Entry Point
    class Beggin
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new IExplorer());
        }
    
    }

    // Main Programm
    class IExplorer : Form
    {
        public IExplorer()
        {
            // Form Design
            IEDesign();
            // Add Controls
            RegisterControls();
        }

        private void IEDesign()
        {
            this.Size = new Size(900, 600);
            this.Name = "IExplorer";
            this.Text = "Internet Explorer 10";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.WindowState = FormWindowState.Maximized;

        }

        // Decleare that we need
        private WebBrowser wBrowser;
        private ListBox liBox;
        private Button bGo;
        private Button bDelete;
        private Button bBookmark;
        private TextBox tbURL;
        private Panel _Panel;
        private Label lbURL;

        // Add All Controls in Form
        private void RegisterControls()
        {
            // Web Browser
            wBrowser = new WebBrowser();
            wBrowser.Url = new Uri("http://google.gr");
            wBrowser.Dock = DockStyle.Fill;

            // ListBox
            liBox = new ListBox();
            liBox.Dock = DockStyle.Left;
            liBox.Width = 180;

            //Button Go
            bGo = new Button();
            bGo.Text = "Go";
            bGo.Dock = DockStyle.Left;

            //Button Delete
            bDelete = new Button();
            bDelete.Text = "Delete";
            bDelete.Dock = DockStyle.Left;
            // Delete Button State = Disabled
            bDelete.Enabled = false;

            // Button Bookmark
            bBookmark = new Button();
            bBookmark.Text = "Book Mark";
            bBookmark.Dock = DockStyle.Left;

            // URL Box
            tbURL = new TextBox();
            tbURL.Dock = DockStyle.Top;
            tbURL.BackColor = Color.DarkSeaGreen;
            tbURL.Multiline = false;
            tbURL.Height = 25;
            tbURL.ForeColor = Color.Azure;
            tbURL.Font = new Font("Segoe UI Semibold", 10);
           
            //URL Info Label
            lbURL = new Label();
            lbURL.AutoSize = true;
            lbURL.Visible = false;
            lbURL.BackColor = Color.FromArgb(70, Color.ForestGreen);
            lbURL.Font = new Font("Segoe UI Semibold", 10);
            lbURL.Location = new Point(this.Width / 2, 4);

            // Panel
            _Panel = new Panel();
            _Panel.Dock = DockStyle.Top;
            _Panel.Height = 25;

            // Add Buttons & URL Box to Panel
            _Panel.Controls.AddRange(new Control[] { lbURL, bGo, bDelete, bBookmark });

            // Finally Add Them
            this.Controls.AddRange(new Control[] { wBrowser, liBox,tbURL,_Panel });

            // Add Events
            bGo.Click += new EventHandler(bGo_Click);
            bBookmark.Click += new EventHandler(bBookmark_Click);
            bDelete.Click += new EventHandler(bDelete_Click);
            liBox.MouseDoubleClick += new MouseEventHandler(liBox_MouseDoubleClick);
            liBox.MouseClick += new MouseEventHandler(liBox_MouseClick);
            wBrowser.Navigated += new WebBrowserNavigatedEventHandler(wBrowser_Navigated);
            this.Load += new EventHandler(IExplorer_Load);
            this.FormClosed += new FormClosedEventHandler(IExplorer_FormClosed);

        }

        // Enable Delete Button
        private void liBox_MouseClick(object sender, EventArgs e) {
            bDelete.Enabled = true;
        }

        // Load clicked bookmark
        private void liBox_MouseDoubleClick(object sender, EventArgs e)
        {
            if (liBox.SelectedItem != null)
            {
                if (wBrowser.Url.Equals(liBox.SelectedItem.ToString()))
                { return; }
                else {
                    wBrowser.Navigate(liBox.SelectedItem.ToString());
                }
            }
        }

        // Go to site
        private void bGo_Click(object sender, EventArgs e)
        {
            bDelete.Enabled = false;

            // Check if Box is null or have spaces
            if (String.IsNullOrEmpty(tbURL.Text.ToString()) ||
                String.IsNullOrWhiteSpace(tbURL.Text.ToString()))
            {
                lbURL.Text = "Please insert an right address and try again.";
                lbURL.Visible = true;
                return;
            }

            // Add perfix "http://" if dont exist
            if (!tbURL.Text.StartsWith("http://") &&
                !tbURL.Text.StartsWith("https://"))
            {
                tbURL.Text = "http://" + tbURL.Text.ToString();
            }

            try {
                lbURL.Visible = false;
                wBrowser.Navigate(tbURL.Text.ToString());
            }
            catch (System.UriFormatException) {
                return;
            }
        }

        // Add Book Mark
        private void bBookmark_Click(object sender, EventArgs e)
        {
            // Check if BookMark Exist
            for (int x=0; x < liBox.Items.Count; x++)
            {
                if (liBox.Items[x].Equals(wBrowser.Url.ToString())) {
                    lbURL.Visible = true;
                    lbURL.Text = "You cann't add this address because this one already exist.";
                    return;
                }
            }
            // Add Bookmark
            liBox.Items.Add(wBrowser.Url.ToString());
        }

        // Delete Bookmark
        private void bDelete_Click(object sender, EventArgs e)
        {
            liBox.Items.RemoveAt(liBox.SelectedIndex);
            liBox.Refresh();
        }

        // Show URL to URLBox From WebBrowser
        private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            lbURL.Text = "";
            tbURL.Text = wBrowser.Url.ToString();
        }

        // Store Bookmark
        private void IExplorer_FormClosed(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"bookmark.txt", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);

            try
            {
                for (int x = 0; x < liBox.Items.Count; ++x)
                {
                    sw.WriteLine(liBox.Items[x].ToString());
                    sw.Flush();
                }
            }
            catch {
                lbURL.Visible = true;
                lbURL.Text = "An error happen while storing bookmark.";
            }
            finally {
                sw.Close();
            }
        }

        // Create Bookmark on load
        private void IExplorer_Load(object sender, EventArgs e)
        {               
            FileStream fs = new FileStream(@"bookmark.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            try
            {
                while (sr.Peek() >= 0) {
                    liBox.Items.Add(sr.ReadLine());
                }
            }
            catch {
                lbURL.Visible = true;
                lbURL.Text = "An error happe while reading data.";
            }
            finally
            { sr.Close(); }

        }
    }
}

Edited 5 Years Ago by Panathinaikos22: n/a

private void listbox_DoubleClick(object sender, EventArgs e)
{
    tbx_website.Text = listbox.SelectedItem.ToString();
}

its tbx_website.Text = LBlist.SelectedItem.ToString(); instead of
listbox.SelectedItem.ToString();

if double click the selected item from the listbox it goes directly textbox then click the go button.

the instruction says:
*A text file of bookmarks will be loaded into the listbox. If the text file does not exist, or an exception occurred, a messagebox will be used to display an error message.
*When the form is closed, the current contents of the listbox will be stored in the bookmarks textfile (called bookmarks.txt). If a file error occurs, display a messagebox specifying the error.


there is something wrong my listbox.
Everytime i opene the program the list i have is "System.IO.StreamReader".
and when i close it give warning: "The process cannot access the file because it is being used by another process.
This warning doesn't pop if i bookmark(add) some website to my listbox

private void btn_go_Click(object sender, EventArgs e)
        {
            wBrowser.Navigate(tbx_website.Text);
        }

        private void btn_bookmark_Click(object sender, EventArgs e)
        {
            //Add the url to the listbox
            LBlist.Items.Add(tbx_website.Text);

            if (LBlist.Items.Count == 0)
            {
                btn_delete.Enabled = false;
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            // The Delete button was clicked
            int i_delete = LBlist.SelectedIndex;
            
            try
            {
                // Remove the item in the List.
                LBlist.Items.RemoveAt(i_delete);

            }
            catch
            {
            }
        }

        private void listbox_DoubleClick(object sender, EventArgs e)
        {
            wBrowser.Navigate(LBlist.SelectedItem.ToString());     
        }

        private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            tbx_website.Text = wBrowser.Url.ToString();
        }

        private void LBlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_delete.Enabled = (LBlist.Items.Count > 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // test to exists 'bookmarks.txt' in the current path
            if (File.Exists("bookmarks.txt"))
            {
                StreamReader sr = new StreamReader("bookmarks.txt");
 
                foreach (string item in sr.ToString().Split(new char[] { ';' }))
                    LBlist.Items.Add(item);
            }
            else
            {
                MessageBox.Show("No found the file!");
            }
            btn_delete.Enabled = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            StreamWriter sw_file;
            string s_bmfile = "";

            foreach (object item in LBlist.Items)
                s_bmfile += item.ToString() + ";";

            try
            {
                sw_file = new StreamWriter("bookmarks.txt"); 
                sw_file.WriteLine(s_bmfile);
                sw_file.Close();
            }
            catch
            {
                MessageBox.Show(ex.Message);
            }
            
        }
This article has been dead for over six months. Start a new discussion instead.