had good luck in google groups befor so i decieded on posting my
question here also.
Im trying to add a favorites function to my web browser. the problem
when i hit the add to favorites button it does nothing.
but on form close it creates the favorites.xml file. so i know the
problem is somewhere in the add to favorites function.
hears my code:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Resources;
- using Microsoft.Win32;
- using System.IO;
- namespace Webbrowser
- {
- public partial class Form1 : Form
- {
- private string theURL = String.Empty;
- private Favorite theFavorites = new Favorite();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_load(object sender, EventArgs e)
- {
- this.theFavorites = Favorite.DoLoadFavorites();
- this.favoriteToolStripMenuItem.DropDown.Items.Clear(); //clear the
- items that already exist if any
- foreach (Favorite currentItem in
- this.theFavorites.TheFavoritesCollection)
- {
- this.favoriteToolStripMenuItem.DropDown.Items.Add(currentItem.TheDisplayName);
- this.favoriteToolStripMenuItem.DropDown.Items[this.favoriteToolStripMenuItem.DropDown.Items.Count
- - 1].ToolTipText = currentItem.TheURL;
- }
- }
- public class Favorite
- {
- //****************** String Declaration **********************//
- private string theDisplayName = string.Empty;
- private string theURL = String.Empty;
- private List<FavoritetheFavorites = new List<Favorite>();
- //****************** String Definition ***********************//
- public List<FavoriteTheFavoritesCollection
- {
- get
- {
- return this.theFavorites;
- }
- }
- public string TheDisplayName
- {
- get
- {
- return theDisplayName;
- }
- set
- {
- theDisplayName = value;
- }
- }
- public string TheURL
- {
- get
- {
- return theURL;
- }
- set
- {
- theURL = value;
- }
- }
- public Favorite() { }
- public Favorite(string displayName, string url)
- {
- theDisplayName = displayName;
- theURL = url;
- }
- public int CompareTo(Favorite fav)
- {
- return TheDisplayName.CompareTo(TheDisplayName);
- }
- //**************Favourite Add/Load/Save Functions ****************//
- public void DoAddFavorite(Favorite theFavToAdd)
- {
- this.theFavorites.Add(theFavToAdd);
- }
- public static Favorite DoLoadFavorites()
- {
- if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath
- + "\\favorites.xml"))
- {
- //deserialize Type[] theTypes = new Type[1];
- Type[] theTypes = new Type[1];
- theTypes[0] = typeof(List<Favorite>);
- XmlSerializer theSerializer = new XmlSerializer(typeof(Favorite),
- theTypes);
- System.IO.StreamReader theReader = new
- System.IO.StreamReader(System.Windows.Forms.Application.StartupPath +
- "\\favorites.xml");
- Favorite theFavorites =
- (Favorite)theSerializer.Deserialize(theReader);
- theReader.Close();
- return theFavorites;
- }
- else
- {
- return new Favorite();
- }
- }
- public void DoSaveFavorites()
- {
- //serialize Type[] theTypes = new Type[1];
- Type[] theTypes = new Type[1];
- theTypes[0] = typeof(List<Favorite>);
- XmlSerializer theSerializer = new XmlSerializer(typeof(Favorite),
- theTypes);
- System.IO.StreamWriter theWriter = new
- System.IO.StreamWriter(System.Windows.Forms.Application.StartupPath +
- "\\favorites.xml");
- theSerializer.Serialize(theWriter, this);
- theWriter.Close();
- }
- }
- private void webBrowser1_DocumentTitleChanged(object sender, EventArgs
- e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- webBrowser1.Navigate(textBox1.Text);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- webBrowser1.GoBack();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- webBrowser1.GoForward();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- webBrowser1.Refresh();
- }
- private void printToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowPrintDialog();
- }
- private void printPreviewToolStripMenuItem_Click(object sender,
- EventArgs e)
- {
- webBrowser1.ShowPrintPreviewDialog();
- }
- private void refreshToolStripMenuItem_Click(object sender, EventArgs
- e)
- {
- webBrowser1.Refresh();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- webBrowser1.Stop();
- }
- private void toolStripStatusLabel3_Click(object sender, EventArgs e)
- {
- toolStripStatusLabel3.Text = webBrowser1.StatusText;
- }
- private void webBrowser1_ProgressChanged(object sender,
- WebBrowserProgressChangedEventArgs e)
- {
- toolStripProgressBar1.Value = (int)(((double)e.CurrentProgress /
- e.MaximumProgress) * 100);
- toolStripStatusLabel3.Text = webBrowser1.StatusText;
- }
- private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- MessageBox.Show(
- "Product Version : " + ProductVersion + "\n Copy Right 2008 DSAQ.net",
- "About");
- }
- private void theDSAQSiteToolStripMenuItem_Click(object sender,
- EventArgs e)
- {
- webBrowser1.Navigate("http://dsaq.net");
- }
- private void googleToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webBrowser1.Navigate("http://google.com");
- }
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowSaveAsDialog();
- }
- private void properitesToolStripMenuItem_Click(object sender,
- EventArgs e)
- {
- webBrowser1.ShowPropertiesDialog();
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Form1.ActiveForm.Close();
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string url;
- openFileDialog1.ShowDialog();
- url = openFileDialog1.FileName;
- webBrowser1.Navigate(url);
- }
- private void webBrowser1_DocumentCompleted(object sender,
- WebBrowserDocumentCompletedEventArgs e)
- {
- string title;
- textBox1.Text = webBrowser1.Url.ToString();
- title = openFileDialog1.SafeFileName.ToString();
- switch (title)
- {
- case "openFileDialog1":
- Form1.ActiveForm.Text = webBrowser1.DocumentTitle + " - DSAQ Web
- Browser".ToString();
- break;
- default:
- Form1.ActiveForm.Text = openFileDialog1.SafeFileName + " - DSAQ Web
- Browser".ToString();
- break;
- }
- }
- private void optionsToolStripMenuItem_Click(object sender, EventArgs
- e)
- {
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- this.theFavorites.DoSaveFavorites();
- }
- private void addToFavoritesToolStripMenuItem_Click(object sender,
- EventArgs e)
- {
- Favorite theFavToAdd = new
- Favorite(webBrowser1.DocumentTitle.ToString(),
- webBrowser1.Url.ToString());
- theFavToAdd.DoAddFavorite(theFavToAdd);
- this.favoriteToolStripMenuItem.DropDown.Items.Add(theFavToAdd.ToString());
- this.favoriteToolStripMenuItem.DropDown.Items[this.favoriteToolStripMenuItem.DropDown.Items.Count
- - 1].ToolTipText = theFavToAdd.TheURL.ToString();
- }
- private void favoriteToolStripMenuItem_DropDownItemClicked(object
- sender, ToolStripItemClickedEventArgs e)
- {
- webBrowser1.Navigate(theURL);
- }
- }
- }