Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Everything is working except for my questions; what's the code for:
*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.

Thank you...
C#
    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)
    {
        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();
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-11 20:14pm    
Not clear what's the problem.
--SA

C#
private void Form1_Load(object sender, EventArgs e)
{
    LBlist.Items.Clear();

    // 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.ReadToEnd().Split(new char[] { ';' }))
            LBlist.Items.Add(item);

        sr.Close();
    }
    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.Write(s_bmfile);
        sw_file.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 
Share this answer
 
v2
Comments
[no name] 29-Sep-11 21:48pm    
i tried it but everytime i start the program
the only list i have from my listbox 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 some website to my listbox
hzawary 1-Oct-11 13:29pm    
Oh! Sorry to late it, this solution is to improve, I hope best for you:)
Bala Selvanayagam 1-Oct-11 16:17pm    
Jonathan C101, Please let me know whether my solution helped you ?
hzawary 1-Oct-11 16:47pm    
Bala Selvanayagam, OP want to read and write from a file (.txt);)
* Create a new windows applicaiton project & add a new form

* Add controls textBox1,comboBox1,btnAdd (button),brnBrowse,btnRemove,btnRemove

* Add
using System.IO;


*try the following code, you can type in a string in the text box and click btnAdd and see the data is in the combo box, you can close the applicaion and open it the data will be still there -- hope this helps

C#
public partial class Form1 : Form
   {

       public readonly string xsdPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "url.xsd");
       public readonly string xmlPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "url.xml");
       private DataSet ds;

       public Form1()
       {
           InitializeComponent();
       }

       private void LoadDataset(){

           try
           {
               if (File.Exists(xsdPath))
               {
                   ds = new DataSet();
                   ds.ReadXmlSchema(xsdPath);
               }
               else
                   CreateSchema();


               if (File.Exists(xmlPath))
                   ds.ReadXml(xmlPath, XmlReadMode.IgnoreSchema);

           }
           catch (Exception ex) { throw ex; }
       }

       private void CreateSchema()
       {
           try{

               ds = new DataSet("dUrl");
               var tUrl = ds.Tables.Add();
               tUrl.Columns.Add("Url",typeof(string));

           }
           catch (Exception ex) { throw ex; }
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           try
           {
               LoadDataset();
               RefreshData();
           }
           catch (Exception ex) { MessageBox.Show(ex.Message); }
       }

       private void RefreshData()
       {
           try
           {
               comboBox1.DataSource = ds.Tables[0];
               comboBox1.DisplayMember = "Url";
           }
           catch (Exception ex) { throw ex; }
       }

       private void Form1_FormClosing(object sender, FormClosingEventArgs e)
       {
           ds.WriteXml(xmlPath, XmlWriteMode.DiffGram);
       }

       private void btnAdd_Click_1(object sender, EventArgs e)
       {
           try
           {
               if (textBox1.Text.Trim().Length != 0)
               {
                   ds.Tables[0].Rows.Add(textBox1.Text);
               }
               RefreshData();
               textBox1.Text = "";
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void btnRemove_Click(object sender, EventArgs e)
       {
           ds.Tables[0].Rows[comboBox1.SelectedIndex].Delete();
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900