Windows Form Catalogue

Problems with certain functions of a Windows Form Database

Page 1 of 1

7 Replies - 1359 Views - Last Post: 08 December 2009 - 08:14 PM Rate Topic: -----

#1 crazylegs67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 09

Windows Form Catalogue

Post icon  Posted 08 December 2009 - 10:03 AM

Hi all

This is my first post so please excuse any errors or if I don't follow the correct protocol. I only started learning C# a month or so ago, so I have a very basic understanding atm.

I am building a basic database to retain a list of IT Equipment.

I have created an Array List for the data & I have linked it to an XML File. I have added basic buttons to the form, such as Prev, Next, Add & Remove and I have added some picture boxes so that they are displayed when the product is chosen.

This is the coding I have set up so far

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.Collections;
using System.Xml;

namespace Final
{
	public partial class Form1 : Form
	{
		private ArrayList itEquipment;
		private Equipment equipment;
		private XmlDocument equipmentDoc;
		private XmlNode rootNode;
		private XmlNodeList nodeList;

		private int index = 0;

		public Form1()
		{
			InitializeComponent();

			//TODO - Addition of Code

			itEquipment = new ArrayList();
			equipmentDoc = new XmlDocument();
			equipmentDoc.Load(@"equipment.xml");

			rootNode = equipmentDoc.DocumentElement; //set current Node to root
			nodeList = rootNode.SelectNodes("*");

			string name = "";
			string refNo = "";
			string installNotes = "";
			string maintCheck = "";
			string details = "";

			foreach (XmlNode node in nodeList)
			{
				foreach (XmlNode equipmentnode in node)
				{
					if (equipmentnode.Name == "name")
					{
						name = equipmentnode.InnerText;
					}
					else
					{
						if (equipmentnode.Name == "refNo")
						{
							refNo = equipmentnode.InnerText;
						}

						if (equipmentnode.Name == "installNotes")
						{
							installNotes = equipmentnode.InnerText;
						}
						else
						{
							if (equipmentnode.Name == "maintCheck")
							{
								maintCheck = equipmentnode.InnerText;
								equipment = new Equipment(name, refNo, installNotes, maintCheck);
								itEquipment.Add(equipment);
							}//end if
						}
					}
				}

			}

			//populate listBox

			foreach (Equipment equipment in itEquipment)
			{
				details = equipment.getName();
				details = details + "," + equipment.getmaintCheck();

				equipmentListBox.Items.Insert(index, equipment.getName());
				index++;

				picBoxControl();
																				
			}
		}

		private void equipmentListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			textBox1.Text = ((Equipment)itEquipment[equipmentListBox.SelectedIndex]).getName();
			textBox2.Text = ((Equipment)itEquipment[equipmentListBox.SelectedIndex]).getrefNo();
			textBox3.Text = ((Equipment)itEquipment[equipmentListBox.SelectedIndex]).getinstallNotes();
			textBox4.Text = ((Equipment)itEquipment[equipmentListBox.SelectedIndex]).getmaintCheck();

			picBoxControl();

					  
		}

