27.1.13

Dynamic controls using C# in windows forms


Here is the small piece of coding to do some Inheritance concept in C#.

Here is the problem,

  1. I want to write custom code to create controls on my windows forms page.
  2. Instead of Button class, I need to use inheritance to create my own button class and create objects.



  public class MyButton : Button
        {
            public MyButton()
                : base()
            {
                // set whatever styling properties needed here
                ForeColor = Color.Red;
                BackColor = Color.Green;
                 Location = new System.Drawing.Point(269, 96);
            }
        }

        public class MyButton1 : MyButton
        {
            public MyButton1()
                : base()
            {
                // set whatever styling properties needed here
                ForeColor = Color.Red;
                BackColor = Color.Green;
                Location = new System.Drawing.Point(269, 96);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MyButton1 btn = new MyButton1();
            btn.Name = "MyButton";
            this.Controls.Add(btn);
        }
    }


Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech

No comments:

Post a Comment