我有一个用户在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;
}
Run Code Online (Sandbox Code Playgroud)
然后在底部我有这个功能:
public List<string> scrambledTextBoxWords()
{
List<string> words = _scrambledWords;
return words;
}
Run Code Online (Sandbox Code Playgroud)
然后在按钮点击事件的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]);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我在Form1中键入一些文本,例如:
丹尼你好黄色
然后我为新类创建实例并返回我想要的单词列表.并使用AppendText将它们添加到textBox2
Thep roblem就是在textBox2中,文本看起来像:
dannyhelloyellow
我希望它看起来和textBox1中的相同,包括空格:例如,在hello和yellow之间有7个空格,所以在textBox2中它看起来像:
丹尼你好黄色
我该怎么做 ?
最简单的方法是
textBox2.Text = String.Join(" ", scrmbtb.scrambledTextBoxWords());
Run Code Online (Sandbox Code Playgroud)
使用当前的解决方案
textBox2.AppendText(scrmbltb.scrambledTextBoxWords()[i] + " ");
Run Code Online (Sandbox Code Playgroud)
如果这就是你的所有功能,你最好把你的班级变成类似的东西.
你有
private List<string> _scrambledWords;
public List<string> scrambledTextBoxWords()
{
List<string> words = _scrambledWords;
return words;
}
Run Code Online (Sandbox Code Playgroud)
这是一样的
public List<string> ScrambledTextBoxWords {get; private set;}
Run Code Online (Sandbox Code Playgroud)
然后
textBox2.Text = String.Join(" ", scrmbtb.ScrambledTextBoxWords);
Run Code Online (Sandbox Code Playgroud)