多行,textBox1.Text =
RGYR
RGGB
RGRG
RYBG
RYYB
GBRY
RYBG
我希望游戏一次只读一行;
Color[] colourset = newSequence(textBox1.Text.Length);
正在读取字符串作为一个整体,而没有预料到每个第4个字符的换行符并且它搞乱了所有内容。
如何在多行文本框中为每个索引读取一行?
上下文代码:
private Color[] sequence;
//Declare dictionary
private Dictionary<char,Color> stringTocolor = new Dictionary<char,Color>();
public SimonSays ()
{
//add content to Dictionary
stringTocolor.Add('R', Color.Red);
stringTocolor.Add('G', Color.Green);
stringTocolor.Add('B', Color.Blue);
stringTocolor.Add('Y', Color.Yellow);
Color[] colourset = newSequence(textBox1.Text.Length); //This may be the problem? Reading entire string length instead of just one line at a time??
}
public Color[] newSequence(int length)
{
Color[] array = new Color[length];
//check dictionary has the char key or not
for (int i = 0; i < textBox1.Text.Length; i++)
{
if (stringTocolor.ContainsKey(textBox1.Text[i]))
{
array[i] = stringTocolor[textBox1.Text[i]];
}
//give alert if wrong key
else
{
MessageBox.Show("Wrong Colour input at index " + i + " of textbox string!");
}
}
this.sequence = array;
return array;
}
public void newSequence (Color [] sequence)
{
this.sequence= //read next line of string
}
答案 0 :(得分:1)
您可以通过Lines属性进行索引:
for(int i = 0 ; i < textBox1.Lines.Length; i++)
Color[] colourset = newSequence(textBox1.Lines[i].Length);
答案 1 :(得分:0)
如何在多行文本框中为每个索引读取一行?
您可以根据EnvironMent.NewLine
拆分字符串,然后在循环中使用索引获取所需的项目。
试试这个:
var lines = textBox1.Text.Split(new [] {Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries);
答案 2 :(得分:0)
我做到了,原来就是这个。
Color[] colourset = newSequence(textBox1.Lines[0].Length);