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?