我的程序出现静默错误,出现了问题。我不明白这是什么意思。 frominloggen上的this.ResumeLayout(false); ==>

然后弹出的窗口说:


  类型为“ System.StackOverflowException”的未处理异常
  发生在System.Windows.Forms.dll中


这是我的main():

namespace Gip_info_Benny
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frominloggen());
        }
    }
}


这是我的frominloggen代码

        public frominloggen()
        {
            InitializeComponent();

            timer1.Start();


            initialiseerDB();
            eersteForm = new frominloggen();
            tweedeForm = new FrmWelkom();
            derdeForm = new FrmKijkafwezighedenEnloonberekeningNa();
            vierdeForm = new FrmBekijklonen();
            vijfdeForm = new FrmKijkPersoneelsGegevensNaEnPasAan();
            zesdeForm = new FrmVoegPersoneelslidToe();
            zevendeForm = new FrmVerwijderPersoneelslid();
            achtsteFrom = new FrmPersoneelsgegevensWijzigenUitvoeren();

            txtPaswoordInvoeren.PasswordChar = '*';
        }

        private void initialiseerDB()
        {
            cnnGip = new SqlConnection();
            cnnGip.ConnectionString = @"Data Source=BENNYLAPTOP\SQLEXPRESS";
        }

        private void frominloggen_Load(object sender, EventArgs e)
        {
            MenuStrip strip;
            strip = new MenuStrip();

            ToolStripMenuItem keuze1, keuze2, keuze3, keuze4, keuze5;
            keuze1 = new ToolStripMenuItem("&Home");
            keuze2 = new ToolStripMenuItem("&Afwezigheden/Loonberekening");
            keuze3 = new ToolStripMenuItem("&Bekijk lonen");
            keuze4 = new ToolStripMenuItem("&Personeelsgegevens wijzigen");
            keuze5 = new ToolStripMenuItem("&Toevoegen/Verwijderen");
            keuze5.DropDownItems.Add("Toevoegen personeelslid", null, a_click);
            keuze5.DropDownItems.Add("Verwijderen personeelslid", null, b_click);
            strip.Items.Add(keuze1);
            strip.Items.Add(keuze2);
            strip.Items.Add(keuze3);
            strip.Items.Add(keuze4);
            strip.Items.Add(keuze5);
            this.Controls.Add(strip);
        }
        private void a_click(object sender, EventArgs e)
        {
            zesdeForm.ShowDialog();
        }

        private void b_click(object sender, EventArgs e)
        {
            zevendeForm.ShowDialog();
        }

        private void btnInlogen_Click(object sender, EventArgs e)
        {
            SqlCommand scmdcontroleergebruiker;
            scmdcontroleergebruiker = new SqlCommand();
            scmdcontroleergebruiker.Connection = cnnGip;
            scmdcontroleergebruiker.CommandType = CommandType.Text;
            scmdcontroleergebruiker.CommandText = "SELECT GebruikersNaam,Paswoord FROM Tbl_Account WHERE GebruikersNaam = '" + txtGebruikersnaamInvoeren.Text + "' , Paswoord = '" + txtPaswoordInvoeren.Text + "';";

            SqlDataReader sdrReadGebuiker;
            cnnGip.Open();
            sdrReadGebuiker = scmdcontroleergebruiker.ExecuteReader();

            int teller = 0;

            while (sdrReadGebuiker.Read())
            {
                teller += 1;
            }
            if (teller == 1)
            {
                MessageBox.Show("Gebruikersnaam en paswoord correct");
                tweedeForm.Naamgebruiker(txtGebruikersnaamInvoeren.Text); //is makkelijker als het werkt
                tweedeForm.ShowDialog();
                this.Hide();
            }
            else if (teller > 0)
            {
                MessageBox.Show("Gebruiker niet in gebruik");
            }
            else
            {
                MessageBox.Show("Gebruikersnaam of pasword niet correct");
            }

            txtGebruikersnaamInvoeren.Clear();
            txtPaswoordInvoeren.Clear();
            sdrReadGebuiker.Close();
            cnnGip.Close();
        }

        private void frominloggen_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Wil u het programma sluiten?", "Sluiten", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                Application.Exit();
            }
            else if (dialog == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime DatumTijd = DateTime.Now;
            lblHuidigetijd.Text = DatumTijd.ToString();
        }
    }
}


