The toolbox i'm using are: listbox, webbrowser, textbox and 3buttons(bookmark, delete. Go).
i have a problem with delete button. At first i can delete what i bookmark, but when i bookmark again
and delete it, it's not working, the delete button becomes disable...
i want to know how to do double click the selectedindex from listbox.
Also, 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.
Thank ypu for your help..
My code is this:
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);
}
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);
if (LBlist.Items.Count == 0)
{
btn_delete.Enabled = false;
}
}
catch
{
}
}
private void listbox_DoubleClick(object sender, EventArgs e)
{
tbx_website.Text = LBlist.SelectedIndex.ToString();
}
private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tbx_website.Text = wBrowser.Url.ToString();
}
private void LBlist_SelectedIndexChanged(object sender, EventArgs e)
{
if (LBlist.Items.Count == 0)
{
btn_delete.Enabled = false;
}
}
Loading
Jonathan CrispePosted Sep 30, 2011, 12:49 AM
*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.
i did what he asked and is in "Form1_Load" but everytime i open the program the list i have is says "System.IO.StreamReader".
and when i close it gives me 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.
ok...i updated my code to this:
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);
}
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);
if (LBlist.Items.Count == 0)
{
btn_delete.Enabled = false;
}
}
catch
{
}
}
private void listbox_DoubleClick(object sender, EventArgs e)
{
wBrowser.Navigate(LBlist.SelectedIndex.ToString());
}
private void wBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tbx_website.Text = wBrowser.Url.ToString();
}
private void LBlist_SelectedIndexChanged(object sender, EventArgs e)
{
if (LBlist.Items.Count == 0)
{
btn_delete.Enabled = false;
}
}
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.Write(s_bmfile); sw_file.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Sam HobbsPosted Sep 27, 2011, 2:14 PM
Note that nearly always, you do not need to flush until after all data is written. In other words, you must flush before closing but the using statement will take care of closing too.
Jonathan CrispePosted Sep 27, 2011, 1:48 PM
my bookmarks.txt is empty..
Sam HobbsPosted Sep 26, 2011, 3:23 PM
Jonathan CrispePosted Sep 26, 2011, 12:49 PM
whatever is on the listbox has to save on txt.file(bookmarks.txt) when form is closed.
i have this:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
StreamWriter sw_file;
string s_save = LBlist.Items.ToString();
sw_file = new StreamWriter("bookmarks.txt");
sw_file.Write(s_save);
}
Help me, pls...
Jonathan CrispePosted Sep 24, 2011, 11:37 PM
Another Question: my double click is not working properly. its gives numbers when i double click the selectedindex from listbox....or how to openly directly to webbrowser when you double click the selectediindex.
Sam HobbsPosted Sep 24, 2011, 11:45 AM
That way it will be enabled when there are items. You also should enable the button after adding an item. Also, whenever your program uses LBlist.SelectedIndex, it should check if it is -1.