System.StackOverflowException 通过表单 [英] System.StackOverflowException by forms
问题描述
我正在尝试使用按钮打开另一个表单,但刚开始时还可以.在一些表格使 stackoverflow 错误之后!错误在表单 1、表单 2 和表单 3 上(我开始调试多次):代码非常简单.喜欢表格 3:
I'm trying to open another form with a button and it's only ok at the beginning. After a few forms made the stackoverflow error acours ! The error is on form 1, form 2 and form 3 (I started to debug multiple times): the codes are really simple. like for form 3:
public partial class Form2 : Form
{
Form3 obrok = new Form3();
public Form2()
{
InitializeComponent();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
obrok.Show();
this.Hide();
}
}
(从注释中添加,特别是为了保留代码缩进(MI))
上述错误已解决(使用语句)现在问题是当我不想将值从文本框保存到访问数据库时.编码:表单 1 用户 = 新表单 1();private void button1_Click(object sender, EventArgs e){string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"+ "数据源=C:\Users\Simon\Desktop\save.mdb";
The above error is solved(using statement) Now the problem is when i wan't to save values from textbox to the access database. The code: Form1 users=new Form1(); private void button1_Click(object sender, EventArgs e) { string conString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\Users\Simon\Desktop\save.mdb";
OleDbConnection empConnection = new OleDbConnection(conString);
string insertStatement = "INSERT INTO obroki_save "
+ "([ID_uporabnika],[datum],[ID_zivila],[skupaj_kalorij]) "
+ "VALUES (@ID_uporabnika,@datum,@ID_zivila,@skupaj_kalorij)";
OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);
insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Char).Value = users.iDTextBox.Text;
insertCommand.Parameters.Add("@datum", OleDbType.Char).Value = DateTime.Now;
insertCommand.Parameters.Add("@ID_zivila", OleDbType.Char).Value = iDTextBox.Text;
insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Char).Value = textBox1.Text;
empConnection.Open();
try
{
int count = insertCommand.ExecuteNonQuery();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
empConnection.Close();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
}
}
当我不想传递值时,它会提示条件表达式中的数据类型有错误.
When i wan't to pass the values it show a massage that there is an error in the data type in conditional expression .
推荐答案
因此,Form2 在构造时实例化了一个新的 Form3,Form3 在构造时实例化了一个新的 Form5……也许 Form5 甚至实例化了另一个表单,然后又实例化了另一个表单等等?在我看来,您以某种方式声明了您的类,以便实例化其中一个实际上实例化了许多其他表单对象,甚至可能还有循环引用(例如 Form3=>Form5=>Form???=>...=>Form3=>Form5=>Form???=>... 永远),因此您在表单的构造函数中陷入了无限递归.
So, Form2 instances a new Form3 when it's constructed, Form3 instances a new Form5 on construction... maybe Form5 even instances yet another form, which instances another one and so on? In my opinion, you declared your classes in a way so that instancing one of them actually instances a lot of other form objects, and maybe there's even a circular reference (e.g. Form3=>Form5=>Form???=>...=>Form3=>Form5=>Form???=>... forever), so you're stuck in infinite recursion in the constructors of your forms.
如果事情是这样的话,如果你的程序逻辑可能的话,你应该将你的表单声明移动到显示它们的方法中,最好是包含在 using 语句中:这样它们只有在需要时才会被实例化, 并在不再使用时完全销毁.
If things are like that, if possible for your program logic, you should move your declarations of the forms inside the method that shows them, better if enclosed inside a using statement: in this way they get instanced only when they are needed, and are destroyed exactly when they are no longer used.
这篇关于System.StackOverflowException 通过表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!