如何让用户输入RichTextBox粘贴消息的次数?

时间:2019-10-15 09:12:35

标签: c# forms

我目前有一个表单,如果您在Richtextbox中键入内容,它将粘贴一条消息1,000次。我正在尝试获得一种表单,您可以在其中输入文本框,以将消息粘贴多少次。

private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        richTextBox.Text = "Hello world\r\n";
        for (int i = 0; i < 10; ++i) richTextBox.Text += richTextBox.Text;
    }

1 个答案:

答案 0 :(得分:0)

这是非常简单的问题之一,我不确定我是否正确理解了您的问题。

  1. 您需要输入UI元素。 当然,另一个文本框也可以。但也有NumericUpDown Control

  2. 除非控件已经提供了整数,否则您将String Input解析为Integer。使用TryParse()

  3. 您使用该解析数字而不是10常数。

最后一点,应该避免对控件进行循环操作。编写用户界面非常昂贵。如果每个用户触发的事件仅执行一次,则不会注意到它。但是循环执行此操作,您会发现它。我什至编写了示例代码来说明这一点:

using System;
using System.Windows.Forms;

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

        int[] getNumbers(int upperLimit)
        {
            int[] ReturnValue = new int[upperLimit];

            for (int i = 0; i < ReturnValue.Length; i++)
                ReturnValue[i] = i;

            return ReturnValue;
        }

        void printWithBuffer(int[] Values)
        {
            textBox1.Text = "";
            string buffer = "";

            foreach (int Number in Values)
                buffer += Number.ToString() + Environment.NewLine;
            textBox1.Text = buffer;
        }

        void printDirectly(int[] Values){
            textBox1.Text = "";

            foreach (int Number in Values)
                textBox1.Text += Number.ToString() + Environment.NewLine;
        }

        private void btnPrintBuffer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(10000);
            MessageBox.Show("Printing with buffer");
            printWithBuffer(temp);
            MessageBox.Show("Printing done");
        }

        private void btnPrintDirect_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Generating Numbers");
            int[] temp = getNumbers(1000);
            MessageBox.Show("Printing directly");
            printDirectly(temp);
            MessageBox.Show("Printing done");
        }
    }
}