Hello everyone, I'm Kruvazör. Today, we'll talk about creating dynamic forms with C#.
Creating dynamic forms in the C# programming language allows you to manage the user interface of your application flexibly at runtime.
Firstly, an object is created from the Form class, and then necessary objects (such as buttons, labels, etc.) are dynamically added to this object, and their positions are determined. The properties and events of objects can also be dynamically set. This approach is used to create flexible and customizable user forms according to the needs of the application.
For example, providing a dynamic form in response to evolving customer requests in a desktop automation you're building can greatly simplify your tasks.
Now that we have a general understanding of the topic, let's provide an example.
Here's our scenario:
A company is recording the products it receives on a given day one by one and wants to export them. However, the number of products the company receives is never constant. Some days, it might receive 5, while on others, it could be 5000. I used XML for outputting the files.
Let's start designing the form:
1 label
1 textbox
1 button
1 flowlayoutpanel
Let's define our lists to hold the dynamically created labels and textboxes in the global scope.
Creating dynamic forms in the C# programming language allows you to manage the user interface of your application flexibly at runtime.
Firstly, an object is created from the Form class, and then necessary objects (such as buttons, labels, etc.) are dynamically added to this object, and their positions are determined. The properties and events of objects can also be dynamically set. This approach is used to create flexible and customizable user forms according to the needs of the application.
For example, providing a dynamic form in response to evolving customer requests in a desktop automation you're building can greatly simplify your tasks.
Now that we have a general understanding of the topic, let's provide an example.
Here's our scenario:
A company is recording the products it receives on a given day one by one and wants to export them. However, the number of products the company receives is never constant. Some days, it might receive 5, while on others, it could be 5000. I used XML for outputting the files.
Let's start designing the form:
1 label
1 textbox
1 button
1 flowlayoutpanel
Let's define our lists to hold the dynamically created labels and textboxes in the global scope.
Kod:
private List<TextBox> textBoxListesi = new List<TextBox>();
private List<Label> labelListesi = new List<Label>();
Then, let's navigate to the button click event and write our code.
Kod:
private void button1_Click(object sender, EventArgs e)
{
// Her bir TextBox ve Label için varsayılan uzunluk
int satirUzunlugu = 700;
int kontrolGenislik = satirUzunlugu / 5;
// Form genişliği, bir kenarda boşluk bırakmak için
int formGenislik = satirUzunlugu + 20;
// TextBox sayısını TextBox'tan al
int textBoxSayisi = Convert.ToInt16(textBox1.Text);
// FlowLayoutPanel'i temizle
flowLayoutPanel1.Controls.Clear();
// TextBox ve Label listelerini temizle
textBoxListesi.Clear();
labelListesi.Clear();
// Kalan TextBox sayısını takip etmek için değişken
int kalanTextBoxSayisi = textBoxSayisi;
// TextBox ve Label'ları oluştur ve FlowLayoutPanel'e ekle
for (int i = 0; i < textBoxSayisi; i++)
{
// Label oluştur
Label urunLabel = new Label();
urunLabel.Name = "lblUrun" + i.ToString();
urunLabel.Text = "Ürün" + (i + 1).ToString() + ":";
// TextBox oluştur
// buralarda textbox ve labellara özellikler penceresinden verdiğimiz özellikleri kod olarak veriyoruz.
TextBox yeniTextBox = new TextBox();
yeniTextBox.Name = "txtBox" + i.ToString();
yeniTextBox.Text = "";
yeniTextBox.Width = kontrolGenislik - 10;
yeniTextBox.Height = 20;
// FlowLayoutPanel'e Label ve TextBox'i ekle
flowLayoutPanel1.Controls.Add(urunLabel);
flowLayoutPanel1.Controls.Add(yeniTextBox);
// Label ve TextBox'i ilgili listelere ekle
textBoxListesi.Add(yeniTextBox);
labelListesi.Add(urunLabel);
// Kalan TextBox sayısını azalt
kalanTextBoxSayisi--;
}
// Kaydet Button'u oluştur ve FlowLayoutPanel'e ekle bu button ile xml işlemi yapacağız
Button kaydetButton = new Button();
kaydetButton.Name = "btnKaydet";
kaydetButton.Text = "Kaydet";
kaydetButton.Width = 100;
kaydetButton.Height = 30;
kaydetButton.Click += new EventHandler(KaydetButtonClick);
flowLayoutPanel1.Controls.Add(kaydetButton);
// FlowLayoutPanel'in AutoScroll özelliğini etkinleştir
flowLayoutPanel1.AutoScroll = true;
}
Let's navigate to the click event of the "Save" button.
Kod:
private void KaydetButtonClick(object sender, EventArgs e)
{
// Verileri XML'e aktar
XmlDocument xmlDocument = new XmlDocument();
XmlElement rootElement = xmlDocument.CreateElement("TextBoxData");
for (int i = 0; i < textBoxListesi.Count; i++)
{
XmlElement textBoxElement = xmlDocument.CreateElement("TextBox");
textBoxElement.SetAttribute("Name", textBoxListesi[i].Name);
textBoxElement.SetAttribute("Text", textBoxListesi[i].Text);
textBoxElement.SetAttribute("UrunAdi", labelListesi[i].Text.Replace(":", "")); // ':' karakterini kaldır
rootElement.AppendChild(textBoxElement);
}
xmlDocument.AppendChild(rootElement);
// XML belgesini proje klasörüne kaydet
string xmlDosyaYolu = System.IO.Path.Combine(Application.StartupPath, "TextBoxData.xml");
xmlDocument.Save(xmlDosyaYolu);
MessageBox.Show("Veriler XML dosyasına aktarıldı. Dosya yolu: " + xmlDosyaYolu);
}
Here are the complete codes:
Kod:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
namespace DynamicFormDesign
{
public partial class Form1 : Form
{
private List<TextBox> textBoxListesi = new List<TextBox>();
private List<Label> labelListesi = new List<Label>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Her bir TextBox ve Label için varsayılan uzunluk
int satirUzunlugu = 700;
int kontrolGenislik = satirUzunlugu / 5;
// Form genişliği, bir kenarda boşluk bırakmak için
int formGenislik = satirUzunlugu + 20;
// TextBox sayısını TextBox'tan al
int textBoxSayisi = Convert.ToInt16(textBox1.Text);
// FlowLayoutPanel'i temizle
flowLayoutPanel1.Controls.Clear();
// TextBox ve Label listelerini temizle
textBoxListesi.Clear();
labelListesi.Clear();
// Kalan TextBox sayısını takip etmek için değişken
int kalanTextBoxSayisi = textBoxSayisi;
// TextBox ve Label'ları oluştur ve FlowLayoutPanel'e ekle
for (int i = 0; i < textBoxSayisi; i++)
{
// Label oluştur
Label urunLabel = new Label();
urunLabel.Name = "lblUrun" + i.ToString();
urunLabel.Text = "Ürün" + (i + 1).ToString() + ":";
// TextBox oluştur
TextBox yeniTextBox = new TextBox();
yeniTextBox.Name = "txtBox" + i.ToString();
yeniTextBox.Text = "";
yeniTextBox.Width = kontrolGenislik - 10;
yeniTextBox.Height = 20;
// FlowLayoutPanel'e Label ve TextBox'i ekle
flowLayoutPanel1.Controls.Add(urunLabel);
flowLayoutPanel1.Controls.Add(yeniTextBox);
// Label ve TextBox'i ilgili listelere ekle
textBoxListesi.Add(yeniTextBox);
labelListesi.Add(urunLabel);
// Kalan TextBox sayısını azalt
kalanTextBoxSayisi--;
}
// Kaydet Button'u oluştur ve FlowLayoutPanel'e ekle
Button kaydetButton = new Button();
kaydetButton.Name = "btnKaydet";
kaydetButton.Text = "Kaydet";
kaydetButton.Width = 100;
kaydetButton.Height = 30;
kaydetButton.Click += new EventHandler(KaydetButtonClick);
flowLayoutPanel1.Controls.Add(kaydetButton);
// FlowLayoutPanel'in AutoScroll özelliğini etkinleştir
flowLayoutPanel1.AutoScroll = true;
}
private void KaydetButtonClick(object sender, EventArgs e)
{
// Verileri XML'e aktar
XmlDocument xmlDocument = new XmlDocument();
XmlElement rootElement = xmlDocument.CreateElement("TextBoxData");
for (int i = 0; i < textBoxListesi.Count; i++)
{
XmlElement textBoxElement = xmlDocument.CreateElement("TextBox");
textBoxElement.SetAttribute("Name", textBoxListesi[i].Name);
textBoxElement.SetAttribute("Text", textBoxListesi[i].Text);
textBoxElement.SetAttribute("UrunAdi", labelListesi[i].Text.Replace(":", "")); // ':' karakterini kaldır
rootElement.AppendChild(textBoxElement);
}
xmlDocument.AppendChild(rootElement);
// XML belgesini proje klasörüne kaydet
string xmlDosyaYolu = System.IO.Path.Combine(Application.StartupPath, "TextBoxData.xml");
xmlDocument.Save(xmlDosyaYolu);
MessageBox.Show("Veriler XML dosyasına aktarıldı. Dosya yolu: " + xmlDosyaYolu);
}
}
}
Topic Owner @Kruvazör C# Dinamik Form oluşturma