c# Favorites

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrewq2

    c# Favorites

    I have a post in fourms.micorsof t.com but no one is responding, and i
    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:
    Code:
    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);
    
    }
    
    }
    
    }
  • raylopez99

    #2
    Re: c# Favorites

    On Aug 1, 12:20 pm, andrewq2 <andrewque...@g mail.comwrote:
    I have a post in fourms.micorsof t.com but no one is responding, and i
    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:
    [code]
    using System;
    >
    I would step through the code line by line--don't forget to jump over
    functions--using the Debugger of VS. It's probably something simple.
    What language are you using? This looks like ADO.NET to me.

    RL

    Comment

    • andrewq2

      #3
      Re: c# Favorites

      On Aug 1, 3:42 pm, raylopez99 <raylope...@yah oo.comwrote:
      On Aug 1, 12:20 pm, andrewq2 <andrewque...@g mail.comwrote:
      >
      I have a post in fourms.micorsof t.com but no one is responding, and i
      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:
      [code]
      using System;
      >
      I would step through the code line by line--don't forget to jump over
      functions--using the Debugger of VS.  It's probably something simple.
      What language are you using?  This looks like ADO.NET to me.
      >
      RL
      im using c# and i finaly got it to work almost execpt for the saving
      of the xml file it creates the file with the right format but with no
      data.
      for ex.
      <?xml version="1.0" encoding="utf-8"?>
      <Favorite xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
      <TheFavoritesCo llection />
      <TheDisplayNa me />
      <TheURL />
      </Favorite>
      this it all creates it dosent save the url's or title's
      this is my updated code.
      using System;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows. Forms;
      using System.Xml;
      using System.Xml.Seri alization;
      using System.Resource s;
      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()
      {
      InitializeCompo nent();
      }
      private void Form1_load(obje ct sender, EventArgs e)
      {
      this.theFavorit es = Favorite.DoLoad Favorites();
      this.favoriteTo olStripMenuItem .DropDown.Items .Clear(); //
      clear the items that already exist if any
      foreach (Favorite currentItem in
      this.theFavorit es.TheFavorites Collection)
      {

      this.favoriteTo olStripMenuItem .DropDown.Items .Add(currentIte m.TheDisplayNam e);

      this.favoriteTo olStripMenuItem .DropDown.Items[this.favoriteTo olStripMenuItem .DropDown.Items .Count
      - 1].ToolTipText = currentItem.The URL;
      }

      }
      public class Favorite
      {
      //*************** *** String Declaration
      *************** *******//
      private string theDisplayName = string.Empty;
      private string theURL = String.Empty;
      private List<Favoriteth eFavorites = new
      List<Favorite>( );

      //*************** *** String Definition
      *************** ********//
      public List<FavoriteTh eFavoritesColle ction
      {
      get
      {
      return this.theFavorit es;
      }
      }

      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(Favor ite fav)
      {
      return TheDisplayName. CompareTo(TheDi splayName);
      }
      //**************F avourite Add/Load/Save Functions
      *************** *//
      public void DoAddFavorite(F avorite theFavToAdd)
      {
      this.theFavorit es.Add(theFavTo Add);
      }
      public static Favorite DoLoadFavorites ()
      {
      if
      (System.IO.File .Exists(System. Windows.Forms.A pplication.Star tupPath +
      "\\favorites.xm l"))
      {
      //deserialize Type[] theTypes = new Type[1];
      Type[] theTypes = new Type[1];
      theTypes[0] = typeof(List<Fav orite>);
      XmlSerializer theSerializer = new
      XmlSerializer(t ypeof(Favorite) , theTypes);
      System.IO.Strea mReader theReader = new
      System.IO.Strea mReader(System. Windows.Forms.A pplication.Star tupPath +
      "\\favorites.xm l");
      Favorite theFavorites =
      (Favorite)theSe rializer.Deseri alize(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<Fav orite>);
      XmlSerializer theSerializer = new
      XmlSerializer(t ypeof(Favorite) , theTypes);
      System.IO.Strea mWriter theWriter = new
      System.IO.Strea mWriter(System. Windows.Forms.A pplication.Star tupPath +
      "\\favorites.xm l");

      theSerializer.S erialize(theWri ter, this);
      theWriter.Close ();
      }
      }
      private void webBrowser1_Doc umentTitleChang ed(object sender,
      EventArgs e)
      {

      }
      private void button1_Click(o bject sender, EventArgs e)
      {
      webBrowser1.Nav igate(textBox1. Text);

      }

      private void button2_Click(o bject sender, EventArgs e)
      {
      webBrowser1.GoB ack();

      }

      private void button3_Click(o bject sender, EventArgs e)
      {
      webBrowser1.GoF orward();

      }

      private void button4_Click(o bject sender, EventArgs e)
      {
      webBrowser1.Ref resh();
      }

      private void printToolStripM enuItem_Click(o bject sender,
      EventArgs e)
      {
      webBrowser1.Sho wPrintDialog();
      }

      private void printPreviewToo lStripMenuItem_ Click(object
      sender, EventArgs e)
      {
      webBrowser1.Sho wPrintPreviewDi alog();
      }

      private void refreshToolStri pMenuItem_Click (object sender,
      EventArgs e)
      {
      webBrowser1.Ref resh();
      }

      private void button5_Click(o bject sender, EventArgs e)
      {
      webBrowser1.Sto p();
      }
      private void toolStripStatus Label3_Click(ob ject sender,
      EventArgs e)
      {
      toolStripStatus Label3.Text = webBrowser1.Sta tusText;
      }
      private void webBrowser1_Pro gressChanged(ob ject sender,
      WebBrowserProgr essChangedEvent Args e)
      {
      toolStripProgre ssBar1.Value = (int)
      (((double)e.Cur rentProgress / e.MaximumProgre ss) * 100);
      //toolStripStatus Label3.Text = webBrowser1.Sta tusText;

      }

      private void aboutToolStripM enuItem_Click(o bject sender,
      EventArgs e)
      {
      MessageBox.Show (
      "Product Version : " + ProductVersion + "\n Copy Right
      2008 DSAQ.net", "About");
      }

      private void theDSAQSiteTool StripMenuItem_C lick(object sender,
      EventArgs e)
      {
      webBrowser1.Nav igate("http://dsaq.net");
      }

      private void googleToolStrip MenuItem_Click( object sender,
      EventArgs e)
      {
      webBrowser1.Nav igate("http://google.com");
      }

      private void saveAsToolStrip MenuItem_Click( object sender,
      EventArgs e)
      {
      webBrowser1.Sho wSaveAsDialog() ;
      }

      private void properitesToolS tripMenuItem_Cl ick(object sender,
      EventArgs e)
      {
      webBrowser1.Sho wPropertiesDial og();
      }

      private void exitToolStripMe nuItem_Click(ob ject sender,
      EventArgs e)
      {
      Form1.ActiveFor m.Close();
      }

      private void openToolStripMe nuItem_Click(ob ject sender,
      EventArgs e)
      {
      string url;
      openFileDialog1 .ShowDialog();
      url = openFileDialog1 .FileName;
      webBrowser1.Nav igate(url);
      }

      private void webBrowser1_Doc umentCompleted( object sender,
      WebBrowserDocum entCompletedEve ntArgs e)
      {
      string title;
      textBox1.Text = webBrowser1.Url .ToString();
      title = openFileDialog1 .SafeFileName.T oString();
      switch (title)
      {
      case "openFileDialog 1":
      Form1.ActiveFor m.Text = webBrowser1.Doc umentTitle
      + " - DSAQ Web Browser".ToStri ng();
      break;
      default:
      Form1.ActiveFor m.Text =
      openFileDialog1 .SafeFileName + " - DSAQ Web Browser".ToStri ng();
      break;
      }
      }

      private void optionsToolStri pMenuItem_Click (object sender,
      EventArgs e)
      {
      }
      private void Form1_FormClosi ng(object sender,
      FormClosingEven tArgs e)
      {
      this.theFavorit es.DoSaveFavori tes();
      }

      private void addToFavoritesT oolStripMenuIte m_Click(object
      sender, EventArgs e)
      {
      string text = webBrowser1.Doc umentTitle.ToSt ring();
      string url = webBrowser1.Url .ToString();
      Favorite theFavToAdd = new Favorite(text, url);

      theFavToAdd.DoA ddFavorite(theF avToAdd);


      this.favoriteTo olStripMenuItem .DropDown.Items .Add(theFavToAd d.TheDisplayNam e.ToString());


      this.favoriteTo olStripMenuItem .DropDown.Items[this.favoriteTo olStripMenuItem .DropDown.Items .Count
      - 1].ToolTipText = theFavToAdd.The URL.ToString();
      }
      private void
      favoriteToolStr ipMenuItem_Drop DownItemClicked (object sender,
      ToolStripItemCl ickedEventArgs e)
      {

      webBrowser1.Nav igate(e.Clicked Item.ToolTipTex t.ToString());
      }
      private void webBrowser1_Sta tusTextChanged( object sender,
      EventArgs e)
      {
      toolStripStatus Label3.Text = webBrowser1.Sta tusText;
      }
      }
      }

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: c# Favorites

        I would step through the code line by line--don't forget to jump over
        functions--using the Debugger of VS. It's probably something simple.
        What language are you using? This looks like ADO.NET to me.
        ADO isn't a language, and by the using statement for LINQ, it has to be C#
        v3
        >
        RL

        Comment

        • raylopez99

          #5
          Re: c# Favorites

          On Aug 2, 3:37 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
          I would step through the code line by line--don't forget to jump over
          functions--using the Debugger of VS.  It's probably something simple.
          What language are you using?  This looks like ADO.NET to me.
          >
          ADO isn't a language, and by the using statement for LINQ, it has to be C#
          v3
          Right you are. That LINQ using statement trips me up every time I
          compile a new class, since I'm using C# 3.0 (no pun intended) but from
          Visual Studio 2005, and VS 2005 doesn't recognize LINQ. I'll have to
          upgrade to the Professional version one of these days...eBay probably
          has a Academic version for a hundred dollars or so.

          RL

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: c# Favorites

            raylopez99 <raylopez99@yah oo.comwrote:
            On Aug 2, 3:37 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
            I would step through the code line by line--don't forget to jump over
            functions--using the Debugger of VS.  It's probably something simple.
            What language are you using?  This looks like ADO.NET to me.
            ADO isn't a language, and by the using statement for LINQ, it has to beC#
            v3
            Right you are. That LINQ using statement trips me up every time I
            compile a new class, since I'm using C# 3.0 (no pun intended) but from
            Visual Studio 2005, and VS 2005 doesn't recognize LINQ. I'll have to
            upgrade to the Professional version one of these days...eBay probably
            has a Academic version for a hundred dollars or so.
            Or just download the free Express edition...

            Visual Studio dev tools & services make app development easy for any developer, on any platform & language. Develop with our code editor or IDE anywhere for free.


            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            Working...