Hej Udviklere, jeg vil lige høre om nogen havde lyst til at kigge min kildekode igennem til mit LinkBog og komme med kommentarer på hvordan jeg løser diverse problemer.
Med Venlig Hilsen
Martin A. Kristensen
Link.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Link
{
/**
* Private variabler
*/
private string name;
private string addr;
/**
* Constructor
* Opretter nyt Link objekt
*/
public Link(string newName, string newAddr)
{
name = newName;
addr = newAddr;
}
/**
* Property
* Get: returns name
* Set: set value to name
*/
public string Name
{
get { return name; }
set { name = value; }
}
/**
* Property
* Get: returns addr
* Set: set value to addr
*/
public string Addr
{
get { return addr; }
set { addr = value; }
}
}
}
Book.cs
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication
{
class Book
{
/**
* Private variabler
*/
private ArrayList Links;
private XmlDocument xmlDoc;
/**
* Constructer
* Opretter instanser nye instanser af ArrayList og XmlDocument
*/
public Book()
{
Links = new ArrayList();
xmlDoc = new XmlDocument();
}
/**
* returns the array which contains the links
*/
public ArrayList getArray()
{
return Links;
}
/**
* Add a new Link object to the link array
*/
public void LinkArrayAdd(Link newLink)
{
Links.Add(newLink);
}
/**
* Clears the link array
*/
public void LinkArrayClear()
{
Links.Clear();
}
/**
* Reads the inputfile (xmlfile) and adds the nodes to the array
* as a Link object
*/
public void ReadBook(string file)
{
xmlDoc.Load(file);
XmlNodeList myList = xmlDoc.SelectNodes("links/link");
for (int i = 0; i < myList.Count; i++)
{
XmlNode linkNode = (XmlNode)myList[i];
XmlNode nameNode = linkNode.SelectSingleNode("name");
XmlNode addrNode = linkNode.SelectSingleNode("addr");
Link newLink = new Link(nameNode.InnerText, addrNode.InnerText);
Links.Add(newLink);
}
}
/**
* Save the Link objects in the link array to a file (xmlfile)
*/
public void SaveBook(string file)
{
xmlDoc.LoadXml("<?xml version=\\"1.0\\" encoding=\\"utf-8\\" ?><links></links>");
foreach (Link myLink in Links)
{
XmlElement newcatalogentry = xmlDoc.CreateElement("link");
XmlElement firstelement = xmlDoc.CreateElement("name");
XmlElement secondelement = xmlDoc.CreateElement("addr");
firstelement.InnerText = myLink.Name;
secondelement.InnerText = myLink.Addr;
newcatalogentry.AppendChild(firstelement);
newcatalogentry.AppendChild(secondelement);
xmlDoc.DocumentElement.InsertAfter(newcatalogentry, xmlDoc.DocumentElement.LastChild);
}
xmlDoc.Save(file);
Links.Clear();
}
}
}
LinkBook.cs
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;
namespace ConsoleApplication
{
public partial class LinkBook : Form
{
/**
* Private variables
*/
// Singleton
private static LinkBook instance = null;
/**
* Constructer
*/
private LinkBook()
{
InitializeComponent();
}
/**
* Singleton Pattern
* Returns the instance of the class or create one
*/
public static LinkBook getInstance()
{
if (instance == null)
instance = new LinkBook();
return instance;
}
/**
* Adds a new item to the listview
*/
public void add(string name, string addr)
{
ListViewItem newItem = new ListViewItem(name);
newItem.SubItems.Add(addr);
listView1.Items.Add(newItem);
listView1.Refresh();
}
/**
* Closes the application
*/
private void afslutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
/**
* Shows the OpenDialog
*/
private void åbenToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
/**
* Shows the save dialog
*/
private void gemToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
}
/**
* Removes one or more items from the listview
*/
private void fjernToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem myItem in listView1.SelectedItems)
listView1.Items.Remove(myItem);
listView1.Refresh();
}
/**
* Opens and shows the addForm
*/
private void tilføjToolStripMenuItem_Click(object sender, EventArgs e)
{
addLink nyLink = new addLink();
nyLink.ShowDialog();
}
/**
* Read the xmlfile and adds the link objects to the listview through the
* Book class
*/
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
listView1.Items.Clear();
Book myBook = new Book();
myBook.ReadBook(openFileDialog1.FileName);
foreach (Link mylink in myBook.getArray())
{
ListViewItem newItem = new ListViewItem(mylink.Name);
newItem.SubItems.Add(mylink.Addr);
listView1.Items.Add(newItem);
}
listView1.Refresh();
myBook.LinkArrayClear();
}
/**
* Saves the listviewitems to a xmlfile through the Book class
*/
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
Book myBook = new Book();
foreach (ListViewItem myItem in listView1.Items)
{
Link newLink = new Link(myItem.SubItems[0].Text, myItem.SubItems[1].Text);
myBook.LinkArrayAdd(newLink);
}
myBook.SaveBook(saveFileDialog1.FileName);
}
}
}
namespace ConsoleApplication
{
partial class LinkBook
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.tilføjToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fjernToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.filerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.åbenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.gemToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.afslutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.contextMenuStrip1.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.ContextMenuStrip = this.contextMenuStrip1;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(0, 24);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(441, 338);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Hjemmeside";
this.columnHeader1.Width = 150;
//
// columnHeader2
//
this.columnHeader2.Text = "Adresse";
this.columnHeader2.Width = 287;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tilføjToolStripMenuItem,
this.fjernToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(111, 48);
//
// tilføjToolStripMenuItem
//
this.tilføjToolStripMenuItem.Name = "tilføjToolStripMenuItem";
this.tilføjToolStripMenuItem.Size = new System.Drawing.Size(110, 22);
this.tilføjToolStripMenuItem.Text = "Tilføj";
this.tilføjToolStripMenuItem.Click += new System.EventHandler(this.tilføjToolStripMenuItem_Click);
//
// fjernToolStripMenuItem
//
this.fjernToolStripMenuItem.Name = "fjernToolStripMenuItem";
this.fjernToolStripMenuItem.Size = new System.Drawing.Size(110, 22);
this.fjernToolStripMenuItem.Text = "Fjern";
this.fjernToolStripMenuItem.Click += new System.EventHandler(this.fjernToolStripMenuItem_Click);
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.filerToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(441, 24);
this.menuStrip1.TabIndex = 1;
this.menuStrip1.Text = "menuStrip1";
//
// filerToolStripMenuItem
//
this.filerToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.åbenToolStripMenuItem,
this.gemToolStripMenuItem,
this.afslutToolStripMenuItem});
this.filerToolStripMenuItem.Name = "filerToolStripMenuItem";
this.filerToolStripMenuItem.Size = new System.Drawing.Size(39, 20);
this.filerToolStripMenuItem.Text = "Filer";
//
// åbenToolStripMenuItem
//
this.åbenToolStripMenuItem.Name = "åbenToolStripMenuItem";
this.åbenToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
this.åbenToolStripMenuItem.Text = "Åben";
this.åbenToolStripMenuItem.Click += new System.EventHandler(this.åbenToolStripMenuItem_Click);
//
// gemToolStripMenuItem
//
this.gemToolStripMenuItem.Name = "gemToolStripMenuItem";
this.gemToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
this.gemToolStripMenuItem.Text = "Gem";
this.gemToolStripMenuItem.Click += new System.EventHandler(this.gemToolStripMenuItem_Click);
//
// afslutToolStripMenuItem
//
this.afslutToolStripMenuItem.Name = "afslutToolStripMenuItem";
this.afslutToolStripMenuItem.Size = new System.Drawing.Size(113, 22);
this.afslutToolStripMenuItem.Text = "Afslut";
this.afslutToolStripMenuItem.Click += new System.EventHandler(this.afslutToolStripMenuItem_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.saveFileDialog1_FileOk);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
// LinkBook
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(441, 362);
this.Controls.Add(this.listView1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "LinkBook";
this.Text = "LinkBook";
this.contextMenuStrip1.ResumeLayout(false);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem filerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem åbenToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem gemToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem afslutToolStripMenuItem;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem tilføjToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fjernToolStripMenuItem;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
}
}
addForm.cs
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;
namespace ConsoleApplication
{
public partial class addLink : Form
{
public addLink()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
LinkBook myBook = LinkBook.getInstance();
myBook.add(textBox1.Text, textBox2.Text);
this.Close();
}
}
}
namespace ConsoleApplication
{
partial class addLink
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Hjemmeside navn";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(45, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Adresse";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(109, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(149, 20);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(109, 32);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(149, 20);
this.textBox2.TabIndex = 3;
//
// button1
//
this.button1.Location = new System.Drawing.Point(109, 58);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(68, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Tilføj";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(183, 58);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 5;
this.button2.Text = "Annuller";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// addLink
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(273, 91);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "addLink";
this.Text = "Tilføj link";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}