C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2008, 02:43 PM   #1
Registered User
 
Join Date: Apr 2008
Posts: 58
Storing labels in a list?

So I have been having quite some trouble finding out why my project wasn't loading the proper text into some dynamically created labels...I talked to a programming friend, and he suggested storing my labels into a list, but I'm in an intro to programming course, and can't find a decent tutorial or explanation of how to implement this into my code.

Here is my program so far:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Influences
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int y = (Form1.ActiveForm.Height / 2);
            int x = (Form1.ActiveForm.Width / 2);
            XmlDocument XML = new XmlDocument();
            XML.Load("Influences.xml");
            FileStream fS = new FileStream("Influences.XML", FileMode.Open);
            XmlTextReader xml = new XmlTextReader(fS);
            //
            //Load initial centered label
            //
            Label myLabel = new Label(); 
            string name = "Me";
            myLabel.Text = name;
            myLabel.Height = 60;
            myLabel.Width = 60;
            myLabel.Location = new System.Drawing.Point(x - (myLabel.Width / 2), y - (myLabel.Height / 2));
            myLabel.BackColor = System.Drawing.Color.Transparent;
            myLabel.ForeColor = System.Drawing.Color.Blue;
            myLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            myLabel.Click += new System.EventHandler(this.myLabel_Click);
            this.Controls.Add(myLabel);
            fS.Close();
        }


        private void myLabel_Click(object sender, EventArgs e)
        {

            FileStream fS = new FileStream("Influences.XML", FileMode.Open);
            XmlTextReader xml = new XmlTextReader(fS);
            int N = 0;
            int xlabeltop = 0;
            int ylabeltop = 0;
            int x = Form1.ActiveForm.Width / 2;
            int y = Form1.ActiveForm.Height / 2;
            while (xml.Read())
            {
                if (xml.NodeType.ToString() == "Element")
                {
                    if (xml.Name == "ring")
                    {
                        N = Convert.ToInt32(xml.GetAttribute("count"));
                    }
                    if (xml.Name == "name1")
                    {
                        
                        for(int i=0; i < N; i++)
                        {
                            //
                            //Label
                            //
                            Label lbl = new Label();
                            int r = lbl.Width;
                            lbl.Text = xml.GetAttribute("id");
                            lbl.Height = 60;
                            lbl.Width = 60;
                            double xcenterlabel = x + r * (Math.Cos(2*Math.PI * ((double)i) / (double)N));
                            double ycenterlabel = y + r * (Math.Sin(2*Math.PI * ((double)i) / (double)N));
                            xlabeltop = (int)xcenterlabel - (lbl.Width / 2);
                            ylabeltop = (int)ycenterlabel - (lbl.Height / 2);
                            lbl.Location = new System.Drawing.Point(xlabeltop, ylabeltop);
                            lbl.ForeColor = System.Drawing.Color.White;
                            lbl.BackColor = System.Drawing.Color.Transparent;
                            lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                            lbl.Click += new System.EventHandler(this.lbl_Click);
                            this.Controls.Add(lbl);
                        }
                    }
                }
            }
            fS.Close();
        }


        private void lbl_Click(object sender, EventArgs e)
        {
            this.Location = new System.Drawing.Point(10,10);
        }
    }
}
Here is my XML:
Code:
<subject>
	<names>
		<ring count="4">
			<name1 id="Ghandi"></name1>
			<name1 id="Bob"></name1>
			<name1 id="Jerry"></name1>
			<name1 id="Tony"></name1>
		</ring>
	</names>
</subject>
When you run the program (windows form application), it creates the proper amount of labels based on the XML element named "count". However, the text that populates the labels is the same for each one. They all read "Ghandi".

I know that I need to somehow make the labels "unique". But I have no clue how I could accomplish this...

My friend said something about the labels being created and destroyed in the for loop, which would give each label's contents the same text.

How could I apply a list to store the labels and then call them in my for loop to read the corresponding information?
arcaine01 is offline   Reply With Quote
Old 05-10-2008, 05:51 PM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
You're using a somewhat messy traversing loop, which doesn't seem to take into account grouping (ie it makes a linear read). I suggest using the buildt-in Select functions to pick nodes. It could be something like this:
Code:
			System.Xml.XmlDocument Document = new System.Xml.XmlDocument();
			Document.Load("SomeFile.xml");

			System.Xml.XmlNode SubjectNode = Document.DocumentElement.SelectSingleNode("subject");
			System.Xml.XmlNode NamesNode = SubjectNode.SelectSingleNode("names");

			foreach(System.Xml.XmlNode RingNode in NamesNode.SelectNodes("ring"))
			{
				string Count = RingNode.Attributes["count"].Value;

				foreach(System.Xml.XmlNode NameNode in NamesNode.SelectNodes("name1"))
				{
					string Id = NameNode.Attributes["id"].Value;
				}
			}
I suggest putting in some tests for null in case the xml file lacks some tags/attributes.
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 05-10-2008, 11:10 PM   #3
Registered User
 
Join Date: Apr 2008
Posts: 58
Thanx again man...you seriously saved my life just now!
arcaine01 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting linked list please help with CODE scarlet00014 C Programming 3 09-27-2008 11:24 PM
Anyone good with linked list.....I am not.... chadsxe C++ Programming 11 11-10-2005 02:48 PM
Linked List jpipitone C Programming 4 03-30-2003 09:27 PM
Linked list with two class types within template. SilasP C++ Programming 3 02-09-2002 06:13 AM
singly linked list clarinetster C Programming 2 08-26-2001 10:21 PM


All times are GMT -6. The time now is 07:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22