5 Replies - 720 Views - Last Post: 22 September 2011 - 05:07 PM Rate Topic: -----

#1 saksagan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-September 11

Adding a custom property to an object

Posted 22 September 2011 - 03:21 PM

Hello all, this will be my first post on this forum, so excuse my mistakes already :smile2:

I've been trying to add a property that will store an int number for each and every object i create. For instance, i wish to create a label at runtime (not a predefined one, but one that is created like "label blabla = new label();" and "controls.add(blabla);") So we don't know how many labels will be created. What i want is each and every label to have a property which i can then access from another class (more likely an event, like clicking on one of the labels) like "... = sender.myprop". I'll post my code so far:

private class datas
        {
            //declaring three properties name, section and hour
            private int name;
            public int nameprop
            {
                get { return name; }
                set { name = value; }
            }
            private int section;
            public int sectionprop
            {
                get { return section; }
                set { section = value; }
            }
            private int hour;
            public int hourprop
            {
                get { return hour; }
                set { hour = value; }
            }
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {   
            //for loops that will create a grid of 9 hours and 5 days and name them numbers, so i can refer to them later.                 
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= 9; j++)
                {
                    Label item_label = new Label();
                    Point loc = new Point();
                    Size labelsize = new Size();

                    item_label.Name = Convert.ToString(5*(j-1) + i);
                    item_label.Text = Convert.ToString(j + 7) + ".40";
                    item_label.BorderStyle = BorderStyle.FixedSingle;
                    item_label.TextAlign = ContentAlignment.MiddleCenter;
                    loc.X = (i-1) * 70 + 20;
                    loc.Y = (j-1) * 30 + 20;
                    labelsize.Height = 20;
                    labelsize.Width = 40;
                    item_label.Location = loc;
                    item_label.Size = labelsize;
                    item_label.Click += new EventHandler(item_label_Click);
                    Controls.Add(item_label);

                }
            }
        }

        public void item_label_Click(object sender, EventArgs e)
        {
            Control hop = new Control();
            hop = (Control)sender;
            datas hoba = new datas();
            hoba.hourprop = Convert.ToInt32(hop.Name);
            hoba.nameprop = Convert.ToInt32(textBox1.Text);
            hoba.sectionprop = Convert.ToInt32(textBox2.Text);
            //here it is, how do i assign these three properties to the label?!
        }



To anyone wondering, i'm trying to write a program that will choose my class sections for me, so that there won't be any clashes. B)

Is This A Good Question/Topic? 0
  • +

Replies To: Adding a custom property to an object

#2 MATTtheSEAHAWK   User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Adding a custom property to an object

Posted 22 September 2011 - 03:46 PM

I don't really understand what you want to do exactly? Assign a number to a label property? Just create a new class that derives from the label and add a property to that. Then you can use that class. Like so:

public class MyLabel : Label
{
   //your property here then use this class instead of label
}

Was This Post Helpful? 0
  • +
  • -

#3 saksagan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-September 11

Re: Adding a custom property to an object

Posted 22 September 2011 - 03:55 PM

umm i'm not sure i understand. the main problem is that the labels are not formed beforehand, so i can't directly refer to the i.e. 4th label, but i need to use (Control)sender to be able to refer to the label that triggered the event. what i want is, every created label to have an integer number of its own as a property, so i can read or change it from the click event. it's like when you define the properties and say Controls.Add(MyButton), it adds one (of many) button, i want to assign an instant of my property to the label.

This post has been edited by saksagan: 22 September 2011 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Adding a custom property to an object

Posted 22 September 2011 - 04:34 PM

Well, labels have a .Tag property that you can assign any object you want to it. Maybe that'll do what you need.
Was This Post Helpful? 4
  • +
  • -

#5 saksagan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-September 11

Re: Adding a custom property to an object

Posted 22 September 2011 - 05:04 PM

View PostCurtis Rutland, on 22 September 2011 - 04:34 PM, said:

Well, labels have a .Tag property that you can assign any object you want to it. Maybe that'll do what you need.


ah... i've defined an instant of my property class and equalized label.tag to that instant. that's done it. muchas gracias!
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Adding a custom property to an object

Posted 22 September 2011 - 05:07 PM

No problem, amigo.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1