Reutrn String From Child Form

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2006
Posts: 88
Reputation: blacklocist is an unknown quantity at this point 
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

Reutrn String From Child Form

 
0
  #1
Jan 30th, 2008
Hi All,

I am having this issue and don't really know a good way to overcome it. I have a main form. With a click of a button another form is created. For the example let's just say it has a simple text box. You type whatever you want in it.

My problem arises is how do I get the string from that child class to the calling class.

  1. private void OpenForm_Click(object sender, EventArgs e)
  2. {
  3. this.ChildClass = new TextboxForm();
  4. this.ChildClass.Show();
  5. //Before the class loses scope and gets GC'ed I can call a property of the child class
  6.  
  7. string strTemp = this.ChildClass.UserString;
  8.  
  9. //But the problem is after it shows the form it immediatly assigns the strTemp with null or "" because the user hasn't even had time to type something in the box.
  10.  
  11. }

So what I would like to know is a a good solution for when the child form closes it can somehow pipe back information to the parent class. I tried doing some research and a delegate kept popping in my head but through frustration couldn't get them working. So is a delegate a good solution for this or am I completely missing something.
Forgive my spelling, I spell code not English.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

Re: Reutrn String From Child Form

 
1
  #2
Jan 31st, 2008
I give an alternative to do so:
First:
Create a class in witch u create a static string variable
Here is the procedure:
first step:

public class Variables
{
public static string myVariable ="";
}

Second step:
In the event handler ChildForm_Formclosing(object sender, EventArgs e)
implement it as follow
ChildForm_Formclosing(object sender, EventArgs e)
{
Variables.MyVariable=TextBox.Text.ToString();
}

Third step:
In the Main form

//define an instantiate a new ChildForm
ChildForm oChildForm = new ChildForm();
//Add a new event
oChildForm.FormClosing+= new EventHandler(ChildForm_FormClosing)
{
TextBox1.Text = Variables.MyVariable;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: tostrinj is an unknown quantity at this point 
Solved Threads: 1
tostrinj's Avatar
tostrinj tostrinj is offline Offline
Newbie Poster

Re: Reutrn String From Child Form

 
1
  #3
Jan 31st, 2008
Ok, sounds simple but how about a child form having a read only property that can hold a string.
Once the child form closes, this property should still be available immediately after.

change

this.ChildClass = new TextboxForm();
this.ChildClass.Show();
to

this.ChildClass = new TextboxForm();
this.ChildClass.ShowDialog(this);
Last edited by tostrinj; Jan 31st, 2008 at 4:17 pm.
===========================
can you repeat the part of the stuff where you said all about the things?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 88
Reputation: blacklocist is an unknown quantity at this point 
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

Re: Reutrn String From Child Form

 
0
  #4
Feb 2nd, 2008
Thanks All!
Forgive my spelling, I spell code not English.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC