| | |
Reutrn String From Child Form
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 88
Reputation:
Solved Threads: 2
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.
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.
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.
C# Syntax (Toggle Plain Text)
private void OpenForm_Click(object sender, EventArgs e) { this.ChildClass = new TextboxForm(); this.ChildClass.Show(); //Before the class loses scope and gets GC'ed I can call a property of the child class string strTemp = this.ChildClass.UserString; //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. }
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.
•
•
Join Date: Oct 2007
Posts: 172
Reputation:
Solved Threads: 16
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;
}
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;
}
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);
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?
can you repeat the part of the stuff where you said all about the things?
![]() |
Other Threads in the C# Forum
- Previous Thread: Client Server Architecture in VS.NET2005
- Next Thread: C# Mystery
| Thread Tools | Search this Thread |
.net 2007 access ado.net algorithm array barchart bitmap box broadcast buttons c# camera check checkbox client color combobox control conversion cs4 csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ httpwebrequest image index input install ip java label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox security serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml





