I am working on a C# project in which I have a couple of textboxs in my first form and through a second form user inputs the text data and it is produced in my first form (the already opened one). I am taking this data and search it to the database behind.
I used the following article to pass values from the second form to the first one ( http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx ).
The problem is that as soon as I take the text data from my first form the string variable is absolutly empty.
I tranformed the code of the article's project in order to see what I am saying. (As soon as the user clicks on the second scenario and writes the data, he closes the second form and press the enter button the messagebox alert is empty...)
any idea what is wrong?
Cheers
Loading
FroglegPosted Jun 9, 2011, 3:54 AM
FroglegPosted Jun 9, 2011, 3:49 AM
namespace DelegatesWinFormsDemo
{
public delegate void SetParameterValueDelegate(string value);
public delegate void AddItemDelegate(string item);
public partial class FrmMain : Form
{
//Declare delagete callback function, the owner of communication
public SetParameterValueDelegate SetParameterValueCallback;
string myString;//<<<<
{
InitializeComponent();
}
private void btnOpenFrm1_Click(object sender, EventArgs e)
{
FrmChild1 frm = new FrmChild1();
//Subscribe frm for Callback
this.SetParameterValueCallback += new SetParameterValueDelegate(frm.SetParamValueCallbackFn);
frm.Show();
}
private void btnOpenFrm2_Click(object sender, EventArgs e)
{
FrmChild2 frm = new FrmChild2();
//Subscribe frm for Callback
this.SetParameterValueCallback += new SetParameterValueDelegate(frm.SetParamValueCallbackFn);
frm.Show();
}
private void txtParam_TextChanged(object sender, EventArgs e)
{
//Send Notification to all subscribers
SetParameterValueCallback(txtParam.Text);
}
private void btnScenario2_Click(object sender, EventArgs e)
{
FrmDialog dlg = new FrmDialog();
//Subscribe this form for callback
dlg.AddItemCallback = new AddItemDelegate(this.AddItemCallbackFn);
dlg.ShowDialog();
}
private void AddItemCallbackFn(string item)
{
lstBx.Items.Add(item);
myString = item;//<<<<
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(myString);//<<<<
}
Michael OmmerPosted Jun 9, 2011, 2:06 AM
http://www.megaupload.com/?d=GKXWR799
Michael OmmerPosted Jun 9, 2011, 1:58 AM
FroglegPosted Jun 7, 2011, 2:11 PM