【问题标题】:How can i copy from textBox1 the text to textBox2 including spaces?如何从 textBox1 将文本复制到 textBox2,包括空格?
【发布时间】:2013-07-01 17:46:54
【问题描述】:

我有一个用户在 textBox1 中输入手册的文本 然后单击一个按钮,将文本从 textBox1 复制到 textBox2,但在 textBox2 中,文本看起来像一个长字符串。

我希望它在复制文本时也会复制单词之间的确切空格。

在我的新课程中,我在顶部有这段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScrambleRandomWordsTest
{
    class ScrambleTextBoxText
    {
        private static readonly Random RandomGen = new Random();
        private List<string> _words;
        private List<string> _scrambledWords;

        public ScrambleTextBoxText(List<string> textBoxList)
        {
            _words = textBoxList;
        }

然后在底部我有这个功能:

public List<string> scrambledTextBoxWords()
        {
            List<string> words = _scrambledWords;
            return words;
        }

然后在 Form1 中的按钮单击事件中我有:

private void BtnScrambleText_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            BtnScrambleText.Enabled = false;
            textBoxWords = ExtractWords(textBox1.Text);
            ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(textBoxWords);
            for (int i = 0; i < scrmbltb.scrambledTextBoxWords().Count; i++)
            {
                textBox2.AppendText(scrmbltb.scrambledTextBoxWords()[i]);
            }
        }

所以我在 Form1 中输入了一些文本,例如:

丹尼你好黄色

然后我为新类创建实例,并根据我的需要取回单词列表。 并使用 AppendText 将它们添加到 textBox2

问题是在 textBox2 中的文本看起来像:

dannyhelloyellow

我希望它看起来和 textBox1 中的一样,包括空格: 例如在 hello 和 yellow 之间有 7 个空格,所以在 textBox2 中它看起来像:

丹尼你好黄色

我该怎么做?

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    最简单的方法是

    textBox2.Text = String.Join(" ", scrmbtb.scrambledTextBoxWords());
    

    使用您当前的解决方案

    textBox2.AppendText(scrmbltb.scrambledTextBoxWords()[i] + " ");
    

    如果这就是你的全部功能,你最好将你的类更改为类似的东西。

    你有

    private List<string> _scrambledWords;
    public List<string> scrambledTextBoxWords()
    {
        List<string> words = _scrambledWords;
        return words;
    }
    

    相同
    public List<string> ScrambledTextBoxWords {get; private set;}
    

    然后

    textBox2.Text = String.Join(" ", scrmbtb.ScrambledTextBoxWords);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多