简体   繁体   中英

Output multiple numbers in GUI program - C#

Working on a project and I think I have most of the bones worked out. The problem is that while I can get output, it's not complete output.

Basically, the goal is to make a GUI that has two text boxes where the user can enter numbers from 1 to 50 (num1 for min, num2 for max) and then select from four different boxes for information. First box is show the prime numbers, second is show even numbers, third is show primes, and forth is show a multiplication table laid out with numbers on top row, and multiplication results going down.

I think I have all the right equations and loops in, the problem is that I can't get the right output. I can only get one number whenever an option is selected. It's in the proper range, but I need all numbers in the range to appear.

Here's what I got coded (checkbox 4 is at the start because I can't move it from that spot now without messing up the program, apparently). Really trying to figure out what step I'm missing.

public partial class Form1 : Form
{
    private bool isPrime;

    public Form1()
    {
        InitializeComponent();
    }

    private void checkBox4_CheckedChanged(object sender, EventArgs e)
    {
        int num1 = Convert.ToInt32(textBox1.Text);
        int num2 = Convert.ToInt32(textBox2.Text);

        for (int i = num1; i <= num2; i++)
        {
            for (int j = num1; j <= num2; j++)
            {
                int res = i * j;
                textBox3.Text = res.ToString();
            }

        }
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {

        int num1 = Convert.ToInt32(textBox1.Text);
        int num2 = Convert.ToInt32(textBox2.Text);

        for (int i = num1; i <= num2; i++)
        {
            for (int j = num1; j <= num2; j++)
            {

                if (i != j && i % j == 0)
                {
                    isPrime = false;
                    break;
                }

            }
            if (isPrime)
            {
                textBox3.Text = i.ToString();
            }
            isPrime = true;
        }
    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        int num1 = Convert.ToInt32(textBox1.Text);
        int num2 = Convert.ToInt32(textBox2.Text);

        for(int i = num1; i <= num2; i++)
        {
            if (i % 2 == 0)
            {
                textBox3.Text = i.ToString();
            }
        }
    }

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {
        int num1 = Convert.ToInt32(textBox1.Text);
        int num2 = Convert.ToInt32(textBox2.Text);

        for (int j = num1; j <= num2; j++)
        {
            if (j % 2 != 0)
            {
                textBox3.Text = j.ToString();
            }
        }
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }
}

Try setting textBox3 to multiline

Then try this:

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
    int num1 = Convert.ToInt32(textBox1.Text);
    int num2 = Convert.ToInt32(textBox2.Text);
    string result="";


    for (int i = num1; i <= num2; i++)
    {
        for (int j = num1; j <= num2; j++)
        {
            int res = i * j;
            result=string.format("{0}\n{1}",result,res.ToString());
        }

    }
    textBox3.Text=result;
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
    int num1 = Convert.ToInt32(textBox1.Text);
    int num2 = Convert.ToInt32(textBox2.Text);

    for (int i = num1; i <= num2; i++)
    {
        for (int j = num1; j <= num2; j++)
        {
            int res = i * j;
            textBox3.Text += res.ToString() + " ";
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM