Thread: List of Errors in C# program.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    List of Errors in C# program.

    I have a list of problems that I can't seem to solve for a final project in my introduction to programming course, and I was hoping that some of you may be assist me.

    My First problem, is that I can't seem to populate my dynamically created labels with the xml elements that I want....
    I want the labels to display the id of the nodes named "name1", but it only brings up the first name1 node, and loads it for each of the labels created!

    Here is the entire XML so far:
    Code:
    <subject id="you">
        <name id="Me">
            <ring id="bob" count="12">
                <name1 id="Ghandi"></name1>
                <name1 id="Bob"></name1>
                <name1 id="Jerry"></name1>
                <name1 id="Tony"></name1>
                <name1 id="Francis"></name1>
                <name1 id="Fabio"></name1>
                <name1 id="Lulu"></name1>
                <name1 id="Genie"></name1>
                <name1 id="Sabrina"></name1>
                <name1 id="Bambi"></name1>
                <name1 id="Rambo"></name1>
                <name1 id="Larry"></name1>
            </ring>
        </name>
    </subject>
    Here is the Entire 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;
                
                //int count = 0;
                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)
    	{
    		double x = Form1.ActiveForm.Width/2;
    		double y = Form1.ACtiveForm.Height/2;
    		this.Drawing.Point(x, y);	
    	}
        }
    }
    If you run the program, you will see that when you click on myLabel, it will create the proper amount of defined elements/labels and position them in a circular pattern around the initial label, however, each label comes up with the text "Ghandi". Instead of loading Ghandi for one, Bob for another, and Jerry for the next created label, it loads Ghandi for all of them.


    My Second problem is that I want myLabel to be populated with a random "ring" node. In my XML, I will create a list of "ring" nodes and I want the contents of myLabel to be populated by the id of a random ring node...but I have NO idea on how to accomplish that.

    My FINAL problem with this code, is that I am trying to make it so that when you click on the dynamically created labels "lbl", it will reposition the label to the center. I tried to do this with the:
    Code:
    	private void lbl_Click(object sender, EventArgs e)
    	{
    		double x = Form1.ActiveForm.Width/2;
    		double y = Form1.ACtiveForm.Height/2;
    		this.Drawing.Point(x, y);	
    	}
    however, it is returning the variables x and y as null values.
    So I tried:
    Code:
    	private void lbl_Click(object sender, EventArgs e)
    	{
    		this.Drawing.Point(50, 50);	
    	}
    But it is still returning the values as null!

    How can I make the dynamically created label be centered when it is click on?

    Thank you for any assistance you may provide!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) -

    2) Check out the System.Random class

    3) To change a label location you use MyLabel.Location = new System.Drawing.Point(NewX, NewY);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. Strange errors in linked list program
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 01-02-2006, 08:36 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Linked list generates errors..
    By Nutshell in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 05:19 AM