locked
C# Ad components at runtime RRS feed

  • Question

  • Hello!

     

    Ive tried to add components like a panel or picturebox at runtime but dont really get it to work. And if it does, is it possible to add multiple components with same "goal/mission". Because i want to add panels randomly around the screen and then put a code in a timer to get the panels to animate their position in to the middle.

     

    Thank you!


    If you don´t Believe in it, Then it Doesn´t Exist!
    Wednesday, January 12, 2011 9:46 PM

Answers

  • Hi, first of all, if your post relates to Windows Forms, you should post them in the Windows Forms forum, you will get faster and more appropriate help there:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/threads

     

    About your question, see this example:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        private List<Panel> m_PanelList = new List<Panel>();
        public Form1()
        {
          InitializeComponent();
          this.Shown += new EventHandler(Form1_Shown);
        }
    
        void Form1_Shown(object sender, EventArgs e)
        {
          Random seed = new Random();
          for (int i = 0; i < 4; i++)
          {
            Panel pnl = new Panel();
            pnl.Width = 200;
            pnl.Height = 200;
            pnl.BackColor = Color.Blue;
    
            Random rnd = new Random(seed.Next());
            pnl.Left = rnd.Next(this.ClientRectangle.Width - pnl.Width);
            pnl.Top = rnd.Next(this.ClientRectangle.Height - pnl.Height);
    
            m_PanelList.Add(pnl);
            this.Controls.Add(pnl);
          }
    
          Timer tmr = new Timer();
          tmr.Interval = 10;
          tmr.Tick += new EventHandler(tmr_Tick);
          tmr.Start();
        }
    
        void tmr_Tick(object sender, EventArgs e)
        {
          bool allInTheCenter = true;
    
          foreach (Panel pnl in m_PanelList)
          {
            int targetLeft = (this.Width - pnl.Width) / 2;
            int targetTop = (this.Height - pnl.Height) / 2;
    
            if (pnl.Left != targetLeft)
            {
              allInTheCenter = false;
    
              if (pnl.Left < targetLeft)
                pnl.Left++;
              else
                pnl.Left--;
            }
    
            if (pnl.Top != targetTop)
            {
              allInTheCenter = false;
    
              if (pnl.Top < targetTop)
                pnl.Top++;
              else
                pnl.Top--;
            }
          }
    
          if (allInTheCenter)
            ((Timer)sender).Stop();
    
          Application.DoEvents();
        }
      }
    }
    
    

    If you have any questions, please ask.

    Regards,

    Fábio


    "To alcohol! The cause of and solution to all of life's problems." - Homer Simpson
    • Proposed as answer by bimbim Thursday, January 13, 2011 6:55 AM
    • Marked as answer by Mio_Miao Wednesday, January 19, 2011 5:29 AM
    Wednesday, January 12, 2011 10:03 PM