I declare a new form in a other form. this i have done it several times. But this time it give me the error of : An uhandled exception of Type ' System.StackOverflow exception occured in DTVIEW.exe
I went to the View detail option an it give to me the message: Cannot evaluate expression because the current thread is in a stack overflow state.
Satish KathiPosted Jul 24, 2008, 11:46 AM
What I understood from the above is
You have Form1 with a TextBox and a Button
When you click Button it will open Form2
Form2 also has a Button
When you click the Button in Form2 you have toshw the Text in TextBox of Form1.
If my understanding is correct what you have to do is when you are calling Form2 pass the reference of Form1 either in Constructor or a member then you can access the Form1 TextBox(make sure you declare TextBox as Public in Form1)
Form1......
public partial class Form11 : Form
{
public Form11()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form12 f = new Form12(this);
f.ShowDialog();
f.Dispose();
}
}
Form2.....
public partial class Form12 : Form
{
private Form11 _parent;
public Form12(Form11 form)
{
InitializeComponent();
_parent = form;
}
private void button1_Click(object sender, EventArgs e)
{
string halo = _parent.textBox1.Text + " Form2";
MessageBox.Show(halo);
}
}
Hope This Helps
Copying from the IDE: Copy the code from the Visual Studio, see my code above
Arnold PetronaPosted Jul 24, 2008, 1:48 PM
Thanks My Friends,
It is working now great
I'm Learning a lot from this Forums
Arnold PetronaPosted Jul 24, 2008, 10:42 AM
I look in mine program but nothing strange.
One question: is it possible if a declare a Form in the public partial class Form1 : Form a good idea of is it better to do it in the private void button1_Click();
Another question: When i put the mouse on the f1.textBox1.Text; it give me a note that the textBox1.Text came from TextBox Form1.textbox1. This mean that it get info from Form1 right? Why it still not get any info of Form1.
What do you mean with coy the code from the IDE? ( I don't understand that part)
Arnold PetronaPosted Jul 24, 2008, 10:06 AM
If I have a lot of this code , can it give me some problem sometimes like for not getting info from another form.
I will look through the project again if I see some strange things because i still have the problem for no getting the info.
I will keep you posted.
Many thanks for your Help
Ryan AlfordPosted Jul 24, 2008, 9:50 AM
----- Form2------
private void btnClose_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
MessageBox.Show(f1.textBox1.Text);
f1.Dispose();
this.DialogResult = DialogResult.OK;
this.Close();
}
Arnold PetronaPosted Jul 24, 2008, 9:45 AM
I made those changes in the Forms but i don't understand why a public textbox1.text of Form1 is not accessible in the other Forms when i declare the Form1 f1 = new Form1();
it give me the possibility to do f1.textbox1.Text issue but it get any data of Form1 in the runtime
Arnold PetronaPosted Jul 24, 2008, 9:30 AM
On the 3rd message of yours. The textbox is on public and I use it allready on other forms that i need to use.
Ryan AlfordPosted Jul 24, 2008, 9:26 AM
#2.... when setting the DialogResult on Form2, try using:
this.DialogResult = DialogResult.OK;
using that line along with your other code, I just opened atleast 200 Form2 objects without getting any error messages.
#3....the textbox on Form1 is a private object. You can't access it from an instance. If you want to access it, go to the designer, click on the textbox, go to the Properties window, and set the "Modifiers" property to "Public".
Arnold PetronaPosted Jul 24, 2008, 8:34 AM
I tried something else
I declare the Form2 in example button1_Click() in Form1. When the Form2 is open i need some info of Form1 to execute some funtcion, but i can't get those info from Form1. In the Button1_Click () of Form2 i did as follow:
button1_Click(object sender , EventArgs e)
{ Form1 f1= new Form()
string halo =f1.textbox1.Text + " Form2";
messageBox.Show( halo);
}
I did this several times allready in this same project that I'm workin but now it denie to get the info of Form1
In the runtime the messageBox will show me only the word "Form2"
Arnold PetronaPosted Jul 24, 2008, 8:22 AM
I still have the stack overflow
What I did is as follow:
-Every window that need to be open i did a DialogResult.OK
Form8 f8 = new Form8();
if ( f8.ShowDialog = = DialogResult.OK)
{ f8.Dispose}
In the Form8 I put as Follow
DialogResult = DialogResult.Ok;
this.Close
What else can I do to prevent this overflow stack.
I got the message about the stack overflow when i declare it public partial Form2: Form2
Ryan AlfordPosted Jul 23, 2008, 6:00 PM
------ Form1 -------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShowNextForm_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
f2.Dispose();
}
}
------ Form2 -------
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
Arnold PetronaPosted Jul 23, 2008, 5:43 PM
Another Question, if I do f1.ShowDialog , will it prevent the problem on the stack issue?
Or what is the part to look for to prevent this stack issue
Ryan AlfordPosted Jul 23, 2008, 4:58 PM
let's say you have two forms that move between each other: Form1 --> Form2 --> Form1 --> Form2 --> Form1 --> ....etc. If you create a new Form1 and a new Form2 everytime:
Form1 f1 = new Form1();
f1.Show();
Form2 f2 = new Form2();
f2.Show();
this code will eventually cause a stack overflow.
please post the relevant code from both Forms where you move between the forms.
Satish KathiPosted Jul 23, 2008, 4:52 PM
Change to
Form1 f1 = new Form1();
or
Form f1 = new Form1();
Arnold PetronaPosted Jul 23, 2008, 4:29 PM
What I wrote is this in a form
Form1 f1 = new Form();
I have the problem when i write this line. Tell me how to solve this thing and where
Ryan AlfordPosted Jul 23, 2008, 4:11 PM