		private void button4_Click(object sender, EventArgs e)
		{ 
			//delete item

			
			itEquipment.RemoveAt(equipmentListBox.SelectedIndex);
			itEquipment.RemoveAt(index);


			picBoxControl();

			//  itEquipment.RemoveAt(equipmentListBox.SelectedIndex);
			//   equipmentListBox.Items.RemoveAt(equipmentListBox.SelectedIndex);
		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		{
			//prev 
			index = index - 1;
			if (index < 0) index = itEquipment.Count - 1;

			textBox1.Text = ((Equipment)itEquipment[index]).getName();
			textBox2.Text = ((Equipment)itEquipment[index]).getrefNo();
			textBox3.Text = ((Equipment)itEquipment[index]).getinstallNotes();
			textBox4.Text = ((Equipment)itEquipment[index]).getmaintCheck();

			picBoxControl();
			
		}

		private void button2_Click(object sender, EventArgs e)
		{
			//next 
			index = index + 1;
			if (index >= itEquipment.Count) index = 0;

			textBox1.Text = ((Equipment)itEquipment[index]).getName();
			textBox2.Text = ((Equipment)itEquipment[index]).getrefNo();
			textBox3.Text = ((Equipment)itEquipment[index]).getinstallNotes();
			textBox4.Text = ((Equipment)itEquipment[index]).getmaintCheck();

			picBoxControl();

		}

		private void button3_Click(object sender, EventArgs e)
		{ 
			// Add to Database

			string name = "";
			string refNo = "";
			string installNotes = "";
			string maintCheck = "";

			name = textBox1.Text;
			refNo = textBox2.Text;
			installNotes = textBox3.Text;
			maintCheck = textBox4.Text;

			equipmentListBox.Items.Insert(itEquipment.Count, name);
			equipment = new Equipment(name, refNo, installNotes, maintCheck);
			itEquipment.Add(equipment);

			
		}

		public void picBoxControl()  // change images. 
		
		{
			if (index == 0) 
				
			{
				pictureBox1.Visible = true;
				pictureBox2.Visible = false;
				pictureBox3.Visible = false;
				pictureBox4.Visible = false;
				pictureBox5.Visible = false;
				
			}

			 if (index == 1)
			   
				{
					pictureBox1.Visible = false;
					pictureBox2.Visible = true;
					pictureBox3.Visible = false;
					pictureBox4.Visible = false;
					pictureBox5.Visible = false;
				}

				 if (index == 2) 

			{
				pictureBox1.Visible = false;
				pictureBox2.Visible = false;
				pictureBox3.Visible = true;
				pictureBox4.Visible = false;
				pictureBox5.Visible = false;
			}
				 if (index == 3)
				 {
					 pictureBox1.Visible = false;
					 pictureBox2.Visible = false;
					 pictureBox3.Visible = false;
					 pictureBox4.Visible = true;
					 pictureBox5.Visible = false;
				 }

				 if (index == 4)
				 {
					 pictureBox1.Visible = false;
					 pictureBox2.Visible = false;
					 pictureBox3.Visible = false;
					 pictureBox4.Visible = false;
					 pictureBox5.Visible = true;
					 
				 }
		 }

	   
  }
}


However, I am having the following problems :

1 ) When I delete a record, it continually get a "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" error pointing to the following code.

itEquipment.RemoveAt(equipmentListBox.SelectedIndex);
itEquipment.RemoveAt(index);

I think my count is set up incorrectly so when I delete a record, the count doesn't update?

It seems to work when I use the List Box & select the product from there but I get the error when I use prev & next to navigate then delete a record.

2 ) I would like to add a search button into the form, but I do not know how to link the code to either the XML File or the List Box.

3 ) I am using individual picture boxes for every product. Is there a more efficient way to do this? Such as coding that changes the picture depending on the product? And the picture only changes when I use the Prev & Next buttons. When I highlight something on the list box, the product details change but the picture doesn't

4 ) When I add a record, it only does this temporarily. Is there some way to add a record ( and a picture? ) directly to the XML Document?


Apologies if I am unclear in what my problems are or if I am asking for way too much.

I am new to C# and have spent a lot of time getting to where I am, but I have hit a bit of a brick wall now. I would prefer to keep everything as it stands ( naming convention, XML File, coding etc ) but I appreciate that there may be more efficient ways to do this

Many thanks and any assistance is greatly appreciated

CL

Is This A Good Question/Topic? 0
  • +

Replies To: Windows Form Catalogue

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 10:38 AM

I made few improvements to your code, but it's not finished yet, so lets look at that first and if you have any question about it, feel free to ask.

