PDA

View Full Version : ساختن لیستی از کنترلها در زمان اجرا



z_rezaie
شنبه 10 مرداد 1388, 00:46 صبح
اگر بخواهیم برای این کنترلهایی که زمان اجرا ایجاد میشوند (مثل باتن)
رویداد بنویسیم باید چه کار کنیم؟؟

Ali_Mor
شنبه 10 مرداد 1388, 01:23 صبح
private void Form1_Load(object sender, EventArgs e)
{
Button btn=new Button();
btn.Click += new EventHandler(btn_click);
this.Controls.Add(btn);

}

private void btn_click(object sender, EventArgs e)
{
MessageBox.Show("test");
}

majid325
شنبه 10 مرداد 1388, 02:28 صبح
private void btnStart_Click(object sender, EventArgs e)
{
int IntTxb1, IntTxb2;

if ((!int.TryParse(txb1.Text, out IntTxb1)) || (!int.TryParse(txb2.Text, out IntTxb2))) this.Text = "Value Is Not Valid...";
else
{
this.Controls.Clear();
this.Text = "Please Wait...";

TextBox[,] txbArry = new TextBox[IntTxb1 ,IntTxb2 ];

for (int x = 0; x < IntTxb1; x++)
{
for (int y = 0; y < IntTxb2 ; y++)
{
txbArry[x, y] = new TextBox();
txbArry[x, y].Size = new Size(30, 20);
txbArry[x, y].MaxLength = 3;

txbArry[x, y].Location = new Point(txbArry[x, y].Width * (x+1) , txbArry[x, y].Height * (y+1));
txbArry[x, y].KeyPress += (sendre1, e1) =>
{
e1.Handled = (!System.Text.RegularExpressions.Regex.IsMatch(e1. KeyChar.ToString(), "\\d+"));
};

this.Controls.Add(txbArry[x, y]);
}
}

this.Text = "Please Enter Value's...";
}
}