3
Reply

Problem with Delegates

Aey Saelee

Aey Saelee

Jun 26 2008 3:31 AM
2.5k
Hi all
I'm having a problem with delegate. In my project I have 2 forms, form1 and form2. From form1 I have a button there just for opening form2. And from form2 I have a text box there to enter an Int value. After getting a value from the text box I want to send it back to form1 and appear in a text box on form1.

I do this with a delegate but I don't know if my way is the way you use to solve this problem or not.

Here is my code:


Form1


public delegate void TestDelegate(int i);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frmOpen = new Form2();
frmOpen.ShowDialog();
}
public void GetValueA(int i)
{
textBox1.Text = i.ToString();
}
}


Form2

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int b;
b = Convert.ToInt32(textBox1.Text);

Form1 frm1=new Form1();
TestDelegate td = new TestDelegate(frm1.GetValueA);
td(b);
Close();
}
}

Answers (3)