![]() |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 58
| Storing labels in a list? 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);
}
}
}
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> 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 | |
| | #2 |
| Confused 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;
}
}
__________________ 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 | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 58
| Thanx again man...you seriously saved my life just now! |
| arcaine01 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |