Dynamic Adding of Objects

This short video introduces the dynamic creation and loading of an array of objects in a C# Windows Form. It includes placing the objects, create an event handler and adding the object to the form.

C SharpArraysObjects
1.0x

Dynamic Adding of Objects

Created 2 years ago

Duration 0:05:46
lesson view count 46
This short video introduces the dynamic creation and loading of an array of objects in a C# Windows Form. It includes placing the objects, create an event handler and adding the object to the form.
Select the file type you wish to download
Slide Content
  1. Dynamic Adding of Objects

    Slide 1 - Dynamic Adding of Objects

    • Adding Objects to a form using C# Code
  2. Declare the Array of Objects

    Slide 2 - Declare the Array of Objects

    • Button[] PushMe = new Button[10];
    • Object Type
    • Allocate References to Objects
    • This does not create 10 objects!
    • Array
    • Name
  3. Create The Objects

    Slide 3 - Create The Objects

    • for (int index = 0; index < PushMe.Length; index++)
    • {
    • PushMe[index] = new Button();
    • PushMe[index].Left = myLeft + myWidth *index;
    • PushMe[index].Top = myTop;
    • PushMe[index].Height = myHeight;
    • PushMe[index].Width = myWidth;
    • PushMe[index].Text = index.ToString();
    • this.Controls.Add(PushMe[index]);
    • }
  4. Creating An Event Handler

    Slide 4 - Creating An Event Handler

    • private void PushMe_Click (object sender, EventArgs e)
    • {
    • Button me = (Button)sender;
    • MessageBox.Show("Button " + me.Text);
    • }
  5. Attach Event Handler

    Slide 5 - Attach Event Handler

    • for (int index = 0; index < PushMe.Length; index++)
    • {
    • PushMe[index] = new Button();
    • PushMe[index].Left = myLeft + myWidth *index;
    • PushMe[index].Top = myTop;
    • PushMe[index].Height = myHeight;
    • PushMe[index].Width = myWidth;
    • PushMe[index].Text = index.ToString();
    • PushMe[index].Click += new System.EventHandler(PushMe_Click);
    • this.Controls.Add(PushMe[index]);
    • }
  6. Run Time

    Slide 6 - Run Time