Connecting Tech Pros Worldwide Help | Site Map

To save or not to save

Newbie
 
Join Date: Oct 2006
Posts: 1
#1: Oct 21 '06
Hi, ive just finished building more "tabbed web browser with favourites menu" but i cant ge the favourites menu to work properly. THe add functions are ok the problem is I cant ge t the favourites to save / load.

Expand|Select|Wrap|Line Numbers
  1.  #region Using directives 
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using System.Security.Policy;
  11. using System.Text;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14. using System.IO;
  15.  
  16. #endregion
  17.  
  18. namespace MyOwnTabbedBrowser
  19. {
  20. public partial class BrowserExplorer : Form
  21. {
  22. int current_tab_count = 0;
  23.  
  24. private string theURL = String.Empty;
  25.  
  26. ArrayList tabpages = new ArrayList();
  27.  
  28. ArrayList webpages = new ArrayList();
  29.  
  30. private Favourite theFavourites = new Favourite();
  31.  
  32. public BrowserExplorer()
  33. {
  34. InitializeComponent();
  35. tabControl1.TabPages.Clear();
  36. Create_a_new_tab();
  37. WebBrowser webpage = GetCurrentWebBrowser();
  38. webpage.GoHome();
  39. WebBrowser thiswebpage = GetCurrentWebBrowser();
  40. }
  41.  
  42. public string TheURL
  43. {
  44. get
  45. {
  46. return comboBoxurl.Text;
  47. }
  48. set
  49. {
  50. this.theURL = value;
  51. }
  52.  
  53. }
  54.  
  55. private void BrowserExplorer_FormClosing(object sender, FormClosingEventArgs e)
  56. {
  57. this.theFavourites.DoSaveFavourites();
  58. }
  59.  
  60. public void BrowserExplorer_Load(object sender, EventArgs e)
  61. {
  62. this.tsBack.Enabled = false;
  63. this.tsbForward.Enabled = false;
  64. this.theFavourites = Favourite.DoLoadFavourites();
  65. foreach (Favourite currentItem in this.theFavourites.TheFavouritesCollection)
  66. {
  67. this.theFavouriteMenuToolStripItem.DropDown.Items.Add(currentItem.TheDisplayName);
  68. this.theFavouriteMenuToolStripItem.DropDown.Items[this.theFavouriteMenuToolStripItem.DropDown.Items.Count - 1].ToolTipText = currentItem.TheURL;
  69. }
  70. }
  71.  
  72.  
  73. private void addNewFavouriteToolStripMenuItem_Click(object sender, EventArgs e)
  74. {
  75. WebBrowser thiswebpage = GetCurrentWebBrowser();
  76.  
  77. MessageBox.Show("About to add favourite, change the displaname and theUrl in code:");
  78. Favourite theNewFavourite = new Favourite((thiswebpage.Document.Title), (TheURL));
  79.  
  80. theNewFavourite.DoAddFavourite(theNewFavourite);
  81.  
  82. this.theFavouriteMenuToolStripItem.DropDown.Items.Add(theURL);
  83.  
  84. this.theFavouriteMenuToolStripItem.DropDown.Items[this.theFavouriteMenuToolStripItem.DropDown.Items.Count - 1].ToolTipText = theNewFavourite.TheURL;
  85. this.theFavouriteMenuToolStripItem.DropDown.Items[this.theFavouriteMenuToolStripItem.DropDown.Items.Count - 1].Text = theNewFavourite.TheDisplayName;
  86. this.theFavouriteMenuToolStripItem.DropDown.ItemClicked += new ToolStripItemClickedEventHandler(DropDown_ItemClicked);
  87. }
  88.  
  89. void DropDown_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  90. {
  91. WebBrowser thiswebpage = GetCurrentWebBrowser();
  92. thiswebpage.Navigate(e.ClickedItem.ToolTipText);
  93. }
  94.  
  95. private void theFavouriteMenuToolStripItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
  96. {
  97. WebBrowser thiswebpage = GetCurrentWebBrowser();
  98.  
  99. //thiswebpage.Navigate(TheURL);
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. public class Favourite
  107. {
  108. //****************** String Declaration **********************//
  109. private string theDisplayName = string.Empty;
  110. private string theURL = string.Empty;
  111. private List<Favourite> theFavourites = new List<Favourite>();
  112.  
  113. //****************** String Definition ***********************//
  114.  
  115. public List<Favourite> TheFavouritesCollection
  116. {
  117. get
  118. {
  119. return this.theFavourites;
  120. }
  121. }
  122.  
  123. public string TheDisplayName
  124. {
  125. get
  126. {
  127. return theDisplayName;
  128. }
  129. set
  130. {
  131. theDisplayName = value;
  132. }
  133. }
  134.  
  135. public string TheURL
  136. {
  137. get
  138. {
  139. return this.theURL;
  140. }
  141. set
  142. {
  143. this.theURL = value;
  144. }
  145.  
  146. }
  147.  
  148. public Favourite() { }
  149.  
  150. public Favourite(string displayName, string url)
  151. {
  152. theDisplayName = displayName;
  153. theURL = url;
  154. }
  155.  
  156. public int CompareTo(Favourite fav)
  157. {
  158. return TheDisplayName.CompareTo(TheDisplayName);
  159. }
  160. //**************Favourite Add/Load/Save Functions ****************//
  161. public void DoAddFavourite(Favourite theFavToAdd)
  162. {
  163. this.theFavourites.Add(theFavToAdd);
  164. }
  165. public static Favourite DoLoadFavourites()
  166. {
  167. if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + "\\favourites.xml"))
  168. {
  169. //deserialize Type[] theTypes = new Type[1];
  170. Type[] theTypes = new Type[1];
  171. theTypes[0] = typeof(List<Favourite>);
  172. XmlSerializer theSerializer = new XmlSerializer(typeof(Favourite), theTypes);
  173. System.IO.StreamReader theReader = new System.IO.StreamReader(System.Windows.Forms.Application.StartupPath + "\\favourites.xml");
  174. Favourite theFavourites = (Favourite)theSerializer.Deserialize(theReader);
  175. theReader.Close();
  176. return theFavourites;
  177. }
  178. else
  179. {
  180. return new Favourite();
  181. }
  182. }
  183. public void DoSaveFavourites()
  184. {
  185. //serialize Type[] theTypes = new Type[1];
  186. Type[] theTypes = new Type[1];
  187. theTypes[0] = typeof(List<Favourite>);
  188. XmlSerializer theSerializer = new XmlSerializer(typeof(Favourite), theTypes);
  189. System.IO.StreamWriter theWriter = new System.IO.StreamWriter(System.Windows.Forms.Application.StartupPath + "\\favourites.xml");
  190. theSerializer.Serialize(theWriter, this);
  191. theWriter.Close();
  192. }
  193.  
  194. internal void Add(Favourite theFavToAdd)
  195. {
  196. throw new Exception("The method or operation is not implemented.");
  197. }
  198. }
  199. }
  200.  
PLEASE HELP ME!!!!!!!!!
Reply