Kod İle TextBox Oluştur - CSharp


Sayı parametresine göre ilgili form nesnesine alt alta sıralanacak şekilde TextBox ekleyen alternatif bir C# kodunu görelim.

Kod


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpKodIleTextBoxNesnesiOlustur
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void KodIleTextBoxNesnesiOlustur(int sayi = 1)
        {
            int aralik = 5;
            int textBoxSayac = 1;

            for (int i = 0; i < sayi; i++)
            {
                TextBox textBox = new TextBox();
                textBox.Name = "textBox" + textBoxSayac;
                textBox.Top = 0 + aralik;
                textBox.Left = 20;
                textBox.Width = 100;
                textBox.Text = "textBox Nesnesi " + textBoxSayac;
                textBox.Font = new Font("Arial", 13.5f, FontStyle.Regular);
                textBox.Size = new Size(300, textBox.Width);

                this.Controls.Add(textBox);

                aralik = aralik + 30;
                textBoxSayac = textBoxSayac + 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.AutoScroll = true;
            KodIleTextBoxNesnesiOlustur(100);
        }
    }
}

Kod (Açıklamalı)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpKodIleTextBoxNesnesiOlustur
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void KodIleTextBoxNesnesiOlustur(int sayi = 1)
        {
            //--------------------------------------------------
            //TextBox nesneleri alt alta dizileceği zaman arada
            //ne kadar boşluk kalması gerektiğini bu değişkenle
            //belirliyoruz. Başlangıç değerini de 5 olarak atadık.
            //--------------------------------------------------
            int aralik = 5;

            //--------------------------------------------------
            //TextBox nesnelerini isimlendirirken (Name özelliğine
            //atama yapıyoruz) değişik isim vermek için sayılardan
            //faydalanıyoruz. Bu değişken de Name isimlendirmesi
            //için kullanılıyor.
            //--------------------------------------------------
            int textBoxSayac = 1;

            //--------------------------------------------------
            //TextBox nesnelerini oluşturacak döngüyü tanımla.
            //--------------------------------------------------
            for (int i = 0; i < sayi; i++)
            {

                //--------------------------------------------------
                //Yeni TextBox nesnesi örneği oluştur.
                //--------------------------------------------------
                TextBox textBox = new TextBox();

                //--------------------------------------------------
                //Nesnenin ismini ayarla.
                //--------------------------------------------------
                textBox.Name = "textBox" + textBoxSayac;

                //--------------------------------------------------
                //Nesne form alanında ne kadar üstte olacak?
                //Değer sıfır olduğunda formun kenar kısmına
                //değiyor.
                //--------------------------------------------------
                textBox.Top = 0 + aralik;

                //--------------------------------------------------
                //Oluşturulacak TextBox örneği Form nesnesinin
                //sol kısmından ne kadar uzakta olacak?
                //--------------------------------------------------
                textBox.Left = 20;

                //--------------------------------------------------
                //TextBox nesne örneğinin genişliğini ayarla.
                //--------------------------------------------------
                textBox.Width = 300;

                //--------------------------------------------------
                //TextBox nesne örneğinin "metnini" ayarla.
                //Name özelliği ile alakası yok bunun.
                //--------------------------------------------------
                textBox.Text = "textBox Nesnesi " + textBoxSayac;

                //--------------------------------------------------
                //Yazı özelliklerini ayarla.
                //--------------------------------------------------
                textBox.Font = new Font("Arial", 13.5f, FontStyle.Regular);

                //--------------------------------------------------
                //Oluşturulan textBox örneğini Form nesnesine ekle.
                //--------------------------------------------------
                this.Controls.Add(textBox);

                //--------------------------------------------------
                //Değişkenlere yeni değerler ata.
                //--------------------------------------------------
                aralik = aralik + 30;
                textBoxSayac = textBoxSayac + 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //--------------------------------------------------
            //Eğer TextBox nesneleri çok sayıdaysa bir yerden
            //sonra Form nesnesinden taşacaktır. Taşan nesneleri
            //görebilmek için Form nesnesinin AutoScroll özelliğini
            //aktifleştirdik. Böylelikle form üzerinde mouse 
            //yardımıyla aşağı yukarı gezinti yapabileceğiz.
            //--------------------------------------------------
            this.AutoScroll = true;

            //--------------------------------------------------
            //Oluşturulan metodu kullanarak Form nesnesine
            //istenilen sayıda TextBox ekle.
            //--------------------------------------------------
            KodIleTextBoxNesnesiOlustur(100);
        }
    }
}

Yararlanılan Kaynaklar
Etiketler
csharp açıklamalı içerik csharp textbox csharp windows forms