Problem with Delegates
-
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(); } }
-
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(); } }
-
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(); } }
Hi. Try to create this in Form2:
public int GetNumericValue()
{
this.ShowDialog();//This code will run after form is closed int iOut = 0; int.TryParse(textBox1.Text, out iOut); return iOut;
}
and change the button_click event to
private void button1_Click(object sender, EventArgs e)
{
Form2 frmOpen = new Form2();
textBox1.Text = frmOpen.GetNumericValue().ToString();
}This is similar to the good old InputBox in VB. Kjetil