C#的 窗口无法输出值

![图片说明](https://img-ask.csdn.net/upload/201811/20/1542720105_828719.png)图片说明
代码和运行窗口已经截图
num1,num2分别为第一个第二个整数,运行代码后,无输出结果,而且窗口卡住

c#

2个回答

不能写在Text3_Changed里,要写在按钮的click里
你在Text3_Changed里修改Text3.Text,导致又触发了新的Text3_Changed,如此这般,最后导致堆栈溢出。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Q714037
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a = int.Parse(num1.Text);
            int b = int.Parse(num2.Text);
            for (int i = a; i <= b; i++)
            { 
                bool p = true;
                for (int j = 2; j <= i / 2; j++)
                    if (i % j == 0) { p = false; break; }
                if (p && i >= 2) num3.Text += i.ToString() + " ";
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            num3.Text = "";
        }
    }
}


窗体设计文件:

namespace Q714037
{
    partial class Form1
    {
        /// <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.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.num1 = new System.Windows.Forms.TextBox();
            this.num2 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.num3 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(126, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(115, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "求两个数之前的素数";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(19, 52);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(67, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "第一个整数";
            // 
            // num1
            // 
            this.num1.Location = new System.Drawing.Point(92, 49);
            this.num1.Name = "num1";
            this.num1.Size = new System.Drawing.Size(86, 20);
            this.num1.TabIndex = 2;
            // 
            // num2
            // 
            this.num2.Location = new System.Drawing.Point(279, 49);
            this.num2.Name = "num2";
            this.num2.Size = new System.Drawing.Size(86, 20);
            this.num2.TabIndex = 4;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(206, 52);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(67, 13);
            this.label3.TabIndex = 3;
            this.label3.Text = "第二个整数";
            // 
            // num3
            // 
            this.num3.Location = new System.Drawing.Point(20, 89);
            this.num3.Multiline = true;
            this.num3.Name = "num3";
            this.num3.Size = new System.Drawing.Size(344, 177);
            this.num3.TabIndex = 5;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(92, 280);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(70, 22);
            this.button1.TabIndex = 6;
            this.button1.Text = "找素数";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(236, 280);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(70, 22);
            this.button2.TabIndex = 7;
            this.button2.Text = "清除";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(390, 323);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.num3);
            this.Controls.Add(this.num2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.num1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox num1;
        private System.Windows.Forms.TextBox num2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox num3;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}


图片说明

qq_43412960
qq_43412960 回复caozhy: 嗯
10 个月之前 回复
caozhy
贵阳老马马善福专门编写代码的老马就是我! 回复qq_43412960: 不客气,有空帮我把几个问题采纳下,谢谢
10 个月之前 回复
qq_43412960
qq_43412960 回复caozhy: 谢谢大佬
10 个月之前 回复
caozhy
贵阳老马马善福专门编写代码的老马就是我! 回复qq_43412960: TextChanged在文本框文本修改的时候触发,和你有没有双击没关系。
10 个月之前 回复
qq_43412960
qq_43412960 回复caozhy: 双击,产生一个方法的实质,不是点击一下输出框在启动吗,文本改变,又没点击输出框
10 个月之前 回复
caozhy
贵阳老马马善福专门编写代码的老马就是我! 回复qq_43412960: 你num3.Text = xxx,就会导致文本改变,就会触发新的TextChange
10 个月之前 回复
qq_43412960
qq_43412960 为什么会又触发呢 我又不是总点
10 个月之前 回复

图片说明运行截图

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!