Kod:
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 Form_Kontrol_Ekleme
{
public partial class Kontrol_Ekleme : Form
{
public Kontrol_Ekleme()
{
InitializeComponent();
InitializeMyComponent();
}
private **** Form1_Load(object sender, EventArgs e)
{
//açılan pencereye isim verelim
this.Text = "Kontol Ekleme";
//Açılan Pencereye bir buton ekleyelim (= dinamik buton ekleme)
Button buton = new Button(); //Button nesnesinden bir örnek oluşturma
//butona bir isim verme
buton.Name = "buton";
// bu butonın text ine bir isim verelim
buton.Text = "Başlığı Değiştir";
//butona basıldığında başlığı ="Başlık değişti" olarak değiştirsin
// bu işlem için butona basılma olay bağlanmalı
buton.Click += new EventHandler(buton_Click);
//burada butona basılınca 'buton_Click' methodundaki işlemler yapılacak
//butona yeni bir boyut verelim
buton.Size = new Size(200, 50);
//butonun window üzerindeki yerine ayarlayalım
buton.******** = new Point(50, 50);
//butonun üzerine fare geldiğinde rengi değişsin (mavi olsun)
buton.FlatAppearance.MouseOverBackColor = Color.Blue;
//butona fare ile basıldığında rengi kırmızı olsun
buton.FlatAppearance.MouseDownBackColor = Color.Red;
//buton gorunumunu ayarlayalım
buton.FlatStyle = FlatStyle.Flat;
// şimdi butonu penceremize (window veya forma) ekleyelim
this.Controls.Add(buton);
// böylece pencereye bir kontrol ve onun görevleri eklemiş olduk
}
**** buton_Click(object sender, EventArgs e)
{
//butona tıklandığında bu metot çalışacak
//burada formun başlığını değiştirelim
this.Text = "Başlık değişti.";
}
private **** InitializeMyComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
}
}