Thread: Storing labels in a list?

  1. #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?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    Thanx again man...you seriously saved my life just now!

Popular pages Recent additions subscribe to a feed

Similar Threads

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