c# Favorites

A

andrewq2

I have a post in fourms.micorsoft.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<Favorite> theFavorites = new List<Favorite>();

//****************** String Definition ***********************//

public List<Favorite> TheFavoritesCollection

{

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);

}

}

}
 
R

raylopez99

I have a post in fourms.micorsoft.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;
[/QUOTE]

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
 
A

andrewq2

I have a post in fourms.micorsoft.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;[/QUOTE]

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[/QUOTE]

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">
<TheFavoritesCollection />
<TheDisplayName />
<TheURL />
</Favorite>
this it all creates it dosent save the url's or title's
this is my updated 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<Favorite> theFavorites = new
List<Favorite>();

//****************** String Definition
***********************//
public List<Favorite> TheFavoritesCollection
{
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)
{
string text = webBrowser1.DocumentTitle.ToString();
string url = webBrowser1.Url.ToString();
Favorite theFavToAdd = new Favorite(text, url);

theFavToAdd.DoAddFavorite(theFavToAdd);

 
this.favoriteToolStripMenuItem.DropDown.Items.Add(theFavToAdd.TheDisplayName.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(e.ClickedItem.ToolTipText.ToString());
}
private void webBrowser1_StatusTextChanged(object sender,
EventArgs e)
{
toolStripStatusLabel3.Text = webBrowser1.StatusText;
}
}
}
 
B

Ben Voigt [C++ MVP]

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
 
R

raylopez99

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
 
J

Jon Skeet [C# MVP]

raylopez99 said:
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...

http://microsoft.com/express
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top