Designer.cs中的堆栈溢出

时间:2016-01-03 00:50:56

标签: c# winforms

我的winform的自动生成代码中发生了堆栈溢出。它只发生在表单的自动生成代码的开头,而不是其中的任何控件。我尝试删除第一行,它发生在下一行。没有堆栈跟踪或内部异常,请帮忙。

修改

以下是表单的代码:

namespace Eternal_Continent
{
    public partial class Almanac : Form
    {
        public Almanac()
        {
            InitializeComponent();
        }
        public List<string> Content = new List<string>();
        private void Almanac_Load(object sender, EventArgs e)
        {
            timer1.Interval = 5000;
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(Application.StartupPath + "\\Resources\\font_name.ttf");
            textBox1.Font = new Font(pfc.Families[0], 36);
        }

        private void Almanac_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }
    }
}

以下是设计师的作品:

namespace Eternal_Continent
{
    partial class Almanac
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Almanac));
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.BackColor = System.Drawing.Color.Khaki;
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(546, 582);
            this.textBox1.TabIndex = 0;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            // 
            // Almanac
            // I removed the autoscale lines here, because I wanted to see if it would still create errors, it did
            this.BackgroundImage = Properties.Resources.Stone;
            this.ClientSize = new System.Drawing.Size(546, 582);
            this.Controls.Add(this.textBox1);
            this.Icon = Properties.Textures.EternalContinent1;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Almanac";
            this.Text = "Almanac";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Almanac_FormClosing);
            this.Load += new System.EventHandler(this.Almanac_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
            this.Dispose();
        }

        #endregion
        private System.Windows.Forms.Timer timer1;
        public System.Windows.Forms.TextBox textBox1;
    }
}

编辑#2 删除Dispose()行会导致

  

当前进程已使用Window Manager对象的句柄的所有系统容差

在我的Resources.Designer.cs。

2 个答案:

答案 0 :(得分:0)

这通常会导致意外的间接递归。我在这里有一个旧答案可能有所帮助:https://stackoverflow.com/a/4734422/26414

值得注意的是,如果您逐步使用F11,您可能会看到循环模式,告诉您什么是什么。请注意您正在调试哪个线程。如果有多个线程正在执行用户代码,我相信当逐步执行时,当前行会得到一个更深的黄色突出显示。

答案 1 :(得分:0)

我尝试了您的代码,但我得到了ObjectDisposed例外。在调试时,结果表明您的自动生成的代码的最后一行为this.Dispose(),这是不正确的。

删除该行(InitializeComponent()的最后一行)后,设计人员和代码工作正常,没有任何错误

请注意,我必须注释掉与资源相关的以下行,因为我的项目中并没有这样做。文件系统

// Almanac.cs
pfc.AddFontFile(Application.StartupPath + "\\Resources\\font_name.ttf");

// Almanac.Designer.cs
this.BackgroundImage = Properties.Resources.Stone;
this.Icon = Properties.Textures.EternalContinent1;

您还应该从代码中注释掉这些行,以确保获得与我相同的结果,然后一次包含一行以确保它们不会导致任何问题

注意:我在Win 7上使用VS 2013