这是我的component():

namespace Gip_info_Benny
{
    partial class frominloggen
    {
        /// <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();
            this.txtGebruikersnaamInvoeren = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btnInlogen = new System.Windows.Forms.Button();
            this.txtPaswoordInvoeren = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lblHuidigetijd = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            //
            // txtGebruikersnaamInvoeren
            //
            this.txtGebruikersnaamInvoeren.Location = new System.Drawing.Point(644, 137);
            this.txtGebruikersnaamInvoeren.Name = "txtGebruikersnaamInvoeren";
            this.txtGebruikersnaamInvoeren.Size = new System.Drawing.Size(100, 20);
            this.txtGebruikersnaamInvoeren.TabIndex = 6;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(567, 186);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(54, 13);
            this.label2.TabIndex = 9;
            this.label2.Text = "Paswoord";
            //
            // btnInlogen
            //
            this.btnInlogen.Location = new System.Drawing.Point(649, 220);
            this.btnInlogen.Name = "btnInlogen";
            this.btnInlogen.Size = new System.Drawing.Size(95, 41);
            this.btnInlogen.TabIndex = 5;
            this.btnInlogen.Text = "Inloggen";
            this.btnInlogen.UseVisualStyleBackColor = true;
            this.btnInlogen.Click += new System.EventHandler(this.btnInlogen_Click);
            //
            // txtPaswoordInvoeren
            //
            this.txtPaswoordInvoeren.Location = new System.Drawing.Point(644, 183);
            this.txtPaswoordInvoeren.Name = "txtPaswoordInvoeren";
            this.txtPaswoordInvoeren.Size = new System.Drawing.Size(100, 20);
            this.txtPaswoordInvoeren.TabIndex = 7;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(537, 140);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(84, 13);
            this.label1.TabIndex = 8;
            this.label1.Text = "Gebruikersnaam";
            //
            // lblHuidigetijd
            //
            this.lblHuidigetijd.AutoSize = true;
            this.lblHuidigetijd.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblHuidigetijd.Location = new System.Drawing.Point(793, 411);
            this.lblHuidigetijd.Name = "lblHuidigetijd";
            this.lblHuidigetijd.Size = new System.Drawing.Size(0, 20);
            this.lblHuidigetijd.TabIndex = 10;
            //
            // frominloggen
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(866, 457);
            this.Controls.Add(this.lblHuidigetijd);
            this.Controls.Add(this.txtGebruikersnaamInvoeren);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnInlogen);
            this.Controls.Add(this.txtPaswoordInvoeren);
            this.Controls.Add(this.label1);
            this.Name = "frominloggen";
            this.Text = "frominloggen";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frominloggen_FormClosing);
            this.Load += new System.EventHandler(this.frominloggen_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtGebruikersnaamInvoeren;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnInlogen;
        private System.Windows.Forms.TextBox txtPaswoordInvoeren;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lblHuidigetijd;
        private System.Windows.Forms.Timer timer1;
    }
}

最佳答案

问题是您正在eersteForm = new frominloggen();类的构造函数中调用frominloggen。它陷入无限循环,因为new frominloggen()将再次调用frominloggen类的构造函数,依此类推。

为了检测到这一点,您只需要在IDE中使用调试器即可。例如,如果我制作了一个标准的winforms应用程序并重现了您的错误:

    public Form1()
    {
        InitializeComponent();

        var frm = new Form1();
    }


现在按播放(f5),使应用程序以预期的方式爆炸。调试器应该打断我们告诉我们有关堆栈溢出的信息。我们现在可以调出调用堆栈(ctrl + d,c);如下所示:



调用堆栈中的每一行都是不同的调用层。因此Form1()构造函数(第19行)正在调用Form1()构造函数,后者正在调用Form1()构造函数。在最顶层,通常会有一些不同之处,只是告诉我们在空间不足时它在做什么。如果我们查看Form1()构造函数中的第19行,则为:

var frm = new Form1();


这就是调查堆栈溢出的方式。顺便说一句,您可以双击调用堆栈窗口中的任何一行以直接跳到该调用站点,包括能够读取该方法中的任何局部变量(等)的功能-这样您就可以诊断每一步发生的情况在递归中(如果您有意处理对象树/图形或链接列表,则很有用)。

关于c# - 关于此的Stackoverflow异常。ResumeLayout(false);,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29913632/

10-10 23:30