using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace Final
{
	public partial class Form1 : Form
	{
		private IList<Equipment> itEquipment = null;

		private XmlDocument document = null;
		private XmlNode rootNode = null;
		private XmlNodeList nodeList = null;


		public Form1()
		{
			InitializeComponent();
			InitCustomComponents();
			ReadDocument();
		}


		private void InitCustomComponents()
		{
			document = new XmlDocument();
			document.Load("mydoc.xml");
			rootNode = document.DocumentElement;
			nodeList = rootNode.SelectNodes("*");
		}


		private void ReadDocument()
		{
			foreach (XmlNode node in nodeList)
			{
				Equipment equipment = new Equipment();

				foreach (XmlNode eqNode in node)
				{
					switch (eqNode.Name)
					{
						case "name":
							equipment.Name = eqNode.InnerText;
							break;
						case "refNo":
							equipment.RefNumber = eqNode.InnerText;
							break;
						case "installNotes":
							equipment.InstallNotes = eqNode.InnerText;
							break;
						case "maintCheck":
							equipment.MaintCheck = eqNode.InnerText;
							break;
						default:
							break;
					}
				}

				AddEquipment(equipment);
			}
		}


		private void AddEquipment(Equipment eq)
		{
			if (itEquipment != null)
			{
				itEquipment.Add(eq);
			}
			else
			{
				itEquipment = new List<Equipment>();
				AddEquipment(eq);
			}
		}
	}

	public class Equipment
	{
		public string Name { get; set; }
		public string RefNumber { get; set; }
		public string InstallNotes { get; set; }
		public string MaintCheck { get; set; }

		public Equipment() { }
		public Equipment(string name, string refNum, string installNotes, string maintCheck)
			: this()
		{
			Name = name;
			RefNumber = refNum;
			InstallNotes = installNotes;
			MaintCheck = maintCheck;
		}
	}
}



This post has been edited by FlashM: 08 December 2009 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 crazylegs67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 11:17 AM

Here you go bud


Attached File  Equipment.xml (1.02K)
Number of downloads: 56
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 11:25 AM

Preview my previous post that I have just edited.
Was This Post Helpful? 0
  • +
  • -

#5 crazylegs67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 11:43 AM

I think I can follow what you are doing, but as I said before, I'm pretty new to all of this.

I would need to create a fresh XML File named accordingly and rather than using an Array List, you are using an IList?

Would I need to create a new Class too?

*edit* I added your code to a new Windows Form & got the following? I suspect that this is expected as the coding isn't finished?

Error 1 Form1.Dispose(bool)': no suitable method found to override

pointing to

protected override void Dispose(bool disposing)

This post has been edited by crazylegs67: 08 December 2009 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 01:16 PM

What do you mean by that you will have to create a fresh XML File?
I'm using IList which is very similar to ArrayList, difference is that you can add to an IList only elements of certain type which is defined when creating an IList variable:

IList<Equipment> list = new List<Equipment>();

And I think it's a bit faster than ArrayList, but don't take that for granted as I'm not 100% sure about that.

You don't have to create a new Equipment class. You already have it, you just didn't post the class code inside your post. I Added my Equipment class below Form1 class just to make thing more clear to people reading this post. Equipment class can also be in a seperate file, like you have done in your solution.

I don't know why you're getting this error, I couldn't reproduce it at my machine, that's why I've attached my solution in a rar file. It should work out of the box.

Attached File(s)

  • Attached File  Final.zip (37.48K)
    Number of downloads: 66

This post has been edited by FlashM: 08 December 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 02:00 PM

Ok, here is the final solution. I tried to demonstrate you the way things should be done. Application still missing a lot of errors checking. I also attached solution file.
Please tell me what does the method named picBoxControl() anyway? What is its purpose?

