Sunday, 1 May 2011

Dynamic Button in C#


Dynamic Button in C#




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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a Button object
            Button btn1 = new Button();

            // Set Button properties
            btn1.Height = 50;
            btn1.Width = 250;
            btn1.BackColor = Color.Gray;
            btn1.ForeColor = Color.Black;
            btn1.Location = new Point(20, 100);
            btn1.Text = "Dynamic Button";
            btn1.Name = "Dynamic_Button";
            btn1.Font = new Font("Georgia", 16);

            // Add a Button Click Event handler
            btn1.Click += new EventHandler(Btn1_Click);

            // Add Button to the Form.

            this.Controls.Add(btn1);          
        }
       
        private void Btn1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Dynamic button is clicked");
        }
    }
}

No comments:

Post a Comment