Ken H

Ken H

  • NA
  • 646
  • 361.7k

How to assign values to dynamic controls in c# winform?

Jul 3 2014 12:04 PM
Hi friend,
   How to give dynamic control setting values?
 
My codes:
 
Form1:
using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBox dynamicTB= new TextBox();
dynamicTB.Top = 20;
dynamicTB.Left = 20;
dynamicTB.MouseClick += new MouseEventHandler(textBox_MouseClick);
this.Controls.Add(dynamicTB);
TextBox dynamicTB2 = new TextBox();
dynamicTB2.Top = 50;
dynamicTB2.Left = 20;
dynamicTB2.MouseClick += new MouseEventHandler(textBox_MouseClick);
this.Controls.Add(dynamicTB2);
/*
etc ...
*/
}
private void textBox_MouseClick(object sender, MouseEventArgs e) {
Form2 f2 = new Form2();
f2.GetDateTimeValue += new Form2.GetDateTimeValueHandler(GetValueForTextBox);
f2.Show();
}
private void GetValueForTextBox(object sender, GetValueForTextBoxArgs e)
{
/*
   TextBox tb = (TextBox)sender;
   tb.Text = e.VText;
*/
// MessageBox.Show(e.VText);
// To assign to the current object(TextBox).
// In other words: Assign a value to the object that triggered the event
}
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public delegate void GetDateTimeValueHandler(object sender, GetValueForTextBoxArgs e);
public event GetDateTimeValueHandler GetDateTimeValue;
private void button1_Click(object sender, EventArgs e)
{
GetValueForTextBoxArgs args = new GetValueForTextBoxArgs(dateTimePicker1.Value.ToString());
GetDateTimeValue(this, args);
this.Dispose();
}
}
public class GetValueForTextBoxArgs : System.EventArgs {
public string VText { get; set; }
public GetValueForTextBoxArgs(string text)
{
this.VText = text;
}
}
 
Thanks. 
 

Answers (2)