using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace Final
{
	public partial class Form1 : Form
	{
		private IList<Equipment> itEquipment = null;
		private PictureBox[] pictureBoxes = null;

		private XmlDocument document = null;
		private XmlNode rootNode = null;
		private XmlNodeList nodeList = null;


		public Form1()
		{
			InitializeComponent();
			InitCustomComponents();
			ReadDocument();
			PopulateListbox();
		}


		//Initialize some custom components of our own.
		private void InitCustomComponents()
		{
			pictureBoxes = new PictureBox[5];
			pictureBoxes[0] = pictureBox1;
			pictureBoxes[1] = pictureBox2;
			pictureBoxes[2] = pictureBox3;
			pictureBoxes[3] = pictureBox4;
			pictureBoxes[4] = pictureBox5;

			document = new XmlDocument();
			document.Load("Equipment.xml");
			rootNode = document.DocumentElement;
			nodeList = rootNode.SelectNodes("*");
		}


		//Read our XML document and store each equipment found into Equipment IList.
		private void ReadDocument()
		{
			foreach (XmlNode node in nodeList)
			{
				Equipment equipment = new Equipment();

				foreach (XmlNode eqNode in node)
				{
					switch (eqNode.Name)
					{
						case "name":
							equipment.Name = eqNode.InnerText;
							break;
						case "refNo":
							equipment.RefNumber = eqNode.InnerText;
							break;
						case "installNotes":
							equipment.InstallNotes = eqNode.InnerText;
							break;
						case "maintCheck":
							equipment.MaintCheck = eqNode.InnerText;
							break;
						default:
							break;
					}
				}

				AddEquipment(equipment);
			}
		}


		//Method provides safe way for adding new Equipment object into our Equipment IList
		private void AddEquipment(Equipment eq)
		{
			if (itEquipment != null)
			{
				itEquipment.Add(eq);
			}
			else
			{
				itEquipment = new List<Equipment>();
				AddEquipment(eq);
			}
		}


		//Method populates our Listbox with names of equipment
		private void PopulateListbox()
		{
			foreach (Equipment eq in itEquipment)
			{
				equipmentListBox.Items.Add(eq.Name);
			}
		}


		//Refresh textboxes with newly selected equipment object
		private void equipmentListBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int idx = equipmentListBox.SelectedIndex;

			if (idx >= 0)
			{
				Equipment eq = itEquipment[idx];
				textBox1.Text = eq.Name;
				textBox2.Text = eq.RefNumber;
				textBox3.Text = eq.InstallNotes;
				textBox4.Text = eq.MaintCheck;
			}
		}


		//Remove selected equipment from Listbox and Equipment IList
		private void btnRemoveEquipment_Click(object sender, System.EventArgs e)
		{
			if (equipmentListBox.SelectedIndex >= 0)
			{
				int idx = equipmentListBox.SelectedIndex;
				itEquipment.RemoveAt(idx);
				equipmentListBox.SelectedIndex = -1;
				equipmentListBox.Items.RemoveAt(idx);
				ClearTextboxes();
			}
		}


		//Reset/Clear textboxes method
		private void ClearTextboxes()
		{
			textBox1.Clear();
			textBox2.Clear();
			textBox3.Clear();
			textBox4.Clear();
		}


		//Reset/Clear textboxes calling ClearTextboxes() method
		private void btnClear_Click(object sender, System.EventArgs e)
		{
			ClearTextboxes();
		}


		//Add new equipment into Listbox and Equipment IList
		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			Equipment eq = new Equipment();
			eq.Name = textBox1.Text;
			eq.RefNumber = textBox2.Text;
			eq.InstallNotes = textBox3.Text;
			eq.MaintCheck = textBox4.Text;

			AddEquipment(eq);
			equipmentListBox.Items.Add(eq.Name);
		}


		//I really don't understant this method of yours. But anyway, 
		//I just made few changes, result should be the same.
		private void PictureBoxControl(int index)
		{
			for (int i = 0; i < pictureBoxes.Length; i++)
			{
				if (i == index)
					pictureBoxes[i].Visible = true;
				else
					pictureBoxes[i].Visible = false;
			}
		}
	}

	public class Equipment
	{
		public string Name { get; set; }
		public string RefNumber { get; set; }
		public string InstallNotes { get; set; }
		public string MaintCheck { get; set; }

		public Equipment() { }
		public Equipment(string name, string refNum, string installNotes, string maintCheck) : this()
		{
			Name = name;
			RefNumber = refNum;
			InstallNotes = installNotes;
			MaintCheck = maintCheck;
		}
	}
}


Attached File(s)

  • Attached File  Final.zip (46.21K)
    Number of downloads: 59

This post has been edited by FlashM: 08 December 2009 - 02:01 PM

Was This Post Helpful? 1
  • +
  • -

#8 crazylegs67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-December 09

Re: Windows Form Catalogue

Posted 08 December 2009 - 08:14 PM

Thanks, that is exactly what I need and does the job

The only issue I am having now is with the images. They do not change when I highlight an item in the listbox. I have 5 picture boxes, each with an image linked to them

picBoxControl() was a method I set up to change the images when I used the Prev & Next buttons - linked to pictureBox1.Visible = false etc

I could get it to work with the Prev & Next buttons, but I cannot get it to work in your solution although I see that you have added a method & PictureBoxControl, but the picture boxes stay visible regardless.

I'll have a look at adding a search button function as well than can search the Reference Number or indeed anything contained in the ListBox/XML File

Thanks again for the help so far :) Hopefully I can get the image issue sorted

This post has been edited by crazylegs67: 08 December 2009 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1