Slate.exe 中发生了 'System.StackOverflowException' 类型的未处理异常
An unhandled exception of type 'System.StackOverflowException' occurred in Slate.exe
我正在使用 C# 开发一个应用程序,但意外地开始收到此异常 "An unhandled exception of type 'System.WhosebugException' occurred in Slate.exe"。
以下是我的代码的详细信息。
这是 class,我在其中初始化了我的主要对象 class。
public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); //This thing is raising Exception.
public void SaveAs()
{
SaveFileDialog SFD = new SaveFileDialog();
if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
String FPath = SFD.FileName;
StreamWriter SWriter = new StreamWriter(File.Create(FPath));
SWriter.Write(MainWindowObj.PlayGround.Text);
SWriter.Dispose();
}
}
public void NewFile()
{
MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
}
public void OpenFile()
{
OpenFileDialog OFD = new OpenFileDialog();
if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader SReader = new StreamReader(File.OpenRead(OFD.FileName));
MainWindowObj.PlayGround.Text = SReader.ReadToEnd();
}
}
}
我的主要 class 代码。我已经在其中初始化了 MenuConrols class.
的对象
public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls();
TextEditor_Preferences Prefrences = new TextEditor_Preferences();
public int SaveStatus = 0;
TextEditor_Preferences PreferencesForm = new TextEditor_Preferences();
public MainWindow()
{
InitializeComponent();
}
private void MainMenuFile_SaveAs_Click(object sender, EventArgs e)
{
Menu.SaveAs();
SaveStatus = 1;
}
private void MainMenuFile_New_Click(object sender, EventArgs e)
{
if (SaveStatus == 0 && PlayGround.Text.Contains(" "))
{
Menu.NewFile();
} else
{
PlayGround.Text = "";
}
}
private void MainMenuFile_Open_Click(object sender, EventArgs e)
{
Menu.OpenFile();
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "N")
{
if (SaveStatus == 0 && !String.IsNullOrWhiteSpace(PlayGround.Text))
{
DialogResult DResult = MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (DResult == DialogResult.Yes)
{
PlayGround.Text = "";
} else
{
}
}
else
{
PlayGround.Text = "";
}
}
else if (e.Control && e.KeyCode.ToString() == "O")
{
Menu.OpenFile();
}
else if (e.Control && e.KeyCode.ToString() == "S")
{
Menu.SaveAs();
SaveStatus = 1;
}
}
private void MainMenuEdit_Preferences_Click(object sender, EventArgs e)
{
PreferencesForm.ShowDialog();
}
}
请给我一个解决方案。 如果我遗漏了什么或者问题没有正确提出,请明确告诉我。
谢谢
这是因为,您有一个永无止境的代码正在执行,您正在 MenuControls
class:
中实例化 MainWindow
的实例
public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); // note this
并且在 MainWindow
class 中,您有另一个正在实例化的字段 MenuControls
,因此当您创建 MainWindow
实例时,它会实例化 MenuControls
字段,它再次初始化它拥有的 MainWindow
字段,并继续这样做,因此导致计算器溢出异常。
public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls(); // note this
您需要从 MenuControls
class 中删除 MainWindow
字段,反之亦然,我看到您不需要 MainWindow
字段 MenuControl
class,从那里删除它。
我正在使用 C# 开发一个应用程序,但意外地开始收到此异常 "An unhandled exception of type 'System.WhosebugException' occurred in Slate.exe"。
以下是我的代码的详细信息。
这是 class,我在其中初始化了我的主要对象 class。
public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); //This thing is raising Exception.
public void SaveAs()
{
SaveFileDialog SFD = new SaveFileDialog();
if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
String FPath = SFD.FileName;
StreamWriter SWriter = new StreamWriter(File.Create(FPath));
SWriter.Write(MainWindowObj.PlayGround.Text);
SWriter.Dispose();
}
}
public void NewFile()
{
MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
}
public void OpenFile()
{
OpenFileDialog OFD = new OpenFileDialog();
if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader SReader = new StreamReader(File.OpenRead(OFD.FileName));
MainWindowObj.PlayGround.Text = SReader.ReadToEnd();
}
}
}
我的主要 class 代码。我已经在其中初始化了 MenuConrols class.
的对象public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls();
TextEditor_Preferences Prefrences = new TextEditor_Preferences();
public int SaveStatus = 0;
TextEditor_Preferences PreferencesForm = new TextEditor_Preferences();
public MainWindow()
{
InitializeComponent();
}
private void MainMenuFile_SaveAs_Click(object sender, EventArgs e)
{
Menu.SaveAs();
SaveStatus = 1;
}
private void MainMenuFile_New_Click(object sender, EventArgs e)
{
if (SaveStatus == 0 && PlayGround.Text.Contains(" "))
{
Menu.NewFile();
} else
{
PlayGround.Text = "";
}
}
private void MainMenuFile_Open_Click(object sender, EventArgs e)
{
Menu.OpenFile();
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "N")
{
if (SaveStatus == 0 && !String.IsNullOrWhiteSpace(PlayGround.Text))
{
DialogResult DResult = MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (DResult == DialogResult.Yes)
{
PlayGround.Text = "";
} else
{
}
}
else
{
PlayGround.Text = "";
}
}
else if (e.Control && e.KeyCode.ToString() == "O")
{
Menu.OpenFile();
}
else if (e.Control && e.KeyCode.ToString() == "S")
{
Menu.SaveAs();
SaveStatus = 1;
}
}
private void MainMenuEdit_Preferences_Click(object sender, EventArgs e)
{
PreferencesForm.ShowDialog();
}
}
请给我一个解决方案。 如果我遗漏了什么或者问题没有正确提出,请明确告诉我。 谢谢
这是因为,您有一个永无止境的代码正在执行,您正在 MenuControls
class:
MainWindow
的实例
public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); // note this
并且在 MainWindow
class 中,您有另一个正在实例化的字段 MenuControls
,因此当您创建 MainWindow
实例时,它会实例化 MenuControls
字段,它再次初始化它拥有的 MainWindow
字段,并继续这样做,因此导致计算器溢出异常。
public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls(); // note this
您需要从 MenuControls
class 中删除 MainWindow
字段,反之亦然,我看到您不需要 MainWindow
字段 MenuControl
class,从那里删除它。