C# kod ile form ve kontroller ekleme

  • Konbuyu başlatan Konbuyu başlatan tati
  • Başlangıç tarihi Başlangıç tarihi
Kodla Büyü

tati

Seçkin Üye
Seçkin Üye
Mesajlar
809
Merhaba arkadaşlar Form'a Toolbox’daki kontroller kullanıp programı yaptım. Ancak formu ve kontrolleri kod ile nasıl oluşturacağız ve kullanacağız fikri veya örnek var mıdır?

Teşekkürler
 
hocam googlede c# dinamik kontrol ekleme diye aratırsan oldukça fazla sonuç çıkmaktadır.
Kod:
Button buton = new Button();
buton.Name = "buton";
buton.Text = "Butonun ismi";
 buton.Click += new EventHandler(buton_Click); 
//önemli burada olay tanımlanıyor bu kısımda + ibaresinden sonra c# aşağıdaki buton_click bölümünü tamamlıyor
 this.Controls.Add(buton);
void buton_Click(object sender, EventArgs e)
        {
          burada da butona basınca yapılacak işlemler
        }

hocam burada form ekranında özellikler sekmesindeki tüm özellikleri buradan ekleyebilirsin. Renk stil yazı gibi gibi...
 
hocam öncelikle ilgin için teşekkürler.
program.cs dosyasında aşağıdaki şekilde formu üretiyorum ancak bundan sonra hiçbir şekilde kontrol ekleyemedim.
Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form yeniForm = new Form();
            yeniForm.Text = " Ödev";
            yeniForm.ShowDialog();


        }


    } 
}
 
tati' Alıntı:
arkadaşlar başka fikri olan yok mu :(

hocam tam olarak amacın ney ?
Formu dinamik olarak oluşturak ne elde edeceksin. Yani her defasında bunları oluşturmak pek mantıklı gelmedi.
Mantık olarak anlamadım ama alttaki arama işini görecektir
googlede c# dynamic create form
 
forma buton combobox vs..vs.. hangi kontorlü eklemek isityorsan o kontrolün sınıfından nesne örnekliyorsun misal
button b1=new button();

en önemli kısım b1'e ait özellikleri kullanıp tüm özelliiklerini yazılarını belirttikten sonra;
.....................
...............
This.Controls.ADD(b1);

yaparak forma bu kontrolü-butonu vs..vs..yi EKLEMEK!!This Form'u temsil ediyor ..
 
hocamlarım şimdi bir ödev iki şekilde yapılacak. Konu ise texbox a girilen sayıyı yazı olarak label yazdırması. bir form ve toolbox taki kontrolleri ekleyerek yaptım. bu çalışma da http://www.dosya.tc/server36/Mskqpd/samikaya.rar.html burda. ancak ikinci kısımda ise form ve kontrollleri kod kullanarak eklemek. yani başlangıç olarak projede hiç form olamayacak program.cs ile form oluşturulacak yukarıda bunun örneği var ancak kontrol ekleme ve bunları kullanma konusunda hiç bilgim yok :(.
 
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
public class Form2 : Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;

public Form2()
{

this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(189, 173);
this.button1.Name = "button1";
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(44, 34);
this.textBox1.Name = "textBox1";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(53, 75);
this.label1.Name = "label1";
this.label1.Text = "label1";
//
// Form1
//
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";

}

void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("000");
}
}
}
 
BBNET
Geri
Üst